0
{
"Logging":{
  "LogLevel":{
    "Default": "Warning",
    "System.Net.Http.HttpClient.SomeClassLibraryName": "Error"
   }
  }
}

If I add the above code in my app settings then the logs for above-provided namespace only get written for the error and it works.

But when I try to achieve the same thing using the logging configuration for Serilog in .NET 6 WEB API it does not work. This is the method of which I am calling for setting the log configuration :

public static void LoggingConfiguration()
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .MinimumLevel.Override("System.Net.Http.HttpClient.SomeClassLibraryName", LogEventLevel.Error)
        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
        .MinimumLevel.Override(SomeClassLibraryName, LogEventLevel.Information)
        .Enrich.FromLogContext()
        .WriteTo.Async(c => c.File("Logs/Log.txt", rollingInterval: RollingInterval.Day))
        .WriteTo.Async(c => c.Console())
        .CreateLogger();
}

I have tested this code for Microsoft and by changing the minimum level from information to warning and error, it works for them but is not working for a specific case.

I have explored the following options so far but did not find any satisfactory results.

https://github.com/serilog/serilog/wiki/Writing-Log-Events#source-contexts

https://github.com/serilog/serilog/issues/1390

https://github.com/serilog/serilog-extensions-logging/issues/177

6
  • 1
    And where do you call this LoggingConfiguration? I would expect it requires registration via options or smth. Like here: learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/… Commented Aug 17, 2024 at 17:44
  • The method is being called in the Program.cs and its working fine, I hv tested some scenerios Commented Aug 17, 2024 at 17:47
  • And what would be the scenarios in which it does not work? Commented Aug 17, 2024 at 17:49
  • "System.Net.Http.HttpClient.SomeClassLibraryName": "Error", at the same time if I put this in the app settings it works perfectly, but due to constraint I want to control it via my custom method not app settings Commented Aug 17, 2024 at 17:49
  • 1
    Add it as an answer @Pawel Commented Aug 17, 2024 at 18:04

2 Answers 2

1

You should rely on built-in registration like here.

Try moving the registration to builder like so:

var builder = WebApplication.CreateBuilder();
builder.Logging.SetMinimumLevel(LogLevel.Warning);

or

var builder = WebApplication.CreateBuilder();
builder.Logging.AddFilter("System.Net.Http.HttpClient.ClassLibraryName", LogLevel.None);
Sign up to request clarification or add additional context in comments.

Comments

1

Serilog does not use the default Logging section, by default it uses Serilog one (with MinimumLevel for log level configs, see for example How to prevent logging all of my Entity Framework SQL queries? or the docs), it is used by the build-in logger, so either register Serilog (from the Serilog.AspNetCore docs):

LoggingConfiguration(); // call your Serilog setup
// install the Serilog.AspNetCore package
builder.Services.AddSerilog();

or use the build-in logger APIs (docs):

builder.Logging.SetMinimumLevel(LogLevel.Warning);
builder.Logging.AddFilter("System.Net.Http.HttpClient.ClassLibraryName", LogLevel.None);

Though personally I would recommend to stick with the config approach as far more flexible one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.