在.net core 3.1控制台应用程序中将nlog与ApplicationInsightsTelemetryWorkerService一起使用

问题描述 投票:0回答:1

我正在使用应用程序见解和nlog配置.net core 3控制台应用程序>

我的代码配置如下

Program.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): .....

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

非常感谢。

我正在使用应用程序见解和nlog配置.net core 3控制台应用程序,我的代码配置如下Program.cs .ConfigureLogging((hostingContext,logging)=> {logging ....

c# .net-core nlog azure-application-insights asp.net-core-3.1
1个回答
0
投票
请注意,通过在日志记录部分中使用名为ApplicationInsights的部分,可以在不同的级别上设置Application Insights的日志级别:
© www.soinside.com 2019 - 2024. All rights reserved.