Logging from Umbraco to Logentries with log4net, LeAppender

So while building this Umbraco site I wanted to use a custom log4net appender, in this case I wanted to connect log4net to logentries to be able to track my logs online. Normally when wanting to use log4net and send to logentries u just install a Nuget package and setup the appender in the log4net.config. But since log4net comes bundled in Umbraco in a none nuget-package kind of way, when installing the appender package my site crashed.

Giving me this amazing error...

log4net-umbraco-error

After a bit of searching I found out that Umbraco, out of the box, doesn't play nice with log4net when installed from Nuget. But there was a way to solve this, so here's a little guide how to use the log4net logentries appender, LeAppender with Umbraco.

Fixing the error

First we need to make sure the umbraco log4net dll-file is added to our project.

  1. Get a version of log4net.dll file umbraco is using by going to the umbraco download page and download the version of umbrao that the site is using.

  2. Extract the file and add it to your VS project.

  3. Rename the file to something other than log4net.dll, in this example I renamed mine to log4net.umbraco.dll log4net-umbraco-dll

  4. Right-click on the file and open properties for the file set Build Action: None Copy to Output Directory: Copy if newer

  5. Now when building the log4net.umbraco.dll file in this case should end up under bin\Libraries\log4net.umbraco.dll After these steps add the following code to the web.config-file, this enabled the dll-file.

Installing Logentries Appender

To installing the latest version of Logentries log4net appender, all you have to do is install the nuget-package with the command Install-Package logentries.log4net this adds Logentries Target library and will also automatically install the log4net package as a dependency.

Configure log4net to use Logentries Appender

To start using the appender open the log4net.config-file, this is by default found at config/log4net.config.

Add the following code to your log4net.config-file, this enabled log4net to find the appender.

<appender name="LeAppender" type="log4net.Appender.LogentriesAppender, LogentriesLog4net">
  <immediateFlush value="true" />
  <debug value="true" />
  <useHttpPut value="false" />
  <useSsl value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <!-- The below pattern has been carefully formatted and optimized to work well with the Logentries.com entry parser. For reference see https://logentries.com/doc/search/. -->
    <param name="ConversionPattern" value="%d %logger %level% %m%n" />
  </layout>
</appender>

Now we want to add the log token, this can be done by either adding it to the appender in log4net.config

<appender name="LeAppender" type="log4net.Appender.LogentriesAppender, LogentriesLog4net">
    <token value="bb61600f-f766-451e-b55f-9204f536a79f" />
    ...
</appender>

or by adding it to web.config

<appSettings>
    <add key="Logentries.Token" value="bb61600f-f766-451e-b55f-9204f536a79f" />
</appSettings>

If the settings doesn't seem to load you might need to append the following line to AssemblyInfo.cs for log4net to parse the config line. [assembly: log4net.Config.XmlConfigurator(ConfigFile="[config filename]", Watch = true)]

If you want you can replace the default appender by changing the root -> appender-ref ref from AsynchronousLog4NetAppender to LeAppender

<root>
  <priority value="Info"/>
  <appender-ref ref="LeAppender" />
</root>

If you want to know more about the logentries setup. You can find the full documentation about Logentries Appender here.

Testing the logger

Here is an example class how that send two log messages at startup, one on debug level and one on info level.

public class StartUpLogger : ApplicationEventHandler
{
    private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
    protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication,
    ApplicationContext applicationContext)
    {
        Log.Debug("This is a DEBUG message");
        Log.Info("Here is a INFO message");
    }
}
About Johan Boström

I'm a system architect, developer and solution expert, with experience in web-, application-, server- and windows service development with main focus on .NET / C#. Special skills with many kinds of CMS-tools, like EPiServer, Umbraco and Litium Studio.

Comments