Log4Net with Application Insights

2019-03-14 10:24发布

问题:

I am trying to configure my azure asp.net website to send log4net traces to Azure Application Insights. I can see in my azure console page views etc, hence I know that is working fine. I can also see log4net traces, when configured with a file handler, but when configuring log4net to use the application insights handler I don't see any log4net entries appear in the application insight dashboard, no errors or warnings at build or run time - just no results in the dashboard. I have looked at the network traffic in Fiddler, and I can see the pageview data etc being sent to application insights, but not the log4net trace traffic hence I suspect this is a configuration issue.

Separately I have tried the TelemetryClient() in my main project, and I see the tracetraffic sucesfully in the dashboard. However, this does not fit my use case as TelemetryClient does not seem to support non asp.net dll's as yet (i.e. my business and data logic which are in separate dll's).

Anyone offer any insight or advice?

I have installed the nuget package for Microsoft.ApplicationInsights.Log4NetAppender.dll and I am using Microsoft.ApplicationInsights.0.13.2-build00132

I have the following in my web.config as per https://blog.ehn.nu/2014/11/using-log4net-for-application-insights/

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="ALL"/>
      <appender-ref ref="aiAppender"/>
    </root>
    <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%message%newline"/>
      </layout>
    </appender>
  </log4net>
<configuration>

回答1:

Try installing the PreRelease version of the Log4Net Appender.

I had created a canonical ASP.NET MVC example following the steps created by someone else and had the same problem above. But then followed some steps written in the Application Insights documentation and discovered that those specified installing the PreRelease package for the log4net appender. Having done that it workd :)



回答2:

Your log4net configuration is correct; I have used it in a test web site and it worked. Also, since you're seeing other AI data in Fiddler, your AI is also configured properly. One thing to look for in such cases is the log4net.Config.XmlConfigurator attribute. It's an assembly-level attribute and it might be necessary for log4net configuration to be read properly.

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Can you please make sure you have it specified in your project and try again?



回答3:

In my case (I have added Application Insights to existing web application) besides Microsoft.ApplicationInsights.Log4NetAppender I have had to add Microsoft.ApplicationInsights.Web package using NuGet. After that in created ApplicationInsights.config you need to specify your InstrumentationKey.

E.g. of ApplicationInsights.config

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


回答4:

I also had some problems with sending my Log4Net logs to AI in a Sitecore website. Sitecore has their own implementation of Log4Net so it didn't worked with the AI Nuget package. I've made my own Apprender in which I send the logs to AI.

 public class CustomLogFileAppender : SitecoreLogFileAppender
    {
        protected override void Append(LoggingEvent loggingEvent)
        {
            if (Sitecore.Context.Site != null )
            {

                if(loggingEvent.Level == Level.FATAL)
                {
                    AppsInsightsLogHelper.Critical(loggingEvent.RenderedMessage);
                }

                if (loggingEvent.Level == Level.ERROR)
                {
                    AppsInsightsLogHelper.Error(loggingEvent.RenderedMessage);
                }

                if (loggingEvent.Level == Level.WARN)
                {
                    AppsInsightsLogHelper.Warning(loggingEvent.RenderedMessage);
                }

                if(loggingEvent.Level == Level.INFO)
                {
                    AppsInsightsLogHelper.Info(loggingEvent.RenderedMessage);
                }


            }

                base.Append(loggingEvent);
        }
    }

In sitecore.config:

<log4net>
    <appender name="LogFileAppender" type="namespace.CustomLogFileAppender, dll name">
      ...
    </appender>
</log4net>