如何使用aspnet-session模式布局? [英] How to use aspnet-session pattern layout?

查看:83
本文介绍了如何使用aspnet-session模式布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有adonet附加程序,并且定义了其他列.我想从asp.net会话中获取userId并进行记录.

I have adonet appender and I defined additional column. I want to get the userId from the asp.net session and do log.

根据此页面我有这样使用的%aspnet-session {key}模式:

According to this page there is %aspnet-session{key} pattern which I use like this:

<parameter>
    <parameterName value="@userId" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%aspnet-session{current_member}" />
    </layout>
  </parameter>

我在数据库中得到以下结果:

and I got the following result in the database:

/LM/W3SVC/1/ROOT/trunk-1-129718741958458380spnet-session {current_member}

我在这里做错了什么?

推荐答案

我找到了解决问题的方法.

I found a solution to my problem.

我只是重构它以满足我的需求:

I just refactored it to serve my needs:

public class Log4NetAspNetProperty
    {
        private const string PropertyNamePrefix = "log4net_app_";
        private const string PropertyDefaultValue = null;

        private readonly string propertyName;
        private readonly object propertyValue;

        public string Name { get { return propertyName; } }

        private Log4NetAspNetProperty(string propertyName, object propertyValue)
        {
            if (String.IsNullOrWhiteSpace(propertyName)) throw new ArgumentNullException("propertyName");

            this.propertyName = propertyName;
            this.propertyValue = propertyValue;

            if (HttpContext.Current != null)
                HttpContext.Current.Items[GetPrefixedPropertyName()] = propertyValue;
        }

        public override string ToString()
        {
            if (HttpContext.Current == null)
                return PropertyDefaultValue;

            var item = HttpContext.Current.Items[GetPrefixedPropertyName()];
            return item != null ? item.ToString() : PropertyDefaultValue;
        }

        private static string GetPrefixedPropertyName()
        {
            return String.Format("{0}{1}", PropertyNamePrefix, PropertyDefaultValue);
        }

        public static void CurrentUserId(object userId)
        {
            var property = new Log4NetAspNetProperty("CurrentUserId", userId);
            log4net.ThreadContext.Properties[property.Name] = property;
        }

        public static void CurrentUrl(object url)
        {
            var property = new Log4NetAspNetProperty("CurrentUrl", url);
            log4net.ThreadContext.Properties[property.Name] = property;
        }
    }

这篇关于如何使用aspnet-session模式布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆