How do I write logs from within Startup.cs?

前端 未结 10 1362
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 04:16

In order to debug a .NET Core app which is failing on startup, I would like to write logs from within the startup.cs file. I have logging setup within the file that can be u

10条回答
  •  [愿得一人]
    2020-12-01 04:46

    The official solution is currently to setup a local LoggerFactory like this:

        using var loggerFactory = LoggerFactory.Create(builder =>
        {
            builder.SetMinimumLevel(LogLevel.Information);
            builder.AddConsole();
            builder.AddEventSourceLogger();
        });
        var logger = loggerFactory.CreateLogger("Startup");
        logger.LogInformation("Hello World");
    

    See also: https://github.com/dotnet/aspnetcore/issues/9337#issuecomment-539859667

提交回复
热议问题