37

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>
Pete Philters
  • 869
  • 5
  • 12
BMac
  • 2,183
  • 3
  • 22
  • 30
  • Hello, I have the same issue... Do you have already found a solution? Thanks... – bob Mar 26 '15 at 08:16
  • @bob no, not as yet. I have reached out to the MS team responsible but no feedback as yet. – BMac Mar 26 '15 at 08:39
  • I'm having this exact same issue. I can see uncaught exceptions logged in Application Insights but no tracing for log4net even though it's appearing in the file appender just fine. Did you ever find a reason for this? – Phil May 06 '15 at 17:01
  • hi @Phil , I did not unfortunately. I have parked this for now, as my current project is a few months off production as yet - I do plan on getting back to this before my go live date though. Will certainly update if I ever do get a fix. – BMac May 06 '15 at 20:24
  • Thanks for getting back @Macb. I tried creating a canonical example following what [someone else had blogged](http://geekswithblogs.net/jakob/archive/2014/11/09/using-log4net-for-application-insights.aspx) but still it doesn't work. I've left a comment on that blog fishing for any hints and will come back with any insights (pardon the pun) I come up with. – Phil May 07 '15 at 07:40
  • @Macb I stumbled across a possible solution and posted an answer - fingers crossed it helps you too! – Phil May 07 '15 at 07:57
  • 1
    I'm stepping through the code in Reflector. I don't have it fully working yet, but I have made some progress. In the type attribute of the appender element, try "ApplicationInsights.Log4Net.InsightsAppender, ApplicationInsights.Log4Net, Version=38.0.0.0, Culture=neutral, PublicKeyToken=null" or whatever the AssemblyQualifiedName for your DLL is. "qualified assembly name" and "full assembly name" are different, a difference I glossed over at first. If the appender type is not in log4net's DLL, I think it needs qualified assembly name. – MindModel Mar 01 '16 at 18:36

4 Answers4

5

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 (archive.org backup) 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 :)

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Phil
  • 2,232
  • 1
  • 26
  • 35
  • Thakns @Phil I will certainly give this a try, and let you know. – BMac May 07 '15 at 08:16
  • 1
    @Phil I'm trying to do the same and have APp Insights logging exceptions, number of requests, etc., but I can't see my Log.Info() messages anywhere. Any suggestions? – awj Feb 19 '16 at 16:41
  • Sorry @awj but I'm not looking at my Application Insights integration much at all at the moment and my workload means I'm not likely to for a while either. – Phil Feb 19 '16 at 21:48
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>
  ...
Vladislav
  • 1,696
  • 27
  • 37
  • did you add any telemetry modules? – Enrico Mar 04 '21 at 14:00
  • As far as I can remember, the standard modules were registered (such as `RequestTrackingTelemetryModule` and `ExceptionTrackingTelemetryModule`) – Vladislav Mar 09 '21 at 09:40
  • Great hint, @Vladislav! I was able to have my application tracing logs to AI thanks to your post ;) I had followed a tutorial where nothing was told about the Microsoft.ApplicationInsights.Web NuGet package. – Tiresia Oct 06 '22 at 18:04
3

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>
Robin B
  • 301
  • 1
  • 4
  • Hi Robin, I also made my own appender, but somehow, some exception don't show in AI portal. Have you ran into similar problems? – EricImhauser Sep 03 '19 at 12:41
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?

tokaplan
  • 108
  • 7
  • thanks @tokaplan I shall give this a try tonight and respond either way, this certainly does sound plausible. – BMac Mar 31 '15 at 09:16
  • unfortunately this did not seem to make any difference, still no event traffic showing on the outbound when viewing via fiddler and hence nothing in the dashboard. I think I am going to start with first principles. – BMac Mar 31 '15 at 20:06