如何将 Log4Net 日志发送到 .NET Core 3.1 Web 应用程序的 Application Insights?

How to send Log4Net logs to Application Insights for .NET Core 3.1 web application?

无法将 Log4Net 日志刷新到 net core 3.1 的 Web 应用程序中的 azure application insights

这是项目文件。

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"
          }
        }
      }
}

我终于通过更新 Startup.cs

中的 ConfigureServices() 来完成这项工作
public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
        {
            InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"],
            EnableActiveTelemetryConfigurationSetup = true
        });
    }