.NET Core In Production – Changing Log Level Temporarily

When running the application in production then the log level is set somewhere between Information and Error. The question is what to do if you or your customer experiences some undesired behavior and the logs with present log level aren't enough to pinpoint the issue.

In this article:

Pawel Gerr is architect consultant at Thinktecture. He focuses on backends with .NET Core and knows Entity Framework inside out.
The first solution that comes to mind is to try to reproduce the issue on a developer’s machine with lower log level like Debug. It may be enough to localize the bug but sometimes it isn’t. Even if you are allowed to restart the app in production with lower log level, the issue may go away … temporarily, i.e. the app still has a bug. Better solution is to change the log level temporarily without restarting the app. First step is to initialize the logger with the IConfiguration. That way the logger changes the level as soon as you change the corresponding configuration (file). In this post I will provide 2 examples, one that is using the ILoggingBuilder of the ASP.NET Core and the other example is using Serilog because it is not tightly coupled to ASP.NET Core (but works very well with it!). Using ILoggingBuilder:
					// the content of appsettings.json
    "Logging": {
        "LogLevel": { "Default": "Information" } 
var config = new ConfigurationBuilder().
    AddJsonFile("appsettings.json", false, true) // reloadOnChange=true

// Setup of ASP.NET Core application
    .ConfigureLogging(builder =>
        builder.AddConfiguration(config); // <= init logger

Using Serilog:

					// the content of appsettings.json
    "Serilog": {
        "MinimumLevel": { "Default": "Information" }
var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", false, true)  // reloadOnChange=true

var serilogConfig = new LoggerConfiguration()
    .ReadFrom.Configuration(config) // <= init logger

In case you are interested in integration with (ASP).NET Core 

					// If having a WebHost
    .ConfigureLogging(builder => 

// If there is no WebHost
var loggerFactory = new LoggerFactory()

At this point the loggers are coupled to IConfiguration or rather to appsettings.json, i.e if you change the level to Debug the app starts emitting debug-messages as well, without restarting it.

This solution has one downside, you need physical access to the appsettings.json. Even if you do, it still would be better to not change the configuration file. What we want is a component that let us set and reset a temporary log level and if this temporary level is not active then the values from appsettings.json should be used. That way you can change the level from the GUI or via an HTTP request against the Web API of your web application.

Luckily, the implementation effort for that feature is pretty low, but that’s for another day…


Current articles, screencasts and interviews by our experts

Don’t miss any content on Angular, .NET Core, Blazor, Azure, and Kubernetes and sign up for our free monthly dev newsletter.

EN Newsletter Anmeldung (#7)
Related Articles
One of the more pragmatic ways to get going on the current AI hype, and to get some value out of it, is by leveraging semantic search. This is, in itself, a relatively simple concept: You have a bunch of documents and want to find the correct one based on a given query. The semantic part now allows you to find the correct document based on the meaning of its contents, in contrast to simply finding words or parts of words in it like we usually do with lexical search. In our last projects, we gathered some experience with search bots, and with this article, I'd love to share our insights with you.
If you previously wanted to integrate view transitions into your Angular application, this was only possible in a very cumbersome way that needed a lot of detailed knowledge about Angular internals. Now, Angular 17 introduced a feature to integrate the View Transition API with the router. In this two-part series, we will look at how to leverage the feature for route transitions and how we could use it for single-page animations.
.NET 8 brings Native AOT to ASP.NET Core, but many frameworks and libraries rely on unbound reflection internally and thus cannot support this scenario yet. This is true for ORMs, too: EF Core and Dapper will only bring full support for Native AOT in later releases. In this post, we will implement a database access layer with Sessions using the Humble Object pattern to get a similar developer experience. We will use Npgsql as a plain ADO.NET provider targeting PostgreSQL.