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