简体   繁体   English

是否可以打开/关闭.net核心中的日志记录功能,特别是serilog(将数据记录到.txt文件中)

[英]Is there a to turn on/off the logging functionality in .net core specially the serilog(logging data into .txt file)

I am using .net core 2.2 in a sample project and have implemented LoggerFactory and Serilog(for getting logs in .txt file) and they are working fine. 我在示例项目中使用.net core 2.2,并实现了LoggerFactory和Serilog(用于在.txt文件中获取日志),并且它们工作正常。 But is there a way so that I can turn on logging and off it when I don't need it dynamically without actually doing any code changes and then deploying back into server. 但是有没有一种方法可以使我在不需要动态更改而无需实际更改任何代码然后将其重新部署到服务器中时就可以打开和关闭日志记录。

I have made the necessary changes to start the logging functionality and its working pretty good but now I want my system to stop logging errors into the .txt file when I don't want it to log. 我已经进行了必要的更改以启动日志记录功能,并且它的工作情况还不错,但是现在我希望我的系统在我不想记录它时停止将错误记录到.txt文件中。 Means, Is there a way to turn on/off logging functionality. 意思是,有没有办法打开/关闭日志记录功能。

In Startup.cs, I have added this code to start the .txt logging: 在Startup.cs中,我添加了以下代码以启动.txt日志记录:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
    loggerFactory.AddFile("Logs/log-{Date}.txt");
}

In a controller named customer I have created an instance of the ILogger class. 在名为customer的控制器中,我创建了ILogger类的实例。

private ILogger _logger;
public CustomersController(ILogger < CustomersController > logger) {
    _logger = logger;
}

And in this TimeTaken function I have used it to log the time taken for the DB call to fetch all customers. 在这个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);
}

This codes are working fine just want to know if there is a way to turn on/off their functionality on my will. 这些代码很好用,只是想知道是否有办法按我的意愿打开/关闭其功能。 Like calling an api to stop the logging or anything?? 就像调用api来停止日志记录一样?

Configuration file are used to change application settings without the need to rebuild the application. 配置文件用于更改应用程序设置,而无需重建应用程序。

Knowing that, here is the most basic way to achieve what you are looking for : 知道这一点,这是实现所需内容的最基本方法:

  1. Set up a flag in your appSettings.json 在您的appSettings.json中设置一个标志
  2. Read the value of the flag from IConfiguration ( see how to do so ) 从IConfiguration读取标志的值( 请参阅操作
  3. Enable logging depending on the flag value (for instance with a if surrounding loggerFactory.AddFile("Logs/log-{Date}.txt"); 根据标志值启用日志记录(例如, if周围loggerFactory.AddFile("Logs/log-{Date}.txt");

Hope it helps. 希望能帮助到你。

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

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