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 startedlog4net.MDC.Set("sessionID", Session.SessionID)
m_log.Info("Session " & Session.SessionID & " is starting.")
End SubSub
Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session endslog4net.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