You can override the Application_PreRequestHandlerExecute method in your Global.asax. This method is called for each request just before the handler is run, i.e. it is called just before the code in your page is executed, but after authentication, caching, session resolution etc...
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) { log4net.MDC.Set("sessionID", HttpContext.Current.Session.SessionID); } Also unless you use threadpool threads for your own background worker processes there is no need to clear the MDC value, it will be set for each request as it occurs. Nicko > -----Original Message----- > From: Hart, Leo [mailto:[EMAIL PROTECTED] > Sent: 10 February 2005 13:43 > To: Log4NET User > Subject: Logging in a Web App (Threads, Sessions and Requests) > > I have a Log4Net problem/situation that I'm sure many of you > have come across and have (hopefully) overcome. I scoured > the 'net for an answer but wasn't able to find exactly what I needed. > > I'm starting to use Log4Net in my web application and I'm > quite pleased with it overall. I was a user of Log4J when I > was programming J2EE apps, so obviously Log4Net is a > comfortable fit. > > I've embedded log statements throughout the various tiers of > my enterprise app and it's outputting useful information to > my rolling log file just fine. The problem is when the app > leaves my development machine and moves to a production > server with multiple users hitting it at once. At that point > it becomes very difficult to identify which log entries > pertain to which users. > > So obviously I want to embed the session ID and/or the > request ID in my log entry. The first thing that came to > mind was to simply log the session/request IDs in the > Page_Load/Page_Unload event, but that doesn't work because > the other log entries that occur in between Page_Load and > Page_Unload won't have that info. > > The next thing that came to mind was MDC/NDC. The problem > with that is that MDC/NDC seems to be thread-based and since > a given IIS server thread can support multiple sessions at > once, the sessionID MDC can be overwritten. I realized this > fact once I coded the following in my global.asax: > > > Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) > ' Fires when the session is started > > log4net.MDC.Set("sessionID", Session.SessionID) > > m_log.Info("Session " & Session.SessionID & " is starting.") > End Sub > > > > Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) > ' Fires when the session ends > > log4net.MDC.Remove("sessionID") > > m_log.Info("Session " & Session.SessionID & " has ended.") > End Sub > > > > The last approach that came to mind was making the > MDC.Set/Remove calls in my ASPX page in the > Page_Load/Page_Unload events, but that might not work either > given that, once again, multiple sessions are in each thread. > Things would get especially confusing if I turned on logging > in not only my presentation tier, but my business tier, my > data tier and any third party assemblies I'm utilizing. > > So what do I do? How do I solve this problem? CAN I solve > this problem? > > > Thanks for your time... > Leo Hart >