gpt4 book ai didi

c# - 如何为 Application Insights Log4Net Appender 设置遥测 channel ?

转载 作者:行者123 更新时间:2023-12-03 01:03:09 25 4
gpt4 key购买 nike

我将这些 Nuget 包添加到我的 WPF 应用程序中:

  • Microsoft.ApplicationInsights.Log4NetAppender
  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel

记录器正在记录一个文件,该文件有效。但没有数据传输到 Azure。我收到此错误:

  • AI:服务器遥测 channel 未初始化。所以持久存储被关闭。您需要调用ServerTelemetryChannel.Initialize()。目前监控将继续,但如果无法发送遥测数据,监控将会终止。

我的问题:我应该在哪里(在代码中)初始化遥测 channel ?为什么我必须这样做?如果我必须添加遥测客户端(带有配置),那么附加程序有什么用?

最佳答案

更新 0603:

我的应用程序配置:

enter image description here

使用 Visual Studio 进行调试:

enter image description here

更新:请按照下面的屏幕截图,尝试查找您发送的信息。如果您仍然找不到该信息,请提供您详细的代码(删除个人/重要数据,如仪器 key ,并向我们提供您正在使用的 nuget 包和版本)。

1.点击概览页面的搜索按钮:

enter image description here

2.在搜索屏幕中,正确设置本地时间和事件类型,然后尝试搜索消息:

enter image description here

<小时/>

您最好提供设置 log4net 和 app Insights key 的代码。

我用wpf项目做了一个简单的测试,下面的代码工作正常:

public partial class MainWindow : Window
{

private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
public MainWindow()
{
TelemetryConfiguration.Active.InstrumentationKey = "the key";
log4net.Config.XmlConfigurator.Configure();

log.Info("wpf aaaa11111");


InitializeComponent();
}
}

您收到错误“AI:服务器遥测 channel 未初始化”,可能是由于某些不正确的配置,例如在上面的工作代码中使用以下代码:

//when add the code, it will cause the error you mentioned.
TelemetryConfiguration.Active.TelemetryChannel = new ServerTelemetryChannel();

如果您必须添加遥测客户端(带配置),并且通过正确的配置,log4net 和遥测客户端都可以将数据发送到应用程序洞察。代码如下:

public partial class MainWindow : Window
{
private readonly TelemetryClient telemetryClient;
private static readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
public MainWindow()
{
//configure the key here for log4net
TelemetryConfiguration.Active.InstrumentationKey = "the key";
log4net.Config.XmlConfigurator.Configure();

var config = new TelemetryConfiguration();

//configure the key here for telemetry client
config.InstrumentationKey = "the key";
telemetryClient = new TelemetryClient(config);

log.Info("wpf aaaa333");
log.Info(TelemetryConfiguration.Active.TelemetryChannel.ToString());

telemetryClient.TrackTrace("it is going to start!");

InitializeComponent();
}
}

关于c# - 如何为 Application Insights Log4Net Appender 设置遥测 channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com