KNOWLEDGE BASE

Logging Custom Errors with The Umbraco LogHelper


Logging errors is always a good idea, but sometimes developers face the dilemma of choosing exactly how and when to handle errors and log them. Do you have one big generic try-catch with enough code to handle every eventuality, or do you use several different try-catches to specifically catch and handle particular types of errors at very specific points?

The best choice may depend on your project, but in any case you will want to keep this code lean since it may come at the expense of the readaibility of what the code actually does. Luckily, we find Umbraco's Log Helper as a great way to satisfy logging without needing reams of code to make it meaningful!

The code below shows how you can catch an error and call up Umbraco's LogHelper to log your error:

try
{
// code that could throw an exception
}
catch (Exception ex)
{
  // do things to handle and...
   LogHelper.Error(this.GetType(), "Your custom error message here", ex);
}

When called, this custom error message is logged along with the details of the exception to /App_Data/Logs/UmbracoTraceLog.txt. This makes diagnosing site issues where you are unable to step into the code, or finding out the reason for historical errors a lot easier.

As a bonus, it keeps all your error logging (and Umbraco's!) in the same place making it easier to manage going forwards.


Need an Umbraco Master?

Here at Simon Antony, we have an in house certified Umbraco Grand Master available for hire. Got a problem with your site, need architecture advice, give us a call to speak to Simon directly and see how we can help

Contact Simon Today!