Serilog不会将日志写入文件

时间:2017-08-25 06:35:38

标签: c# .net-core serilog

我有解决方案,其中有5个.net核心项目,一个 - 控制台应用程序,其余是类库。在主要功能课程中我有:

float/int

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

        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"); } 行。现在我不知道该怎么做。也许有人有类似的问题?

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

2 个答案:

答案 0 :(得分:2)

您是否将Serilog添加到LoggerFactory

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

答案 1 :(得分:2)

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

相关问题