How can I include SessionID in log files using log4net in ASP.NET?

后端 未结 5 744
执念已碎
执念已碎 2020-12-25 13:00

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

I\'ve got log4net working with the RollingLogFileAppender for my web application. I\'

5条回答
  •  爱一瞬间的悲伤
    2020-12-25 13:12

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

    References: 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):

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

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

提交回复
热议问题