Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nlog with ApplicationInsightsTelemetryWorkerService in .net core 3.1 console app

I'm configuring a .net core 3 console app with application insights and nlog

My code is configured as follows

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--");

In my nlog.config I have

  <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>

In appsettings.json I have

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

In my code I use constructor injection to get the logger and then just

_logger.LogDebug("something");

Yet when I run this I'm not getting anything in application insights. I also notice in my output window I get some logs starting with:

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

There's not much documentation to go on unfortunately. Can anyone point me in the right direction.

Thanks very much.

like image 276
Suemayah Eldursi Avatar asked Dec 19 '19 08:12

Suemayah Eldursi


People also ask

How do I use NLog in Web API?

Implementing Interface and Nlog. NLog needs to have information about the folder to create log files in, what the name of these files will be, and what a minimum level of logging is. Therefore, we need to create one text file in the main project with the name nlog. config and populate it.


2 Answers

Besides peter Bons' answer, here is one important thing you should know:

The message Application Insights Telemetry (unconfigured): ..... means that the AI-key is not configured properly, so you cannot see the data float into appInsights.

Please try add the AI-key in nlog.config, like below:

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

I can repro your issue, if without add AI_Key in nlog.config; But works fine if add AI_Key in nlog.config.

If you still have the issue, please provide a working sample code as well as these nuget packages and versions.

like image 126
Ivan Yang Avatar answered Oct 17 '22 21:10

Ivan Yang


Do mind that setting the loglevel for Application Insights is done at a different level by using a section called ApplicationInsights inside the logging section:

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

So with you current setup you won't get Debug level messages.

like image 25
Peter Bons Avatar answered Oct 17 '22 22:10

Peter Bons