1

How can I change the log level at run time, using semantic logging?

In my Global.asax Application_Startup I have the following code:

        var listener = new ObservableEventListener();
        listener.EnableEvents((EventSource) MyCustomEventSource.Log, EventLevel.Informational, Keywords.All);
        listener.LogToRollingFlatFile(@"logs\events.json", 5000, "MM-dd-yyyy",
            RollFileExistsBehavior.Increment,
            RollInterval.Week,
            new JsonEventTextFormatter(), 4);

How can I change the log level once the my process has started. My support team may want to turn up logging to Verbose during a troubleshooting session, and then turn it back down once they find the problem. They want to do this without stopping or restarting the process.

Paul Fryer
  • 9,268
  • 14
  • 61
  • 93

2 Answers2

3

First, I strongly recommend using the out-of-process host for production. The reasons for this are primarily performance related.

Dynamic configuration is a feature of the out-of-process service. All you need to do is modify the level attribute on the corresponding <eventSource /> and the change should be picked up automatically.

Here's an example configuration.

Christopher Bennage
  • 2,578
  • 2
  • 22
  • 32
1

First step is to not put all the code in Application_Startup, since that only runs when starting the application. 8)

Second step is to take the hardcoded 'EventLevel.Informational' out, and use a property that is read in from a configuration file.

Third step is to register a watch on that configuration file, so that when the file is updated, your code is called, and you can modify the logging level.

I'll add code later when I get a chance, but the steps should give you an idea. Something along these lines... Watching log4net log file with FileSystemWatcher

MSDN: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Community
  • 1
  • 1
MonkeyWrench
  • 1,809
  • 2
  • 24
  • 48