1

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. 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. Means, Is there a way to turn on/off logging functionality.

In Startup.cs, I have added this code to start the .txt logging:

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.

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.

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??

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • 1
    You can change the log level to something higher https://nblumhardt.com/2014/10/dynamically-changing-the-serilog-level/ – Crowcoder Apr 17 '19 at 09:41
  • Hi, Thanks for the info but this is not what I want. This surely is changing the logging level but I don't want that I want like suppose: My project is publish and a service is there which suddenly due to some reason started failing, now I want the serilog to get activated (by manual activation like by some kind of api calling and changing its logging level) and start logging to Verbose level though it was initially configured to higher level. – Tanmay Taran Apr 26 '19 at 06:00

1 Answers1

0

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
  2. Read the value of the flag from IConfiguration (see how to do so)
  3. Enable logging depending on the flag value (for instance with a if surrounding loggerFactory.AddFile("Logs/log-{Date}.txt");

Hope it helps.

Skrface
  • 1,388
  • 1
  • 12
  • 20
  • Hi @Skrface, Thanks for the reply but what you have suggested is already provided by a method [LoggingLevelSwitch](https://github.com/serilog/serilog/wiki/Writing-Log-Events). This surely is changing the logging level at run time but I don't want that I want like suppose: My project is publish and a service is there which suddenly due to some reason started failing now I want the serilog to get activated (by manual activation like by some kind of api calling and changing its logging level) and start logging to Verbose level though it was initially configured to Information level. – Tanmay Taran Apr 24 '19 at 11:34