【发布时间】:2021-11-09 21:54:12
【问题描述】:
我目前正在使用在 nlog.config 文件中配置的 NLog 登录到 Application Insights。我没有在任何地方设置IncludeScopes(默认情况下是真的)。
我正在尝试使用范围记录自定义属性。它在登录到文件或控制台时有效,但在登录到 Application Insights customDimensions 时无效。
这就是我记录范围的方式:
using (_logger.BeginScope(new Dictionary<string, object> { ["ActivityId"] = Guid.NewGuid()})
{
_logger.LogInformation("Logging from with scope");
}
这是 nlog.config 文件:
<target name="applicationInsights" xsi:type="ApplicationInsightsTarget" >
<instrumentationKey>8d9f67d5-fe36-45cf-935f-2f87bb240b12</instrumentationKey>
<!-- Only required if not using ApplicationInsights.config -->
<contextproperty name="threadId" layout="${threadid}" />
<contextproperty name="processName" layout="${processname}" />
<!-- Can be repeated with more context -->
</target>
很遗憾,当我在 Application Insights 中查看 customDimensions 时,我没有看到 ActivityId。
我在 Azure 中运行我的控制台应用程序,因此注册了一个工作服务(处理消息),如下所示:
services.AddHostedService<PositionMessageProcessor>()
我需要做什么才能获得日志记录范围以在 Application Insights 中记录我的 ActivityId?
更新
我已经设法通过将 ActivityId 添加为特定的contextProperty 来记录它。我真的不想每次使用不同属性调用 BeginScope(...) 时都必须更新配置文件。
有没有一种通用的方法让它适用于所有范围属性?
【问题讨论】:
标签: c# scope azure-application-insights nlog custom-dimensions