How to initialize log4net GlobalContext properties in configuration file

Is there a way to define the global context property in the configuration file if I don't want to set it in the code, for example

log4net.GlobalContext.Properties["AppName"] = "MyCoolApp"; 

?

+4
source share
1 answer

Log4net does not provide this functionality, but you can easily create it yourself:

  • Store several key / value pairs in the configuration file of your choice
  • Read the key value pairs at startup and assign it to the global context

The second part will look something like this:

 Dictionary<string, string> pairs = ReadGlobalContextConfiguration(); foreach (var pair in pairs) { log4net.GlobalContext.Properties[pair.Key] = pair.Value; } 

The first part depends on where you want to save the information. There are many options:

  • Configuration section in /Web.config application
  • Simple xml file
  • Simple text file with key=value entries per line
+2
source

Source: https://habr.com/ru/post/1491392/


All Articles