我正在 WCF 服务应用程序中实现 Serilog。在我的 Global.asax 中,我试图设置全局记录器以打印日志的时间、线程 ID、可能记录的任何异常以及事件中包含的 json 格式的其他属性。
我的日志设置:
Log.Logger = new LoggerConfiguration()
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.Enrich.WithExceptionDetails()
.Enrich.FromLogContext()
.WriteTo.File(
path: "\\LogFiles\\Gateway\\gateway-properties-.log",
rollingInterval: RollingInterval.Day,
outputTemplate: "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception} {Properties:j}")
.CreateLogger();
Log.Information("Initilizing Application");
当我查看为片段中的信息事件创建的日志时,它看起来像这样
16:28 [Information] (1) Initilizing Application
{}
当我尝试记录异常时:
throw new Exception("This is a forced exception");
...
catch (Exception ex)
{
Log.Fatal("Application could not be started", ex);
}
我在日志中看到了这一点:
16:28 [Fatal] (1) Application could not be started
{}
我期待看到日志项中包含的异常消息和堆栈跟踪。
在另一个位置,我正在尝试记录请求过程的成功:
Log.Information("Transaction {0} successfully processed", request.TransactionId, request);
我看到了:
16:40 [Information] (8) Transaction "the correct transaction ID" successfully processed
{}
显然我设置不正确或没有正确记录事情。
具体问题
- 问题 1:我的配置中是否有任何内容与我想要的输出冲突?
- 问题 2:Serilog 是否会自动包含属性的大括号,即使它们不包含在日志事件中?(日志后的空 {})
- 问题 3:当我提供属性时,为什么它们被包含在插值字段中,而不是在日志的属性部分?
- 问题 4:我是否需要进一步配置以包含异常的详细信息?