Log4Net with Application Insights Log4Net with Application Insights azure azure

Log4Net with Application Insights


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


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>


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?