I currently write all log4net events to a database, and it seems to work just fine. To capture the logged in user account I use this piece of code:
HttpConte
According to Log4Net official API docs, MDC is deprecated:
The MDC is deprecated and has been replaced by the Properties. The current MDC implementation forwards to the ThreadContext.Properties.
Other than that MDC.Set only accept strings as values, so the last solution from @wageoghe cannot work (the one that uses HttpContextUserNameProvider)
My solution has been to use HttpContextUserNameProvider with log4net.GlobalContext, also suggested in official API docs:
Add this immediatley after log4net initialization (for example in Global.Application_Start)
log4net.GlobalContext.Properties["user"] = new HttpContextUserNameProvider();
Add this class
public class HttpContextUserNameProvider
{
public override string ToString()
{
HttpContext context = HttpContext.Current;
if (context != null && context.User != null && context.User.Identity.IsAuthenticated)
{
return context.User.Identity.Name;
}
return "";
}
}
Modify log4net configuration by adding the "user" property value, for example: