.Net Core 3: Serilog don't log custom logs

≡放荡痞女 提交于 2020-01-06 15:22:05

问题


I have a web .NET Core 3 application using Serilog. If I filter the technical logs from MS namespaces, all logs disapear, event custom one.

What I did

I configure Serilog in Main.cs like this:

public static int Main(string[] args)
{
    var currentEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    var configuration = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
       .AddJsonFile($"appsettings.{currentEnv}.json", true)
       .AddEnvironmentVariables()
       .Build();

    Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

    try
    {
        CreateWebHostBuilder(args).Build().Run();
        return 0;
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "Host terminated unexpectedly");
        return 1;
    }
    finally
    {
        Log.CloseAndFlush();
    }
}


public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseSerilog((context, configuration) => 
        configuration.ReadFrom.Configuration(context.Configuration));

In the appsettings.Development.Json:

"Serilog": {
    "MinimumLevel": {
        "Default": "Debug",
        "Override": {
            //"Microsoft": "Error"
        }
    },
    "WriteTo": [
        {
            "Name": "Async",
            "Args": {
                "configure": [
                    {
                        "Name": "File",
                        "Args": {
                            "path": "C:\\temps\\LOGS\\AppRefTagHelper-log.txt",
                            "rollingInterval": "Day",
                            "retainedFileCountLimit": 7,
                            "buffered": true,
                            "outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}"
                        }
                    }
                ]
            }
        }
    ]
}

My issue

I run:

Log.LogInformation("TEST");

This works fine, the log file is filled as expected, I can see my custom logs.

If I uncomment the overrides, the logs file stay empty, I Don't even have my custom logs whatever the level.

I am using only stable versions of Serilog Nugets.

What I need

Having my custom logs even if I Don't log technical one.

来源:https://stackoverflow.com/questions/58153918/net-core-3-serilog-dont-log-custom-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!