首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为应用程序设置遥测通道Log4Net附录?

如何为应用程序设置遥测通道Log4Net附录?
EN

Stack Overflow用户
提问于 2019-05-20 13:36:51
回答 2查看 3.2K关注 0票数 1

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

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

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

  • 未初始化服务器遥测通道。所以持久存储被关闭了。您需要调用ServerTelemetryChannel.Initialize()。目前,监测工作将继续进行,但如果无法发送遥测数据,则将放弃监测。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-21 07:52:39

更新0603:

我的app.config:

使用visual studio进行调试:

更新:请遵循下面的截图,并尝试找到您发送的信息。如果您仍然找不到信息,请提供详细的代码(删除个人/重要的数据,如仪表键,并提供给我们的nuget软件包和版本,您正在使用)。

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

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

您最好提供设置log4net和app洞察力键的代码。

我在wpf项目中做了一个简单的测试,下面的代码运行良好:

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

代码语言:javascript
复制
//when add the code, it will cause the error you mentioned.
TelemetryConfiguration.Active.TelemetryChannel = new ServerTelemetryChannel();

如果您必须添加一个遥测客户端(配置),并进行适当的配置,log4net和遥测客户端都可以将数据发送到应用程序洞察力。代码如下:

代码语言:javascript
复制
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();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2019-06-03 10:59:33

所以,终于一切正常了。我在此再次提出所需的步骤:

  1. 将NugetPackages: log4net、Microsoft.ApplicationInsights、Microsoft.ApplicationInsights.Log4NetAppender和Microsoft.ApplicationInsights添加到项目中
  2. 在MainWindow.xaml.cs中: 私有静态只读ILog log = LogManager.GetLogger(typeof(MainWindow));公共MainWindow() {MainWindow=“key";log4net.Config.XmlConfigurator.Configure();log.Info("wpf aaaa11111");InitializeComponent();}
  3. 在App.config中:

  1. 完成

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56222102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档