简体   繁体   English

如何关闭 Serilog?

[英]How to turn off Serilog?

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 same code (as a module, not the service itself).我们正在使用 Serilog 将项目记录到带有 Windows 服务的数据库中,并且用户希望能够进行手动运行,因此我们制作了一个按钮(在网页上)来调用相同的代码(作为模块,而不是服务本身)。

When we added in the code to initialize the log so the code will continue adding to the db log table, it also logs all the http traffic after that as well.当我们在代码中添加初始化日志以便代码将继续添加到数据库日志表时,它还会记录此后的所有 http 流量。 So after this code runs we want to 'turn off' the Logger running on the Webserver.因此,在此代码运行后,我们希望“关闭”在 Web 服务器上运行的 Logger。 Is there an easy way to do that?有没有简单的方法来做到这一点?

Log.Logger = new LoggerConfiguration()
       .WriteTo.MSSqlServer(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString,
                "LOGS")
       .CreateLogger();

Log levels can be modified at runtime with LoggingLevelSwitch : 可以使用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. 记录最初将位于Information级别,您可以通过交换机进行更改。

Serilog doesn't define an Off level, but you can approximate it with: Serilog没有定义Off级别,但您可以使用以下方式对其进行近似:

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

...to turn logging off. ...关闭注销

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 . 转换Serilog注销的最简单方法是创建一个没有任何接收器的新记录器。 From Serilog's documentation, Configuration Basics : 从Serilog的文档, 配置基础知识

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

Edit (08/05/2017) 编辑(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. ,其中IsLoggingEnabled只是一个布尔标志。

according to Microsoft documentation for logging if you set the log level to None, all logs will be suppressed.根据 Microsoft 的日志记录文档,如果将日志级别设置为无,则所有日志都将被抑制。

To suppress all logs, specify LogLevel.None.要禁止所有日志,请指定 LogLevel.None。 LogLevel.None has a value of 6, which is higher than LogLevel.Critical (5). LogLevel.None 的值为 6,高于 LogLevel.Critical (5)。

Any logs below the minimum level are not: Passed to the provider.任何低于最低级别的日志都不会:传递给提供者。 Logged or displayed.记录或显示。

you can refer to this documentation under Configure logging section.您可以在配置日志记录部分下参考此文档。 logging for asp.net core 登录 asp.net 核心

for Serilog you can set对于 Serilog,您可以设置

"Serilog": {
"MinimumLevel": {
  "Default": "6"
}}

this configuration will turn off Serilog , I try it at run time and it worked.此配置将关闭Serilog ,我在运行时尝试它并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM