How to disable Serilog?

We use Serilog to register elements in db using the Windows service, and users wanted to be able to manually start, so we made a button (on the web page) to call the same code (as a module, not the service itself).

When we added db to the initialization code so that the code continues to add db to the log table, it also logs all http traffic after that. Therefore, after running this code, we want to "turn off" Logger running on a web server. Is there an easy way to do this?

Log.Logger = new LoggerConfiguration() .WriteTo.MSSqlServer(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString, "LOGS") .CreateLogger(); 
+6
source share
2 answers

Log levels can be changed at runtime using LoggingLevelSwitch :

 var ls = new LoggingLevelSwitch(); Log.Logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(ls) .WriteTo.MSSqlServer(...) .CreateLogger(); 

Logging will initially be at the Information level, you can change this using the switch.

Serilog does not detect the Off level, but you can approximate it with:

 ls.MinimumLevel = ((LogEventLevel) 1 + (int) LogEventLevel.Fatal); 

... to disable logging.

+10
source

This question has been a couple of years, but I ran into a similar problem right now.

The easiest way to disable Serilog is to create a new log without any receivers . From Serilog documentation Basics of configuration :

 Log.Logger = new LoggerConfiguration().CreateLogger(); Log.Information("No one listens to me!"); 

Edit (05/05/2017)

The solution above is incorrect. After several studies, I found that this can be done by setting a filter when setting up the registrar:

 Log.Logger = new LoggerConfiguration() .Filter.ByExcluding(_ => !IsLoggingEnabled) .WriteTo.MSSqlServer(...) .CreateLogger(); 

where IsLoggingEnabled is just a boolean flag.

+5
source

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


All Articles