This is probably not possible, but I'm wondering if there's a way to make it work.
I have a large ASP.NET MVC43 application that is already instrumented with logging statements.
Now we need to include a "Company Name" value from the Session object in each log entry. Is it possible to configure log4net to read Session data and include it in the log entry? Or some way to force it to...?
Thanks for any ideas.
[Edit] This question helped a lot: How can I include SessionID in log files using log4net in ASP.NET?
I ended up with this as my solution:
In Global.asax.cs:
// After the session is acquired, push the organization code into log4net's thread context, in case it has to log anything.
protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
if (Context.Handler is IRequiresSessionState && Session != null && Session[Constants.EMPLOYEE_DETAILS] != null)
log4net.ThreadContext.Properties["Company"] = ((EmployeeDetails)Session[Constants.EMPLOYEE_DETAILS]).Company;
}
In log4net configuration:
<parameter>
<parameterName value="@Company"/>
<dbType value="String"/>
<size value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Company}" />
</layout>
</parameter>
Works great - I get a company name in my log output now. Thanks for the help, everyone.