【问题标题】:How to send Log4Net logs to Application Insights for .NET Core 3.1 web application?如何将 Log4Net 日志发送到 Application Insights for .NET Core 3.1 Web 应用程序?
【发布时间】:2021-11-12 10:42:14
【问题描述】:

无法将 Log4Net 日志刷新到用于 net core 3.1 的 Web 应用中的 Azure 应用洞察

这是项目文件。

log4net.config

<?xml version="1.0" encoding="utf-8"?>
<log4net>
        <!--Application Insights Appender-->
        <appender name='aiAppender' 
        type='Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, 
        Microsoft.ApplicationInsights.Log4NetAppender'>
        <layout type='log4net.Layout.PatternLayout'>
        <conversionPattern value='%message%newline' />
        </layout>
        </appender>
        <root>
        <level value="ALL" />
        <appender-ref ref="aiAppender" />
        </root>
</log4net>

ApplicationInsights.config

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
    <InstrumentationKey>..Added InstrumentationKey here..</InstrumentationKey>
</ApplicationInsights>

Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry();
    }

Nuget 包:

appsettings.json

{
      "ApplicationInsights": {
        "InstrumentationKey": "<Added instrumentation key here>"
      },
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Error"
          }
        }
      }
}

【问题讨论】:

    标签: .net-core log4net azure-application-insights .net-core-3.1 log4net-appender


    【解决方案1】:

    我终于通过更新 Startup.cs 中的 ConfigureServices() 来完成这项工作

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
            {
                InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"],
                EnableActiveTelemetryConfigurationSetup = true
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2019-02-12
      • 2021-04-09
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多