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

巧了我就是萌 提交于 2019-12-24 01:16:00

问题


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?

回答1:


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.



来源:https://stackoverflow.com/questions/49514679/no-log-entries-in-azure-application-insights-using-microsoft-applicationinsights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!