简体   繁体   中英

No log entries in Azure Application Insights using Microsoft.ApplicationInsights.NLogTarget

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?

Telemetry items are buffered (for 30 seconds by default, see the source code ) before the client send the data to the portal. So you have to keep the console open for at least that amount of time or call Flush() manually or set the developer mode to true:

TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;

EDIT There are multiple channels that can be used, like InMemoryChannel or ServerTelemetryChannel . They all have a default buffer interval of 30 seconds.

The InMemoryChannel for example has a public property TimeSpan SendingInterval so if you cast the TelemetryConfiguration.Active.TelemetryChannel to the actual implementation you should be able to change the buffer interval.

See ServerTelemetryChannel.MaxTelemetryBufferDelay and InMemoryChannel.SendingInterval .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM