【问题标题】:Application insight NLog target应用洞察 NLog 目标
【发布时间】:2016-08-08 12:51:01
【问题描述】:

我有一个控制台应用程序,我想从中将自定义事件发送到我的 Application Insight。我想使用 Application Insight NLog 目标 (https://www.nuget.org/packages/Microsoft.ApplicationInsights.NLogTarget/) 但它不起作用。我尝试通过 .config 文件进行设置并尝试手动设置:

    var config = new LoggingConfiguration();
    ConfigurationItemFactory.Default.Targets.RegisterDefinition("ai", typeof(ApplicationInsightsTarget));
    ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
    aiTarget.InstrumentationKey = "my_key";
    aiTarget.Name = "aiTarget";
    LoggingRule rule = new LoggingRule("*", LogLevel.Info, aiTarget);
    config.AddTarget("aiTarget", aiTarget);
    config.LoggingRules.Add(rule);
    LogManager.Configuration = config;

但仍然没有,我在应用程序洞察力中看不到我的异常或事件。有任何想法吗?

【问题讨论】:

    标签: c# nlog azure-application-insights


    【解决方案1】:

    我假设您遵循文档 here(与您实现的非常接近):

    var config = new LoggingConfiguration();
    
    ApplicationInsightsTarget target = new ApplicationInsightsTarget();
    // You need this only if you did not define InstrumentationKey in ApplicationInsights.config or want to use different instrumentation key
    target.InstrumentationKey = "Your_Resource_Key";
    
    LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
    config.LoggingRules.Add(rule);
    
    LogManager.Configuration = config;
    
    Logger logger = LogManager.GetLogger("Example");
    
    logger.Trace("trace log message");
    

    然后,如果有任何东西开箱即用地发送到 dc.services.visualstudio.com,我会与 Fiddler 再次核对,以及响应代码是什么。如果问题确实出在传输而不是收集上,这可能会提供有关问题的线索。

    如果问题正在收集中,您可以通过PerfView and other Diagnostics tools.在本地进行故障排除

    用于收集 AI 轨迹的 PerfView 命令如下所示:

    PerfView.exe /onlyProviders=*Microsoft-ApplicationInsights-Extensibility-Web,*Microsoft-ApplicationInsights-Web,*Microsoft-ApplicationInsights-Core,*Microsoft-ApplicationInsights-Extensibility-DependencyCollector,*Microsoft-ApplicationInsights-Extensibility-Rtia-SharedCore,*Microsoft-ApplicationInsights-Extensibility-WindowsServer,*Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel collect
    

    【讨论】:

      【解决方案2】:

      对于我的控制台应用程序,我在运行时从 App.config 中读取了“INSTRUMENTATIONKEY”。

      所以我首先在 App.config 中添加了“APPINSIGHTS_INSTRUMENTATIONKEY”作为键。

      <appSettings>
          ....
          <add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="your key" />
          ....
      </appSettings>
      

      然后通过添加以下行在 Main 函数中读取并设置此键。

      var key = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"];   
      Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = key;
      

      然后在 Main 函数的末尾或在您的关闭函数中添加一个 thread.sleep,以便有时间将数据发送到 Application Insights。

      System.Threading.Thread.Sleep(70000);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        • 2017-11-13
        • 2019-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多