【问题标题】:Using nlog with ApplicationInsightsTelemetryWorkerService in .net core 3.1 console app在 .net core 3.1 控制台应用程序中将 nlog 与 ApplicationInsightsTelemetryWorkerService 一起使用
【发布时间】:2019-12-19 08:02:17
【问题描述】:

我正在使用应用程序洞察力和 nlog 配置 .net core 3 控制台应用程序

我的代码配置如下

程序.cs

 .ConfigureLogging((hostingContext, logging) =>
 {
     logging.ClearProviders();
     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
     logging.AddNLog(NLog.LogManager.LoadConfiguration("nlog.config").Configuration);
 })
 .ConfigureServices((hostContext, services) =>
 {
      services.SetupConfiguration(hostContext.Configuration);
      services.AddApplicationInsightsTelemetryWorkerService("--AI-Key--");

在我的 nlog.config 我有

  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
  </extensions>
 <!-- the targets to write to -->
  <targets>
    <target name="Console" xsi:type="Console"  layout="${longdate} ${level} ${message}"/>
    <target xsi:type="ApplicationInsightsTarget" name="appInsights" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="Console" />
    <logger name="*" minlevel="Trace" writeTo="appInsights" />
  </rules>

在 appsettings.json 我有

  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "--AI-Key--"
  },

在我的代码中,我使用构造函数注入来获取记录器,然后

_logger.LogDebug("something");

然而,当我运行它时,我在应用程序洞察力中没有得到任何东西。我还注意到在我的输出窗口中,我得到了一些以以下开头的日志:

Application Insights Telemetry (unconfigured): .....

不幸的是,没有太多的文档可以继续。谁能指出我正确的方向。

非常感谢。

【问题讨论】:

  • 您能否提供一个工作示例代码,以及您使用的是哪个 nuget 包?

标签: c# azure azure-application-insights nlog asp.net-core-3.1


【解决方案1】:

除了 peter Bons 的回答之外,还有一件重要的事情你应该知道:

消息Application Insights Telemetry (unconfigured): .....表示AI-key配置不正确,所以你看不到数据浮动到appInsights中。

请尝试在nlog.config 中添加AI-key,如下所示:

  <targets>
    <target name="Console" xsi:type="Console"  layout="${longdate} ${level} ${message}"/>
    <target xsi:type="ApplicationInsightsTarget" name="appInsights">
      <instrumentationKey>your_AI_Key</instrumentationKey>
    </target>
  </targets>

如果没有在 nlog.config 中添加 AI_Key,我可以重现您的问题;但是如果在 nlog.config 中添加 AI_Key 就可以了。

如果您仍有问题,请提供工作示例代码以及这些 nuget 包和版本。

【讨论】:

  • 谢谢你帮我解决了这个问题。你知道动态设置它的方法吗?说明您是否想为每个环境使用不同的密钥。
  • 我设法通过这样做动态设置它:var appInsightsTarget = NLog.LogManager.Configuration.FindTargetByName&lt;ApplicationInsightsTarget&gt;("appInsights"); 然后appInsightsTarget.InstrumentationKey = instrumentationKey;
  • InstrumentationKey 应该可以使用 NLog 布局渲染器。所以你可以查找设置/环境变量/等。
【解决方案2】:

请注意,通过在日志记录部分中使用名为 ApplicationInsights 的部分在不同级别上设置 Application Insights 的日志级别:

"Logging": {
    "LogLevel": {
      "Default": "Debug",
    "ApplicationInsights"
       "LogLevel": {
           "Default": "Debug"
       }
     }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "--AI-Key--"
  }

因此,在您当前的设置下,您不会收到调试级别的消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多