Serilog 不将日志写入文件
Serilog does not write logs to file
我有解决方案,其中有 5 个 .net-core 项目,一个是控制台应用程序,其余是 class 库。在 class 程序的主要功能中,我有:
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();
在我所有 classes 中,当我捕获异常时,我写了:
catch(Exception e)
{
Log.ForContext<ClassName>().Error(e, "Somethign went wrong");
}
在新日志中我只有 "Server starting..." 而已。我应该做什么?当我调试时,我看到我的程序 运行 与 catch
行。现在我不知道该怎么办。也许有人有类似的问题?
编辑
我从这里建模 https://github.com/serilog/serilog/wiki/Getting-Started
您要将 Serilog 添加到您的 LoggerFactory
吗?
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
loggerFactory.AddSerilog();
}
您只应在所有任务完成后调用 Log.CloseAndFlush()
。将它放在 Console.ReadLine()
调用之后应该可以证明这一点。
我有解决方案,其中有 5 个 .net-core 项目,一个是控制台应用程序,其余是 class 库。在 class 程序的主要功能中,我有:
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();
在我所有 classes 中,当我捕获异常时,我写了:
catch(Exception e)
{
Log.ForContext<ClassName>().Error(e, "Somethign went wrong");
}
在新日志中我只有 "Server starting..." 而已。我应该做什么?当我调试时,我看到我的程序 运行 与 catch
行。现在我不知道该怎么办。也许有人有类似的问题?
编辑 我从这里建模 https://github.com/serilog/serilog/wiki/Getting-Started
您要将 Serilog 添加到您的 LoggerFactory
吗?
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
loggerFactory.AddSerilog();
}
您只应在所有任务完成后调用 Log.CloseAndFlush()
。将它放在 Console.ReadLine()
调用之后应该可以证明这一点。