![](/img/trans.png)
[英]Turn off mvc request logging in asp.net core 2.2 with serilog
[英]Is there a to turn on/off the logging functionality in .net core specially the serilog(logging data into .txt file)
我在示例项目中使用.net core 2.2,并实现了LoggerFactory和Serilog(用于在.txt文件中获取日志),并且它们工作正常。 但是有没有一种方法可以使我在不需要动态更改而无需实际更改任何代码然后将其重新部署到服务器中时就可以打开和关闭日志记录。
我已经进行了必要的更改以启动日志记录功能,并且它的工作情况还不错,但是现在我希望我的系统在我不想记录它时停止将错误记录到.txt文件中。 意思是,有没有办法打开/关闭日志记录功能。
在Startup.cs中,我添加了以下代码以启动.txt日志记录:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
loggerFactory.AddFile("Logs/log-{Date}.txt");
}
在名为customer的控制器中,我创建了ILogger类的实例。
private ILogger _logger;
public CustomersController(ILogger < CustomersController > logger) {
_logger = logger;
}
在这个TimeTaken函数中,我使用它记录了数据库调用获取所有客户所花费的时间。
public JsonResult TimeTaken() {
_logger.LogInformation("Controller Called");
Stopwatch sw = new Stopwatch();
sw.Start();
var customers = new List < Customers > ();
if (!_cache.TryGetValue("CustomersList", out customers)) {
if (customers == null) {
customers = _context.Customers.ToList();
}
var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(5));
_cache.Set("CustomersList", customers, cacheEntryOptions);
}
sw.Stop();
var b = sw.ElapsedMilliseconds;
_logger.LogInformation("Data execution took {0} miliseconds", b);
return Json(b);
}
这些代码很好用,只是想知道是否有办法按我的意愿打开/关闭其功能。 就像调用api来停止日志记录一样?
配置文件用于更改应用程序设置,而无需重建应用程序。
知道这一点,这是实现所需内容的最基本方法:
if
周围loggerFactory.AddFile("Logs/log-{Date}.txt");
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.