Log4Net Best Practices To Use With An Episerver Website

On a recent code base that I review, a previous agency made some interesting choices when it came to logging information into Episerver.  After doing a performance review on the site, every page load was wasting up to 2 seconds logging stuff.  

Over the years I've seen some pretty funky stuff done with logging and EPiserver, so I'd thought I'd write down some tips to try and prevent these mistakes.

Logging To Different Files

First up, it can be semi-common to want to write your logs into different files.  One common situation where I've wanted to do this is importing.  If you have a system where you need to import things like products or news, for example, it's a lot cleaner and easier to spot bugs to have all these logs outside your normal Episerver logs.  

If you need to do this, you DO NOT need to write any code.  You can write different errors to different log files through configuration within the episerver.config file, more information here

Do Not Write Your Own Custom Log4Net Implementation

I've seen a few companies write custom Log4Net implementations instead of simply using config.  You definitely don't have to write custom code to log things when you use Episerver. 

If you think you need to write a custom Log4Net appender and all you need to do is write something to disk, then use the out-of-the-box Episerver one!  Even if you want to write to a database, this can all be done in configuration.  So far I've yet to bump into a good reason why you need to write your own custom log appender, so write code that you don't' need to!

Logging Too Much Will Slow Down Your Website

You can tell Log4Net how much to log, and like most things the more it has to write, the more your page performance will be affected.   Log4Net appenders should be set to 'WARN' on your live website. Within the episerverlog.config ensure that All, Debug or Info is not set as anything less that than can affect site performance.  

Internal Log4Net Debugging

If you decide you want to write to a different log file and you've added some configuration within your episerverlog.config, it can be pretty easy to make a typo so nothing gets logged.  If you find yourself in this situation then you can enable the internal Log4Net logging to help you troubleshoot what's gone wrong. 

To enable the internal Log4Net debugger, in the web.config in the AppSettings makes sure this is enabled: 

<add key="log4net.Internal.Debug" value="true" />

To set where these logs are written, there's a section within the system.diagnosis section within the web.config:

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Temp\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>

If you do enable it, DO NOT PUSH THESE CHANGES TO YOUR LIVE ENVIROMENT! Enabling these logs is good for troubleshooting Log4Net configuration issues, it should not be run on your live website after you are happy the log files are writing correctly.

Make Your Logs Easily Visible

 

If your running your website in DXC, Azure etc.. you want to ensure your logs are easily visible. If you need to RDP onto a server to look at your logs, you've not being as effcient as you could be. If you use a tool like Stackify Relic Ensure Your Episerver Logs Are Apprearing WIthin Your Dashboard, you can install the Stackify Log4Net plug-in within your Episerver website and all you logs can be shipped to the cloud.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top