使用 NLog 和 MicrosoftApplicationInsights.NLogTarget 的控制台应用程序
Console App using NLog and MicrosoftApplicationInsights.NLogTarget
我的基于 .NET 的控制台应用程序有以下 NLog.config。
<targets>
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Console" name="console"
layout="${longdate}|${uppercase:${level}}|${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="file" />
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
我按照其他地方的说明安装了 Microsoft.ApplicationInsights.Web 并且一切正常,例如
var client = new TelemetryClient();
// logging a custom event using the AI API
client.TrackEvent("AppInsights is now ready for logging");
client.Flush();
这与 NLog 输出到文件和控制台一起工作。但是,只要我添加 Microsoft.ApplicationInsights.NLogTarget,所有 NLog 控制台输出和文件输出就会消失,并且不会出现在 Application Insights 中。
有什么想法吗?我在我的 NLog.config 中尝试了不同的设置,为 Application Insights 设置目标,我所做的任何事情都没有任何区别。
经过一些研究和帮助,我可以解决这个问题:数据可以显示在控制台/文件/应用程序洞察中。
您需要将 nlog.config 与 app.config 合并(在安装包 Microsoft.ApplicationInsights.NLogTarget 之后)。合并到 app.config 后,您可以删除 nlog.config 或将其保留在那里。
您应该通过这种方式设置应用程序洞察工具密钥:TelemetryConfiguration.Active.InstrumentationKey = "xxxx";
代码如下:
private static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
var client = new TelemetryClient();
TelemetryConfiguration.Active.InstrumentationKey = "xxxxxxxxxx";
client.TrackEvent("0222: app insights now sending a custom event xxxxxx");
logger.Trace("0222 Nlog :this is a trace message xxxxxx");
logger.Debug("0222 Nlog: this is a debug message xxxxxx");
logger.Info("0222 Nlog: this is a info message xxxxxx");
Console.ReadLine();
}
将nlog.config合并到app.config中(在安装包Microsoft.ApplicationInsights.NLogTarget之后),所以新的app.config如下所示(然后你可以删除nlog.config 或将其留在那里):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
</extensions>
<targets>
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Console" name="console"
layout="${longdate}|${uppercase:${level}}|${message}" />
<target xsi:type="ApplicationInsightsTarget" name="aiTarget"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
<logger name="*" minlevel="Trace" writeTo="console" />
<logger name="*" minlevel="Trace" writeTo="aiTarget"/>
</rules>
</nlog>
</configuration>
运行 你的项目,你可以在控制台看到输出结果 window:
然后转到您定义的日志文件,您可以看到日志文件已创建并具有正确的数据。
最后,导航到 Azure 门户 -> 您的应用见解 -> 搜索,您可以看到消息在那里(可能需要几分钟):
顺便说一下,如何检查消息是否可以发送到 Azure 门户应用洞察:
当运行项目在visual studio时,检查输出window:
如果看到"Application Insights Telemetry (unconfigured):",则表示仪器密钥不正确或设置不正确。它将发送到应用洞察:
如果没有未配置,则可以将其发送到应用洞察。
我的基于 .NET 的控制台应用程序有以下 NLog.config。
<targets>
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Console" name="console"
layout="${longdate}|${uppercase:${level}}|${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="file" />
<logger name="*" minlevel="Debug" writeTo="console" />
</rules>
我按照其他地方的说明安装了 Microsoft.ApplicationInsights.Web 并且一切正常,例如
var client = new TelemetryClient();
// logging a custom event using the AI API
client.TrackEvent("AppInsights is now ready for logging");
client.Flush();
这与 NLog 输出到文件和控制台一起工作。但是,只要我添加 Microsoft.ApplicationInsights.NLogTarget,所有 NLog 控制台输出和文件输出就会消失,并且不会出现在 Application Insights 中。
有什么想法吗?我在我的 NLog.config 中尝试了不同的设置,为 Application Insights 设置目标,我所做的任何事情都没有任何区别。
经过一些研究和帮助,我可以解决这个问题:数据可以显示在控制台/文件/应用程序洞察中。
您需要将 nlog.config 与 app.config 合并(在安装包 Microsoft.ApplicationInsights.NLogTarget 之后)。合并到 app.config 后,您可以删除 nlog.config 或将其保留在那里。
您应该通过这种方式设置应用程序洞察工具密钥:
TelemetryConfiguration.Active.InstrumentationKey = "xxxx";
代码如下:
private static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
var client = new TelemetryClient();
TelemetryConfiguration.Active.InstrumentationKey = "xxxxxxxxxx";
client.TrackEvent("0222: app insights now sending a custom event xxxxxx");
logger.Trace("0222 Nlog :this is a trace message xxxxxx");
logger.Debug("0222 Nlog: this is a debug message xxxxxx");
logger.Info("0222 Nlog: this is a info message xxxxxx");
Console.ReadLine();
}
将nlog.config合并到app.config中(在安装包Microsoft.ApplicationInsights.NLogTarget之后),所以新的app.config如下所示(然后你可以删除nlog.config 或将其留在那里):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget"/>
</extensions>
<targets>
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
<target xsi:type="Console" name="console"
layout="${longdate}|${uppercase:${level}}|${message}" />
<target xsi:type="ApplicationInsightsTarget" name="aiTarget"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
<logger name="*" minlevel="Trace" writeTo="console" />
<logger name="*" minlevel="Trace" writeTo="aiTarget"/>
</rules>
</nlog>
</configuration>
运行 你的项目,你可以在控制台看到输出结果 window:
然后转到您定义的日志文件,您可以看到日志文件已创建并具有正确的数据。
最后,导航到 Azure 门户 -> 您的应用见解 -> 搜索,您可以看到消息在那里(可能需要几分钟):
顺便说一下,如何检查消息是否可以发送到 Azure 门户应用洞察:
当运行项目在visual studio时,检查输出window:
如果看到"Application Insights Telemetry (unconfigured):",则表示仪器密钥不正确或设置不正确。它将发送到应用洞察:
如果没有未配置,则可以将其发送到应用洞察。