【问题标题】:Serilog does not write logs to fileSerilog 不会将日志写入文件
【发布时间】:2018-02-03 03:56:32
【问题描述】:

我有 5 个 .net-core 项目的解决方案,一个控制台应用程序和其余的是类库。在主函数的类程序中,我有:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.LiterateConsole()
            .WriteTo.Async(a => a.RollingFile("logs\\myapp-{Date}.txt",
                outputTemplate : "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level} {Type}] {Message}{NewLine}{Exception} {SourceContext}"))
            .CreateLogger();

         Log.Information("Server starting ...");

            //Here I have class which invoke a lot of(6-7) Tasks 
                NewTask task = new NewTask();
                task.Start();
            //And I end my Main function with
         Log.CloseAndFlush();
         Console.ReadLine();

在我的所有课程中,当我捕捉到我写的异常时:

        catch(Exception e)
        {
            Log.ForContext<ClassName>().Error(e, "Somethign went wrong");
        }

在新日志中,我只有“服务器正在启动...”,仅此而已。我应该做什么?当我调试时,我看到我的程序使用catch 运行。现在我不知道该怎么办。也许有人有类似的问题?

编辑 我从这里建模https://github.com/serilog/serilog/wiki/Getting-Started

【问题讨论】:

    标签: c# .net-core serilog


    【解决方案1】:

    您只应在所有任务完成后致电Log.CloseAndFlush()。将它放在Console.ReadLine() 调用之后应该可以证明这一点。

    【讨论】:

    • 天哪,谢谢。我永远不会去想它。在 RedLine 之后,对我来说是疯了,因为 ReadLine 停止了程序。但不知何故它起作用了
    • 太好了,很高兴为您提供帮助。
    【解决方案2】:

    您是否将 Serilog 添加到您的 LoggerFactory

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
    {
        loggerFactory.AddSerilog();
    }
    

    【讨论】:

    • 你知道我们说的是.net-core而不是asp吗?
    • 好点,你能显示更多的代码吗?我刚刚尝试了 Serilog 示例,它运行良好。
    • 我不能 :( 但正如您在我的编辑部分中看到的那样,我粘贴的示例也适用于我,当然是在一个项目的简单解决方案中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多