【问题标题】:How to set Telemetry Channel for Application Insights Log4Net Appender?如何为 Application Insights Log4Net Appender 设置遥测通道?
【发布时间】:2019-05-20 13:36:51
【问题描述】:

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

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

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

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

我的问题:我应该在哪里(在代码中)初始化遥测通道?为什么我必须这样做?如果我必须添加遥测客户端(带配置),appender 是什么?

【问题讨论】:

  • 如果答案有效,请帮助将其标记为答案。谢谢。

标签: c# wpf azure log4net azure-application-insights


【解决方案1】:

更新 0603:

我的 app.config:

使用 Visual Studio 进行调试:

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

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

2.在搜索画面中,正确设置Local time和Event类型,然后尝试搜索消息:


您最好提供设置 log4net 和应用洞察密钥的代码。

我用 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:服务器遥测通道未初始化”,可能是由于一些不正确的配置,例如在上面的工作代码中使用以下代码:

//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();
    }
}

【讨论】:

  • telemetryClient.TrackTrace() 正在工作,就像以前一样。但我无法从 log.Info 在 Azure 门户中找到任何数据。发现还有另一个主要问题:我首先必须弄清楚在 Azure 门户中如何以及在何处查看 logger.info、.error、.debug 等。我会再联系你
  • 我用谷歌搜索了整个互联网,但不知道如何在 azure 门户上查看 log.Info(如果发送成功)。请帮忙!
  • @Houve,请参阅更新部分。如果您仍有问题,请提供我在更新部分中提到的更多信息。
  • 我创建了一个新的测试 wpf 项目,并将上面第一次提到的代码添加到 MainWindow.xaml.cs,但它仍然不起作用。作为 NugetPackages,我使用 log4net v2.0.8、Microsoft.ApplicationInsights v2.10.0 和 Microsoft.ApplicationInsights.Lof4NetAppender v2.10.0
  • @Houve,你能告诉我测试 wpf 项目的代码吗?
【解决方案2】:

所以,最后一切正常。我在这里再次提供所需的步骤:

  1. 添加 NugetPackages: log4net、Microsoft.ApplicationInsights、Microsoft.ApplicationInsights.Log4NetAppender 和 Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel 到项目

  2. 在 MainWindow.xaml.cs 中:

    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();
    }
    }    
    
  3. 在 App.config 中:

  4. 完成

非常感谢@Ivan Yang 的解决方案和他的时间帮助我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 2017-02-19
    相关资源
    最近更新 更多