3

I've read in a few places that the log4net threadContext isn't safe to use in an asp.net scenario. Here are a few articles:

Log4Net, ThreadContext, and Global.asax
http://piers7.blogspot.com/2005/12/log4net-context-problems-with-aspnet.html

So my scenario is I want to use the threadContext in a WCF layer hosted within IIS. Is the threadcontext going to be ok? I'm thinking the concurrency mode of "single" would make this ok.

Community
  • 1
  • 1
Paul Lemke
  • 5,494
  • 3
  • 47
  • 66
  • Any reason to use log4net instead of the MS Enterprise Library Logging block? That works great with asp.net / IIS hosted WCF services, and is every bit as configurable. – StingyJack Feb 08 '10 at 17:59

3 Answers3

1

As Log4net is thread safe I would not expect a problem.

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • 1
    But will WCF execute a request in a single thread correctly? The problem with asp.net was that in some spots a request might jump to different threads so the threadContext in log4net would be useless. – Paul Lemke Feb 01 '10 at 13:16
  • 1
    @lemkept in most cases by the time it gets to your code WCF will be running the request on a single thread, as the normal case is for WCF to only call one of your methods to process the request. However if you do anything with the request pipeline, I don't know what WCF will do with threading. – Ian Ringrose Feb 01 '10 at 15:18
  • @PaulLemke this is correct and matches by observations from an application I am working on. Therefore this may not be the correct answer. – Remotec May 10 '16 at 10:54
0

I have used log4net thread context to log the the identity of the user who encountered the error in both asp.net applications and wcf services. I have not run into any issues. This is not a definitive answer but you can always decouple your service from the logging framework and test log4net and the threadcontext. if you encounter any issues remove your thread context logic or plug in a new logging framework with minimal impact to your service logic.

Athens Holloway
  • 2,183
  • 3
  • 17
  • 26
0

I have setup a WCF service, hosted in IIS6, that uses log4net with no issues, regardless of the concurrency mode. Using the threadContext should not be a problem.

The only caveat is that if you load the log4net setting from an external file into the WCF layer, you may have to restart it in IIS to pick up the changes.

Jason
  • 2,503
  • 3
  • 38
  • 46