Georg Jansen posted on October 08, 2005 16:27
When debugging web applications, with several clients making request at the (almost) same time, we need some way of distinguish and identify the different requests and sessions.
Using Log4Net as my preferred logging method and storing logs in a SQL server database, one approach to this issue is to takes advantage of Log4net’s Thread.Context.Properties.
You could define several properties which would distinguish the sessions, one simple one is to use the requesting ip, if your ASP.NET application requires login and authentication username would be another. Most ASP.Net applications also have some sort of session management; using generated session identification is a third method.
1. Set property value in global.aspx using the BeginRequest event.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
log4net.ThreadContext.Properties["ip"] =
HttpContext.Current.Request.ServerVariables["REMOTE_HOST"];
log.Debug("Begin request");
}
2. I also prefer to log the EndRequest.
protected void Application_EndRequest(Object sender, EventArgs e)
{
log.Debug("End request");
}
3. In the configuration section for the AdoNetAppender, I output the IP in the thread column, taking advantage of log4Net’s PatternLayout.
<parameter>
<parameterName value="@thread"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread ip=%property{ip}"/>
</layout>
</parameter>
The same approach can of course be used in other appenders, using the same method.
Using L4NDash, you can use the global filter to create a filter so that you only get the logging events from the session you are tracking.