如何在ASP.NET中使用log4net在日志文件中包含SessionID?

时间:2021-12-17 06:35:52

I'm new to log4net, so hopefully this is a really easy question for someone?!

我是log4net的新手,所以希望对某人来说这是一个非常简单的问题?!

I've got log4net working with the RollingLogFileAppender for my web application. I'm using logging to try and find where some performance issues are coming from. In order to do this, it'd be useful to include the ASP.NET SessionID in the log output so that I can make sure I'm looking at log entries for a specific user.

我的log4net使用了RollingLogFileAppender来处理我的Web应用程序。我正在使用日志记录来尝试查找某些性能问题的来源。为了做到这一点,在日志输出中包含ASP.NET SessionID是有用的,这样我就可以确保我正在查看特定用户的日志条目。

Is there any way I can do this through the conversionPattern setting for the appender? Is there a %property{??} setting I can use?

有什么办法可以通过appender的conversionPattern设置来做到这一点吗?我可以使用%property {??}设置吗?

UPDATE: This question still hasn't been answered - does anybody have any ideas?

更新:这个问题仍未得到解答 - 有没有人有任何想法?

5 个解决方案

#1


20  

Alexander K. is nearly correct. The only problem with it is that the PostAcquireRequestState event also occurs for static requests. A call to Session in this situation will cause a HttpException.

Alexander K.几乎是正确的。唯一的问题是PostAcquireRequestState事件也发生在静态请求中。在这种情况下调用Session会导致HttpException。

Therefore the correct solution becomes:

因此正确的解决方案变为:

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState)
    {
        log4net.ThreadContext.Properties["SessionId"] = Session.SessionID;
    }
}

#2


14  

UPDATE (2014-06-12): Starting from log4net 1.2.11 you can use %aspnet-request{ASP.NET_SessionId} in conversion pattern for this purpose.

更新(2014-06-12):从log4net 1.2.11开始,您可以在转换模式中使用%aspnet-request {ASP.NET_SessionId}来实现此目的。

References: https://issues.apache.org/jira/browse/LOG4NET-87 http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

参考文献:https://issues.apache.org/jira/browse/LOG4NET-87 http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html


You should create Application_PostAcquireRequestState handler in Global.asax.cs (it is called in every request):

您应该在Global.asax.cs中创建Application_PostAcquireRequestState处理程序(在每个请求中调用它):

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    log4net.ThreadContext.Properties["SessionID"] = Session.SessionID;
}

And add [%property{SessionID}] to conversionPattern.

并将[%property {SessionID}]添加到conversionPattern。

#3


5  

Someone corrects me if I am wrong, but one ASP.NET thread can handle multiple sessions, so you can not use Session_Start as it is called once when the session starts. What it means is that as soon as a different user accesss the web site, your log4net.ThreadContext might be overwritten by the new user information.

如果我错了,有人会纠正我,但是一个ASP.NET线程可以处理多个会话,所以你不能使用Session_Start,因为它在会话启动时被调用一次。这意味着只要不同的用户访问该网站,您的log4net.ThreadContext就可能被新的用户信息覆盖。

You can either put the below code in Application_AcquireRequestState, or create a HttpModule and do that in AcquireRequestState method. AcquireRequestState is called when ASP.NET runtime is ready to acquire the Session state of the current HTTP request. If you interested in getting username, you can do that in AuthenticateRequest which is raised when ASP.NET runtime is ready to authenticate the identity of the user (and before the AcquireRequestState).

您可以将以下代码放在Application_AcquireRequestState中,也可以创建HttpModule并在AcquireRequestState方法中执行此操作。当ASP.NET运行时准备好获取当前HTTP请求的会话状态时,将调用AcquireRequestState。如果您对获取用户名感兴趣,可以在AuthenticateRequest中执行该操作,这是在ASP.NET运行时准备好验证用户身份(以及在AcquireRequestState之前)时引发的。

    private void AcquireRequestState(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        log4net.ThreadContext.Properties["SessionId"] = context.Session.SessionID;
     }

After that you can set up your log4net.config (or in web.config) like this.

之后,您可以像这样设置log4net.config(或在web.config中)。

<appender name="rollingFile"
      type="log4net.Appender.RollingFileAppender,log4net" >
  <param name="AppendToFile" value="false" />
  <param name="RollingStyle" value="Date" />
  <param name="DatePattern" value="yyyy.MM.dd" />
  <param name="StaticLogFileName" value="true" />

  <param name="File" value="log.txt" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern"
      value="%property{SessionId} %d [%t] %-5p %c - %m%n" />
  </layout>
</appender>

Hope this helps!

希望这可以帮助!

#4


5  

I was looking answer for this too and found out that %aspnet-request{ASP.NET_SessionId} works fine for me.

我也在寻找答案,并发现%aspnet-request {ASP.NET_SessionId}对我来说很好。

#5


1  

You can try:

你可以试试:

<conversionPattern
    value="%date %-5level %logger ${COMPUTERNAME} [%property{SessionID}] - %message%newline" />

...in your Web.config, and in Global.asax.cs:

...在您的Web.config和Global.asax.cs中:

protected void Session_Start(object sender, EventArgs e)
{
    log4net.ThreadContext.Properties["SessionID"] = Session.SessionID;
    log4net.Config.XmlConfigurator.Configure();
}

#1


20  

Alexander K. is nearly correct. The only problem with it is that the PostAcquireRequestState event also occurs for static requests. A call to Session in this situation will cause a HttpException.

Alexander K.几乎是正确的。唯一的问题是PostAcquireRequestState事件也发生在静态请求中。在这种情况下调用Session会导致HttpException。

Therefore the correct solution becomes:

因此正确的解决方案变为:

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState)
    {
        log4net.ThreadContext.Properties["SessionId"] = Session.SessionID;
    }
}

#2


14  

UPDATE (2014-06-12): Starting from log4net 1.2.11 you can use %aspnet-request{ASP.NET_SessionId} in conversion pattern for this purpose.

更新(2014-06-12):从log4net 1.2.11开始,您可以在转换模式中使用%aspnet-request {ASP.NET_SessionId}来实现此目的。

References: https://issues.apache.org/jira/browse/LOG4NET-87 http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

参考文献:https://issues.apache.org/jira/browse/LOG4NET-87 http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html


You should create Application_PostAcquireRequestState handler in Global.asax.cs (it is called in every request):

您应该在Global.asax.cs中创建Application_PostAcquireRequestState处理程序(在每个请求中调用它):

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    log4net.ThreadContext.Properties["SessionID"] = Session.SessionID;
}

And add [%property{SessionID}] to conversionPattern.

并将[%property {SessionID}]添加到conversionPattern。

#3


5  

Someone corrects me if I am wrong, but one ASP.NET thread can handle multiple sessions, so you can not use Session_Start as it is called once when the session starts. What it means is that as soon as a different user accesss the web site, your log4net.ThreadContext might be overwritten by the new user information.

如果我错了,有人会纠正我,但是一个ASP.NET线程可以处理多个会话,所以你不能使用Session_Start,因为它在会话启动时被调用一次。这意味着只要不同的用户访问该网站,您的log4net.ThreadContext就可能被新的用户信息覆盖。

You can either put the below code in Application_AcquireRequestState, or create a HttpModule and do that in AcquireRequestState method. AcquireRequestState is called when ASP.NET runtime is ready to acquire the Session state of the current HTTP request. If you interested in getting username, you can do that in AuthenticateRequest which is raised when ASP.NET runtime is ready to authenticate the identity of the user (and before the AcquireRequestState).

您可以将以下代码放在Application_AcquireRequestState中,也可以创建HttpModule并在AcquireRequestState方法中执行此操作。当ASP.NET运行时准备好获取当前HTTP请求的会话状态时,将调用AcquireRequestState。如果您对获取用户名感兴趣,可以在AuthenticateRequest中执行该操作,这是在ASP.NET运行时准备好验证用户身份(以及在AcquireRequestState之前)时引发的。

    private void AcquireRequestState(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        log4net.ThreadContext.Properties["SessionId"] = context.Session.SessionID;
     }

After that you can set up your log4net.config (or in web.config) like this.

之后,您可以像这样设置log4net.config(或在web.config中)。

<appender name="rollingFile"
      type="log4net.Appender.RollingFileAppender,log4net" >
  <param name="AppendToFile" value="false" />
  <param name="RollingStyle" value="Date" />
  <param name="DatePattern" value="yyyy.MM.dd" />
  <param name="StaticLogFileName" value="true" />

  <param name="File" value="log.txt" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern"
      value="%property{SessionId} %d [%t] %-5p %c - %m%n" />
  </layout>
</appender>

Hope this helps!

希望这可以帮助!

#4


5  

I was looking answer for this too and found out that %aspnet-request{ASP.NET_SessionId} works fine for me.

我也在寻找答案,并发现%aspnet-request {ASP.NET_SessionId}对我来说很好。

#5


1  

You can try:

你可以试试:

<conversionPattern
    value="%date %-5level %logger ${COMPUTERNAME} [%property{SessionID}] - %message%newline" />

...in your Web.config, and in Global.asax.cs:

...在您的Web.config和Global.asax.cs中:

protected void Session_Start(object sender, EventArgs e)
{
    log4net.ThreadContext.Properties["SessionID"] = Session.SessionID;
    log4net.Config.XmlConfigurator.Configure();
}