asp.net-coreloggingsyslogsystemd-journaldasp.net-core-9.0

How to write message with warning priority to Debian journal


I have an ASP.NET Core 9 application controller that logs messages with warning level in Debian 12:

public class ContextLessLogger(ILogger syslog)
{
    public Log() 
    {
        syslog.Log(LogLevel.Warning, "test");
    }
}

Filtering messages by warning priority produces no results:

 journalctl -t MyApp -n 100 -p warning
-- No entries --

If priority filter is removed, entries are shown:

journalctl -t MyApp -n 100

produces

veebr 09 14:33:04 uvn-xxxxx.eu MyApp[3659254]: warn: Service.ContextLessLogger[0]
veebr 09 14:33:04 uvn-xxxxx.eu MyApp[3659254]:       test   

It looks like .NET 9 does not write warning log level to Linux log journal. How to fix this so that messages can filtered by warning priority?

Using journal based logging which is used by default in Debian 12.


Solution

  • Add builder.Host.UseSystemd(); and I can write warning log level to Linux log journal.

    Test Result in my side.

    enter image description here

    enter image description here

    Add builder.Host.UseSystemd(); in Program.cs, here is my sample code.

    using Microsoft.Extensions.Hosting.Systemd;
    
    var builder = WebApplication.CreateBuilder(args);
    
    // Add services to the container.
    builder.Services.AddControllersWithViews();
    
    builder.Host.UseSystemd();
    
    var app = builder.Build();
    ...