How to turn off Serilog?

前端 未结 2 716
忘掉有多难
忘掉有多难 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:01

    This question is a couple of years old, but I happen to be facing a similar problem right now.

    The easiest way to turn Serilog's logging off is by creating a new logger without any sinks. From Serilog's documentation, Configuration Basics:

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

    Edit (08/05/2017)

    The solution above is not correct. After some more research, I've found that this can be done by setting a filter when configuring the logger:

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

    , where IsLoggingEnabled is just a boolean flag.

提交回复
热议问题