【问题标题】:Log4Net and Application Insights-no data is coming throughLog4Net 和 Application Insights - 没有数据通过
【发布时间】:2017-07-04 11:50:41
【问题描述】:

我在我的 C# 控制台应用程序中使用 Log4Net,并且希望记录的事件也出现在 Application Insights 中。

我添加了 Application Insights 以及 Application Insights Log4Net Appender。

我已按照此处的说明进行操作:https://jan-v.nl/post/using-application-insights-in-your-log4net-application,但没有运气。

Log4Net 记录正常。但是,当我转到 Application Insights 仪表板时,我看到:“应用程序在应用程序洞察力中没有数据。”

在我的主 cs 文件的开头,我有这个:

var telemetryClient = new TelemetryClient { InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"] };

最后是这样的:

telemetryClient.Flush();

在我的 app.config 文件中,我有这个:

 <log4net>
    <root>
      <level value="INFO" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="aiAppender" />
    </root>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="C:\logs\logfile.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
      </layout>
    </appender>
    <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline" />
      </layout>
    </appender>
  </log4net>

当我运行应用程序时,我在输出窗口中看到了这样的东西:

Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Message","time":"2017-07-04T10:26:15.7741453Z","tags":{"ai.internal.sdkVersion":"log4net:2.2.0-220","ai.cloud.roleInstance":"xxx","ai.user.id":"AzureAD\\xxx"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"xxx","severityLevel":"Information","properties":{xxx"}}}}

我错过了什么?

【问题讨论】:

  • app.config 中有 AI 检测键吗?
  • 是的。 此处填充字符

标签: c# azure-application-insights


【解决方案1】:

自从提出这个问题以来,ApplicationInsights 已经发展。由于各种原因,原来的静态 TelemetryConfiguration.Active 现在已经过时了。那些仍在尝试快速完成 log4net 配置的人可以这样做(在他们的 log4net 配置文件中):

<appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message%newline" />
  </layout>
  <threshold value="INFO" />
  <InstrumentationKey value="12345678-7946-1234-1234-8b330fbe1234" />
</appender>

【讨论】:

  • 对于我自己的应用程序,我实际上将其改进为在本地复制/更改 ApplicationInsightsAppender 以允许我在启动时通过代码设置 TelemetryClient。我希望能够设置 user/session/... 值,而不是让应用程序中的 appender 和遥测位通过两个具有自己 sessionID 的不同遥测通道。
【解决方案2】:

Log4Net AI appender 不会使用您创建的 TelemtryClient。

像这样设置 AI Instrumentation Key:

TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["applicationInsights"];

见,https://github.com/Microsoft/ApplicationInsights-dotnet-logging

【讨论】:

  • 因为日志记录包对您的 TelemetryClient 实例一无所知。单个应用程序(或网站或其他)完全有可能将不同类型的遥测数据发送到不同的 ikey,因此在您的示例中,您创建了一个“私有”遥测客户端,其中您的代码是唯一知道的那个特定的 ikey。日志记录使用来自TelemetryConfiguration.Active 的共享配置将数据发送到该公共位置。
  • 此方法已被标记为过时:我们不建议在 .NET Core 上使用 TelemetryConfiguration.Active。更多详情请见github.com/microsoft/ApplicationInsights-dotnet/issues/1152
【解决方案3】:

适用于 dotnetcore 2.2Microsoft.Extensions.Logging.ApplicationInsights 2.10

我还有一个问题,我的日志没有发送到 AI。找了很久之后。在我看到调试我对 AI 的调用和遥测通道被执行后,我就走上了这条路,但是当只是运行 api 时,没有发送任何数据。

我发现它与使用的遥测通道有关(查看 microsoft 的在线文档中的遥测通道是什么,但它充当您的应用程序和 azure 应用程序洞察力之间的中间件)。

如果您在 DEV 机器上工作(没有服务器!),您可以将此标志设置为 true

在 StartUp.ConfigureServices 中:

#if DEBUG
            TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
#endif

然后我所有的日志都被发送到 AI,并且可以在跟踪表中找到,并且可以看到每个请求。

我在他们的 GIT 页面上找到了答案:https://github.com/Microsoft/ApplicationInsights-dotnet/issues/964

另外注意:默认级别是Warning,要设置为较低级别,请将其添加到program.cs

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .ConfigureLogging(logging =>
            {                
                // Optional: Apply filters to configure LogLevel Trace or above is sent to
                // ApplicationInsights for all categories.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);

                // Additional filtering For category starting in "Microsoft",
                // only Warning or above will be sent to Application Insights.
                logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);

            })
            .UseStartup<Startup>();
}

【讨论】:

  • 还可能值得在该应用退出之前设置一个延迟以确保数据被刷新。我在 webjobs 中发现了这一点,即使强制刷新应用程序见解也不能保证它会刷新到 AI 服务器。我发现除了我的“工作完成”跟踪之外的所有内容都被写入。我增加了 2 分钟的安全等待时间。这可能已经修复,但值得检查。
猜你喜欢
  • 2015-05-02
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
相关资源
最近更新 更多