C# 如何为Application Insights NLog目标设置上下文
我在WPF应用程序和NLog中使用Microsoft Application Insights记录异常和调试信息。因此,我将Application Insights NLog Target添加到应用程序中。但此NLog目标记录的所有事件不包含Azure portal上的上下文数据 使用遥测客户端记录的其他事件包含这些数据C# 如何为Application Insights NLog目标设置上下文,c#,azure,nlog,azure-application-insights,C#,Azure,Nlog,Azure Application Insights,我在WPF应用程序和NLog中使用Microsoft Application Insights记录异常和调试信息。因此,我将Application Insights NLog Target添加到应用程序中。但此NLog目标记录的所有事件不包含Azure portal上的上下文数据 使用遥测客户端记录的其他事件包含这些数据 var telemetryClient = new TelemetryClient(); telemetryClient.InstrumentationKey
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "xxx";
telemetryClient.Context.User.Id = Environment.UserName;
telemetryClient.Context.Session.Id = Guid.NewGuid().ToString();
//var config = new LoggingConfiguration();
ConfigurationItemFactory.Default.Targets.RegisterDefinition(
"ai",
typeof(ApplicationInsightsTarget)
);
ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
aiTarget.InstrumentationKey = "xxx";
aiTarget.Name = "ai";
LogManager.Configuration.AddTarget("ai", aiTarget);
LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Info, aiTarget);
LogManager.Configuration.Reload();
LogManager.ReconfigExistingLoggers();
我的问题是-是否可以将此上下文数据也用于NLog目标记录的事件?或者,如何也为NLog设置上下文数据?您可以使用
遥测初始化器来确保创建的所有项都获得所需的上下文
修改的示例来自:
您还需要注册初始值设定项,等等,上面的帖子中有相关说明,有几种方法可以做到这一点
(但不要使用上下文初始值设定项
,它的名称可能与您认为的名称不符),并且:
namespace ApmTips.Tools
{
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Diagnostics.Tracing;
using System.Diagnostics;
public class ExtendedIDTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
{
telemetry.Context.[some field] = [some value];
}
}
}