.net-corelog4netazure-application-insights.net-core-3.1log4net-appender

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


Not able to flush Log4Net logs to azure application insights in the web app for net core 3.1

Here're the project files.

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 Packages:

enter image description here

appsettings.json

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

Solution

  • I got this working finally by updating ConfigureServices() in Startup.cs

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