如何关闭 Serilog? [英] How to turn off Serilog?

查看:64
本文介绍了如何关闭 Serilog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Serilog 将项目记录到带有 Windows 服务的数据库中,并且用户希望能够手动运行,因此我们制作了一个按钮(在网页上)来调用相同的代码(作为一个模块,而不是服务本身).

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).

当我们在代码中添加初始化日志以便代码将继续添加到数据库日志表时,它还会记录此后的所有 http 流量.因此,在此代码运行后,我们希望关闭"在 Web 服务器上运行的 Logger.有没有简单的方法可以做到这一点?

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. So after this code runs we want to 'turn off' the Logger running on the Webserver. Is there an easy way to do that?

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

推荐答案

可以在运行时使用 LoggingLevelSwitch 修改日志级别:

Log levels can be modified at runtime with LoggingLevelSwitch:

var ls = new LoggingLevelSwitch();

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

日志记录最初将在 Information 级别,您可以通过开关进行更改.

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

Serilog 没有定义 Off 级别,但您可以使用以下方法对其进行近似:

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

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

...关闭注销.

这篇关于如何关闭 Serilog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆