Skip to main content

With the release of Umbraco v7.6 a whole list of visual back office changes were released, but that was not all. One of the smaller changes was the update of the log4net dependency to v2.0.8.
That might not sound significant, but before this release Umbraco was shipping with a custom build of log4net for legacy support reasons. With v7.6 there was the chance to remove this and include the official log4net release, which has made it a lot easier to use other libraries and packages that rely on log4net as a Nuget dependency, without having to resort to a workaround.

One of the packages that I was interested to try out is the Application Insights Log4Net Appender (on Nuget & GitHub).
It allows your application to send any log4net logs up to your Application Insights account, so you can analyse them in the Application Insights dashboard.

Why another log?

But why would you need another log if the default Umbraco logs capture everything anyway and you can use the Diplo Trace Log Viewer package to view logs from within the CMS?
In my opinion there are two reasons why sending your logs to Application Insights can be beneficial:

  1. If the exception captured was part of a frontend request (i.e. a visitor to your website or an editor in the CMS), Application Insights will allow you to view the captured telemetry of that session, so you can see what other pages the user might have visited before or after the exception occurred, and which browser they were using.
    This is the kind of information you cannot get from a single entry in the Umbraco trace log, but can be immensely useful for diagnosing the cause of the error.
  2. Application Insights allows you to setup alert rules that will notify you when the website suddenly starts reporting a large number of exceptions. You can either setup a metric rule that will get triggered if the number of exceptions logged gets over a certain threshold (e.g. 5 exceptions in the last 5 minutes) or let Smart Detection use its machine learning algorithms and automatically notify you when it detects potential issues.
    This kind of alerting is a great dev ops tool to help you proactively deal with an issue at an early stage before it becomes a major issue that could affect all your website's users.

How to set it up?

I'm assuming you're already using Application Insights in your project, but otherwise follow their installation instructions first.
The easiest way to add the log appender to your project is by adding the Nuget package:

Install-Package Microsoft.ApplicationInsights.Log4NetAppender

This will add some log4net appender configuration settings to the log4net section in the web.config of your website. You will need to move these, since Umbraco uses a separate config file for log4net settings (~/Config/log4net.config).
Once moved, your log4net config file will look like this:

<?xml version="1.0"?>
<log4net>
  
  <root>
    <priority value="Info"/>
    <appender-ref ref="AsynchronousLog4NetAppender" />
    <appender-ref ref="aiAppender" />
  </root>

  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="App_Data\Logs\UmbracoTraceLog.txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value=" %date [P%property{processId}/D%property{appDomainId}/T%thread] %-5level %logger - %message%newline" />
    </layout>
    <encoding value="utf-8" />
  </appender>

  <appender name="AsynchronousLog4NetAppender" type="Umbraco.Core.Logging.ParallelForwardingAppender,Umbraco.Core">
    <appender-ref ref="rollingFile" />
  </appender>

  <appender name="aiAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message%newline" />
    </layout>
  </appender>

  <!--Here you can change the way logging works for certain namespaces  -->

  <logger name="NHibernate">
    <level value="WARN" />
  </logger>
  
</log4net>

The changes can be found on lines 26 to 30 where the appender is created and log format is defined.
And on line 7 the appender is added to the list, so any log entries should now be added into Application Insights.
As you can see the existing Umbraco log appender is still there, which means that you can still access the logs in their usual location as well.

Application Insights is a great way to track a website's performance, but I was after a setup that allowed me to disable this on my dev environment and make sure it's enabled on live.

Read more

I noticed that a website hosted on Azure Web Apps was generating multiple Umbraco trace log files for the same day. I went to investigate what caused it, and also came up with a suggested change to resolve this.

Read more