'Why does not Serilog selflog work in this simple windows service?

I am unable to get Serilog selflog working in my .NET Core 6.0 C# Windows Service. This is how the Main looks like :

public static void Main(string[] args)
        {
            Serilog.Debugging.SelfLog.Enable(msg => System.Diagnostics.Debug.WriteLine($"Seri-serlflog : {msg}"));

            Log.Logger = new LoggerConfiguration().DefaultLoggerSetup<Program>();
            var serviceName = "MyService";

            var configurationBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            var appSettings = configurationBuilder.Get<AppSettings>();

            try
            {
                Log.Information("{@serviceName} microservice starting up.", serviceName);
                
                var built = Host.CreateDefaultBuilder(args)
                    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration))
                    .ConfigureServices((hostContext, services) =>
                    {
                        services
                            .AddHostedService<MyApp.Cloud.MyService.BusinessLogicLayer.MyService>()
                            .Configure<MySettings>(configurationBuilder.GetSection("MQSettings"))
                            .AddAutoMapper(typeof(ConnectorToCloudTreatment))
                            .AddTransi...
                    }).Build();
                Log.Information("{@serviceName} all built.", serviceName);
                built.Run();
                Log.Information("{@serviceName} microservice closing down.", serviceName);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Unhanled exception in {@serviceName}.", serviceName);
            }
            finally
            {
                Log.Information("{@serviceName} Cleaning up.", serviceName);
                Log.CloseAndFlush();
                Log.Information("{@serviceName} Application Exit.", serviceName);
            }
        }
    }

The serilog part of application.json looks like this :

  "serilog": {
    "Using": [ "Serilog.Sinks.MSSqlServer", "Serilog.Sinks.Email" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss.fff} [{Level}] {SourceContext} {Message}{NewLine}{Exception}",
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "/Logs/logs.txt",
          "outputTemplate": "{Timestamp:G} {SourceContext} [{Level}] {Message}{NewLine:1}{Exception:1}",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
          "fileSizeLimitBytes": 1000000,
          "rollOnFileSizeLimit": "true",
          "shared": "true",
          "flushToDiskInterval": 3
        }
      }
    ]
  }

I do a lot of logging in this application and it works fine but there is a couple of problems and to find why I need to use the selflog but I do not get any information at all to the output > debug?

Edit : Seems like this may have something to do with the injection. In program.cs I use the static Log but in all other classes I use injected typed loggers. But regardless I should get a couple of $"Seri-serlflog : {msg}" rows in the output?



Solution 1:[1]

If you're trying to get debug level logging, you'll have to loosen your log level:

"MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Debug",
        "System": "Debug"
      }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason Triplett