Search code examples
log4netazure-application-insights

Log4net doesn't log to Application Insights


I'm using log4net for my MVC application. The log4net.config file is independent from web.config. I added ApplicationInsights.Log4NetAppender to my UI project and moved the aiAppender from web.config to log4net.config. Somehow the application is able to log data into the Logs.txt, but it doesn't log to ApplicationInsights.

Please help me to figure out this issue. Here is my log4net.config:

<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file value="App_Data/Logs/Logs.txt" />
    <encoding value="utf-8" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
    </layout>
  </appender>
  <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
    <appender-ref ref="aiAppender" />
  </root>
</log4net>

The InstrucmentationKey for applicationinsights is reading from appsettings.

TelemetryConfiguration.Active.InstrumentationKey = WebConfigurationManager.AppSettings["InstrumentationKey"];

In AdaptiveSamplingTelemetryProcessor, I'm using adpative sampling.

<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
  <MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
  <ExcludedTypes>Event</ExcludedTypes>
</Add>

I could see all dependencies and requests get logged on ApplicationInsights, but couldn't find any data from log4net.


Solution

  • If the problem is in log4net, you can best enable log4net internal debugging:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
    </configuration>
    

    And write it to file:

    <configuration>
    ...
    
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
    
    ...
    </configuration>
    

    If there is any exception you will find it in the log file. It could also be that the AI logger uses buffering, or the application insights is buffering and not flushed when your application exits.

    more on Log4net Troubleshooting

    flush application insights when your ASP.NET Core application exits