【发布时间】:2022-04-01 18:39:14
【问题描述】:
我有一个根据这篇博文https://blog.datalust.co/smart-logging-middleware-for-asp-net-core/ 实现的 serilog 中间件类
如果我想多次使用 LogContext.PushProperty 在我的日志中推送各种信息,我只需将以下代码放入我的 Invoke 方法中:
LogContext.PushProperty("Address", httpContext.Connection.RemoteIpAddress);
LogContext.PushProperty("Username", httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : null);
LogContext.PushProperty 的文档只显示了添加一个属性并说要使用 using 块还是我需要执行以下操作:
using (LogContext.PushProperty("Address",
httpContext.Connection.RemoteIpAddress))
using (LogContext.PushProperty("Username", httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : null))
{ //rest of invoke method here }
【问题讨论】:
-
您的使用语法是正确的。
标签: asp.net-core serilog