【发布时间】:2018-08-14 03:55:42
【问题描述】:
我有一个启用了内置控制台日志记录的 ASP.NET Core 2.0 应用程序。这是 WebHost 的创建:
var webHost = WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5002")
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
webHost.Run();
发送 HTTP POST 请求时,会生成以下日志消息并显示在控制台标准输出中:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 POST http://localhost:5002/api/sample application/json 2144
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method MyApp.Controllers.SampleController.Post (Model.Solve) with arguments (MyApp.Request.MyRequest) - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor[1]
Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action MyApp.Controllers.SampleController.Post (MyApp) in 1201.9411ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 1446.1907ms 200 application/json; charset=utf-8
作为进入生产的要求,我需要所有日志条目都包含时间戳:现在,我知道我可以在调用 Log() 方法时自己提供时间戳,如 this open issue on the GitHub ASP.NET Logging repository 中所述,但是我应该怎么做对 WebHost 直接生成的日志消息执行此操作(如上所示)? 有没有办法像this StackOverflow answer 中提出的解决方案那样,在无需重写完整的自定义 Logger 的情况下添加时间戳?
谢谢。
【问题讨论】:
标签: c# logging asp.net-core .net-core kestrel-http-server