小编Bil*_*ll 的帖子

serilog 异步接收器不会在关机时将缓冲区刷新到磁盘

将 serilog 异步接收器与文件接收器(并且缓冲 = 真)一起使用时,日志条目在关闭时会丢失。我猜这是缓冲区没有刷新到磁盘,即使它被调用。我不确定为什么会发生这种情况,但我很确定这是异步接收器。如果我从配置中删除它并单独使用文件接收器,日志会按预期刷新到磁盘。

我有一个相当简单的设置。

程序.cs:

 public class Program
{
    public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();

    public static void Main(string[] args)
    {

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

        try
        {
            Log.Information("Starting the web host");

            CreateWebHostBuilder(args).Build().Run();

            Log.Information("Shutting down the web host");

        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");

        }
        finally
        {
            Log.Information("Closing Log");

            Log.CloseAndFlush();
        }



    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) …
Run Code Online (Sandbox Code Playgroud)

c# serilog asp.net-core

7
推荐指数
1
解决办法
2639
查看次数

标签 统计

asp.net-core ×1

c# ×1

serilog ×1