1

I have an ASP.NET website and I am doing logging with log4net. As logging requirements change often, I want to enable PMs to modify the information that gets logged and also to be able to log extra information for debugging in production (i.e without having to recompile)

My plan is to expose certain values to log4net, for example the GET / POST params from context. To log such a param, the user would just go to the log4net config and do something like %message{userId}

I have found a way to do it using a property bag on log4net.ThreadContext but I am not sure if this doesn't have side-effects, i.e. the values are persisted for too long.

Another way is to use a forwarding appender, and inject the extra values whenever the logger gets called, but I haven't been able to implement this, there are not enough examples out there.

Any ideas?

Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74

2 Answers2

1

You should have a look at NDC (Nested Diagnostic Context) in log4net. This will provide you with to possibility to add information logging messages like setting up a stack:

using(log4net.NDC.Push("My extra info")){

} 

All logmessages within the NDC scope will have the inner_context available (pattern layout format):

[%ndc] - %message%newline
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Peter
  • 27,590
  • 8
  • 64
  • 84
1

Look at my answer to this question. The linked question and answer are specifically about adding the username from HttpContext.Current.User to the logfile, but it should be pretty easy to make a more generic solution that might fit your needs.

It might provide you with some ideas of how to incorporate information from the HttpContext into your log4net logging. There are other good ideas in that thread as well.

Community
  • 1
  • 1
wageoghe
  • 27,390
  • 13
  • 88
  • 116