Episerver : The Easiest Way To Add An Error Handler Into Your Epi Site

EPiserver uses Log4Net as its default error handler.  The benefit of Log4Net for a developer is that you can also use it with very minimal configuration.  Log4Net is a really simple and flexible error handling system which is far better to use instead of having to come up with a custom solution yourself, or, importing additional bloat from Nuge. All you have to do is enable it in the config, send your exception to Log4Net in your code and everything else is done for you. To get start using Log4Net on your site First, in your webroot you will need to make a few tweaks to a file called 'EPiServerLog.config'.  This is where all the logging configuration lives.  At the end of that file you should see a section that looks similar to the below snippet. All you need to do to enable Log4Net on your site is to enable the appender ref so your config should look like this :



Boom! That's all there is to it, Log4Net is now configured for your site.. simple hey ? The next step is sending your exceptions to Log4Net to handler. First you need to add a reference to the assembly in your class:
using log4net;
Your next step is to declare the logger as an instance variable in your class :
        private static readonly ILog Logger = log4net.LogManager.GetLogger
What we're doing here is using reflection to tell Log4Net the class name to log (if you rename it you don't have to remember to update your logging reference). I normally add this into a base class so I only define it once but it's up to you. After this all you need to do is send any exceptions you may trap to Log4Net.
        public void Example()
            catch (Exception ex)
The code snippets pretty simple, you just call Log4Net rather than throwing the exception. I also find the Logger.Info method pretty useful at times and it's worth doing some research on. Now you have your system up and running how do you read these exceptions ? Simples... in your webroot you should now have a file called 'EPiServerErrorLog.txt' everything is sent to there. In Epi the default is to log all warnings (set in the EPiServerLog.config file) :

I normally only set my logger to log fatal issue as the warning level is too verbose. For reference you can use FATAL, ERROR, WARN, INFO, DEBUG. That's all there is to it. Enjoy :)

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