1

I want to use log4net to log browser capabilities in a custom property. What is the best context to be used here?

  • log4net.GlobalContext
  • log4net.ThreadContext
  • log4net.ThreadLogicalContext
  • log4net.Core.LoggingEvent
Community
  • 1
  • 1
VivekDev
  • 20,868
  • 27
  • 132
  • 202

1 Answers1

1

As you can see in the page you linked to, here are the expected differences of all those contexts:

  • The global context is shared by all threads in the current AppDomain. This context is thread safe for use by multiple threads concurrently.
  • The thread context is visible only to the current managed thread.
  • The logical thread context is visible to a logical thread. Logical threads can jump from one managed thread to another. For more details see the .NET API System.Runtime.Remoting.Messaging.CallContext.
  • Each event captures the current contextual state at the time the event is generated. Contextual data can be set on the event itself. This context is only visible to the code generating the event itself.

As you can see the context impacts the scope in which your property lives. Since you want to log browser capabilities you are in a web app, so I suppose multiple threads. If you were to keep properties in a shared context (global) you could risk losing info. I'm not sure about thread contexts since the thread may pick up some other request while waiting on some async data.

In the end the Event context seems the safest and most logical choice since you'll certainly log one event per browser (or per request) and don't need to share this information with the rest of the loggers.

samy
  • 14,832
  • 2
  • 54
  • 82
  • Thanks Samy. Though I did not yet fully understood the concept of the context here, I guess I can now go ahead and use the Event Context as you suggested. – VivekDev Mar 12 '15 at 05:15
  • Context could often be replaced by scope in log4net. If you want a property shared by all loggers, scope/context should be global. A property shared by loggers on the same thread, context should be thread. Properties relevant only to the message you are logging will use event context – samy Mar 12 '15 at 07:30