C# 使用Microsoft.ApplicationInsights.NLogTarget的Azure Application Insights中没有日志条目
目标:将日志条目从NLog转发到Azure Application Insights。从: 我创建了一个非常基本的控制台应用程序:C# 使用Microsoft.ApplicationInsights.NLogTarget的Azure Application Insights中没有日志条目,c#,.net,azure,azure-application-insights,nlog,C#,.net,Azure,Azure Application Insights,Nlog,目标:将日志条目从NLog转发到Azure Application Insights。从: 我创建了一个非常基本的控制台应用程序: TelemetryConfiguration.Active.InstrumentationKey = "my instrumentation key"; Logger logger = LogManager.GetLogger("Example"); logger.Info("Hello World"); Console.ReadLine(); 使用以下app
TelemetryConfiguration.Active.InstrumentationKey = "my instrumentation key";
Logger logger = LogManager.GetLogger("Example");
logger.Info("Hello World");
Console.ReadLine();
使用以下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>
但是,当ColoredConsole目标按预期工作时,Azure Application Insights中没有日志消息。只有当我调用TelemetryConfiguration.Active.TelemetryChannel.Flush()时,消息才会到达代码>
- 我是否犯了配置错误李>
- 我需要自己调用.Flush()吗
在客户端将数据发送到门户之前,遥测项目会被缓冲(默认情况下为30秒,请参阅)。因此,您必须保持控制台至少打开一段时间,或手动调用Flush()
,或将设置为true:
编辑
可以使用多个通道,如内存通道中的或服务器遥测通道
。它们都有30秒的默认缓冲时间间隔
例如,内存通道中的具有一个公共属性TimeSpan SendingInterval
,因此如果将遥测配置.Active.遥测通道
强制转换为实际实现,则应该能够更改缓冲区间隔
请参阅和。您知道是否可以配置缓冲区间隔吗?无论如何,当我在应用程序退出时调用Flush()
时,我可以确保所有缓冲消息都发送到Azure application Insights,对吗?提前谢谢。正确,Flush可以。我不确定你是否可以更改缓冲区间隔。我还没有找到一种方法。很酷,非常感谢!我投票赞成你的答案,但我还没有足够的声誉;-)。建议在冲水后睡觉,因为冲水是不同步的。版本。2.6.0-beta3允许NLog通过调用NLog.LogManager.flush()
TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;