How to turn off Serilog?

前端 未结 2 726
忘掉有多难
忘掉有多难 2021-02-09 01:50

We are using Serilog to log items into a db with a Windows service, and the users wanted to be able to do a manual run, so we made a button (on a web page) to make a call to the

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 02:08

    Log levels can be modified at runtime with 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 via the switch.

    Serilog doesn't define an Off level, but you can approximate it with:

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

    ...to turn logging off.

提交回复
热议问题