Log4net 如何使用aspnet会话模式布局?

Log4net 如何使用aspnet会话模式布局?,log4net,pattern-layout,Log4net,Pattern Layout,我有adonet appender,并定义了附加列。我想从asp.net会话中获取用户ID并记录日志 根据我使用的%aspnet会话{key}模式如下: <parameter> <parameterName value="@userId" /> <dbType value="String" /> <size value="255" /> <layout

我有adonet appender,并定义了附加列。我想从asp.net会话中获取用户ID并记录日志

根据我使用的%aspnet会话{key}模式如下:

<parameter>
    <parameterName value="@userId" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%aspnet-session{current_member}" />
    </layout>
  </parameter>
我在数据库中得到了以下结果:


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


我做错了什么?

我找到了解决问题的办法

我只是对其进行了重构以满足我的需要:

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;
        }
    }

我找到了解决问题的办法

我只是对其进行了重构以满足我的需要:

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;
        }
    }

/LM/W3SVC/1/ROOT/trunk-1-129718741958458380spnet-session{current_member} 是因为在log4net定义中有这样的东西才被记录的,对吗

<parameter>
        <parameterName value="@user" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-session{current_memeber}" />
        </layout>
      </parameter>
log4net正在从%aspnet会话{current_memeber}中提取%a,并认为您需要应用程序域。%是转换为应用程序域的log4net模式。这真的很烦人,我最近碰到了这件事,不知道该怎么办

请看这里:

找到了解决这个问题的办法。如果您使用log4net 1.2.11或更高版本,那么像这样声明您的参数应该可以工作

<parameter>
        <parameterName value="@session" />
        <dbType value="String" />
        <size value="2147483647"/>
        <layout type="log4net.Layout.PatternLayout">
          <converter>
            <name value ="AspNetSessionPatternConverter"/>
            <type value="log4net.Layout.Pattern.AspNetSessionPatternConverter"/>
          </converter>
          <conversionPattern value="%aspnet-session{gwsession}" />
        </layout>
      </parameter>

我不知道%a对appdomain短路是否是一个错误,或者您是否应该使用aspnetsessionpatternconverter像这样声明参数,但是如果log4net文档得到更新就更好了。希望这对其他人有所帮助。

/LM/W3SVC/1/ROOT/trunk-1-129718741958458380spnet-session{current_member} 是因为在log4net定义中有这样的东西才被记录的,对吗

<parameter>
        <parameterName value="@user" />
        <dbType value="String" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%aspnet-session{current_memeber}" />
        </layout>
      </parameter>
log4net正在从%aspnet会话{current_memeber}中提取%a,并认为您需要应用程序域。%是转换为应用程序域的log4net模式。这真的很烦人,我最近碰到了这件事,不知道该怎么办

请看这里:

找到了解决这个问题的办法。如果您使用log4net 1.2.11或更高版本,那么像这样声明您的参数应该可以工作

<parameter>
        <parameterName value="@session" />
        <dbType value="String" />
        <size value="2147483647"/>
        <layout type="log4net.Layout.PatternLayout">
          <converter>
            <name value ="AspNetSessionPatternConverter"/>
            <type value="log4net.Layout.Pattern.AspNetSessionPatternConverter"/>
          </converter>
          <conversionPattern value="%aspnet-session{gwsession}" />
        </layout>
      </parameter>

我不知道%a对appdomain短路是否是一个错误,或者您是否应该使用aspnetsessionpatternconverter像这样声明参数,但是如果log4net文档得到更新就更好了。希望这对其他人有所帮助。

您需要使用log4net 1.2.11或更高版本才能访问ASP.NET模式转换器


当您使用较旧版本的log4net时,将收到LM/W3SVC/1/ROOT/trunk-1-129718741958458380spnet-session{current_member}消息。

您需要使用log4net 1.2.11或更高版本才能访问ASP.NET模式转换器


当您使用较旧版本的log4net时,将收到LM/W3SVC/1/ROOT/trunk-1-129718741958458380spnet-session{current_member}消息。

从log4net版本1.2.11.0开始,您可以使用ASP.NET转换器的%aspnet request{ASP.NET_SessionId},如下所示:

<conversionPattern value="%date %level %logger [%aspnet-request{ASP.NET_SessionId}] - %message%newline" />
见:

-支持ASP.Net相关模式转换器,以允许捕获来自HttpContext.Current.Session、缓存、请求等的项。
从log4net版本1.2.11.0开始,您可以使用ASP.NET转换器的%aspnet请求{ASP.NET_SessionId},如下所示:

<conversionPattern value="%date %level %logger [%aspnet-request{ASP.NET_SessionId}] - %message%newline" />
见:

-支持ASP.Net相关模式转换器,以允许捕获来自HttpContext.Current.Session、缓存、请求等的项。
1.2.11在我提出问题时没有公布。因此,如果你使用较新的版本,你就可以了。如果没有看到我的答案上面。peace1.2.11如果在记录时没有建立会话,则会崩溃。例如,应用程序启动期间的日志消息只会导致部分完整的日志条目,没有换行符。在这方面是否存在JIRA问题?1.2.11在我提问时没有发布。因此,如果你使用较新的版本,你就可以了。如果没有看到我的答案上面。peace1.2.11如果在记录时没有建立会话,则会崩溃。例如,应用程序启动期间的日志消息只会导致部分完整的日志条目,没有换行符。是否存在JIRA问题?