Search code examples
c#azure-application-insights

Log4Net and Application Insights-no data is coming through


I'm using Log4Net in my my C# console application and want the logged events to appear in Application Insights as well.

I've added Application Insights as well as the Application Insights Log4Net Appender.

I've followed the instructions here: https://jan-v.nl/post/using-application-insights-in-your-log4net-application with no luck.

Log4Net is logging fine. However, when I go to the Application Insights dashboard, I see this: "THE APPLICATION HAS NO DATA IN APPLICATION INSIGHTS."

At the beginning of my main cs file, I've got this:

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

at the end, there is this:

telemetryClient.Flush();

In my app.config file, I have this:

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

When I run the application, I see this sort of thing in the output window:

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

What am I missing?


Solution

  • ApplicationInsights has evolved since this question was asked. The original static TelemetryConfiguration.Active is now obsolete for various reasons. Those still trying to get their log4net config up quickly can do this (in their log4net configuration file):

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