Goal: Forward log entries from NLog to Azure Application Insights. Starting from: https://github.com/Microsoft/ApplicationInsights-dotnet-logging
I created a very basic console application:
TelemetryConfiguration.Active.InstrumentationKey = "my instrumentation key";
Logger logger = LogManager.GetLogger("Example");
logger.Info("Hello World");
Console.ReadLine();
With the following app.config:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>
<targets>
<target type="ApplicationInsightsTarget" name="aiTarget" layout="${longdate} ${level} ${threadid} ${logger} ${message} ${exception:format=ToString}" />
<target name="console" xsi:type="ColoredConsole" layout="${longdate} ${level} ${threadid} ${logger} ${message} ${exception:format=ToString}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="aiTarget" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
</nlog>
</configuration>
But no log messages arrive in Azure Application Insights while the ColoredConsole target works as expected. The messages arrive only when I call TelemetryConfiguration.Active.TelemetryChannel.Flush();
.
- Did I make a configuration mistake?
- Do I need to call .Flush() by myself?