我想保存Activity.Current?.Id ?? HttpContext.TraceIdentifier
进入数据库,因为这是用户在默认错误视图中看到的请求 ID。但如何呢?HttpContext在startup.cs中不可用,我尝试访问HttpContext布局渲染器没有成功。
HomeController 中的错误方法
public IActionResult Error()
{
var model = new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier };
return View(model);
}
我尝试过布局渲染器
namespace Copacking.Web.Utils
{
[LayoutRenderer("requestid")]
public class NLogRequestIdLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(Activity.Current?.Id ?? HttpContext.TraceIdentifier);
}
}
}
但我收到错误HttpContext,因为需要对象引用。
我该如何解决它?在Nlog配置文件 file ${aspnet-traceidentifier}正在工作但是${活动ID}变量为空:/
您可以执行以下操作https://www.nuget.org/packages/NLog.Web.AspNetCore https://www.nuget.org/packages/NLog.Web.AspNetCore:
layout="${activityid:whenEmpty=${mdlc:item=RequestId:whenEmpty=${aspnet-TraceIdentifier}}}"
请记住在 NLog.config 中包含以下内容:
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)