Enhanced Logging for Umbraco API

Dive into leveraging custom action filters in Umbraco v8 for efficient exception logging. Here's a comprehensive guide to streamlining your API error-tracking process.

Benefits of Action Filter Logging in Umbraco

Logging exceptions in any CMS, including Umbraco, is paramount for seamless user experiences.

Utilizing action filters for this purpose brings forth a slew of advantages:

  • Quick Integration: Easily implementable without a lengthy setup.
  • Flexibility: Choose between global or endpoint-specific logging.
  • Asynchronous Functionality: Ensures non-blocking operations for enhanced performance.

Crafting the Custom Logging Filter

Kickstart by defining the UmbracoCustomLoggingAttribute class, a descendant of the ActionFilterAttribute.

The crux is the OnActionExecutedAsync method, granting access to the HttpActionExecutedContext object and therein, the “Exception” property.

If this property isn't null, employ Umbraco's built-in logger.

public sealed class UmbracoCustomLoggingAttribute : ActionFilterAttribute
   public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
      if (actionExecutedContext.Exception != null)
         Current.Logger.Error<UmbracoCustomLoggingAttribute>(actionExecutedContext.Exception, "An unexpected error has occurred");

      return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);

Integrating the Filter

With the attribute constructed, it's time to integrate it into your UmbracoApiController.

You can either annotate the entire controller or specific methods, offering refined control over the logging process.

public class TestUmbracoApiController : UmbracoApiController
   public IHttpActionResult EndpointA(string id)
      return Json("Hello from Endpoint A");

   public IHttpActionResult EndpointB(string id)
      return Json("Hello from Endpoint B");

Optimal Logging Configuration

Tip: For sites in production, it's prudent to adjust the logging level.

Elevated logging levels enhance performance.

Umbraco 8 integrates seamlessly with the renowned Serilog library.

To restrict logs to “Error” level and above:

  1. Navigate to the \config directory.
  2. Locate and open the serilog.config file.
  3. Head to the <appSettings> section.
  4. Alter the “serilog:minimum-level” value to “Error”.
<?xml version="1.0" encoding="utf-8" ?>
        <!-- VALID Values: Verbose, Debug, Information, Warning, Error, Fatal -->
        <add key="serilog:minimum-level" value="Error" />


Logging errors is the silent sentinel of your Umbraco API, ensuring smooth experiences while identifying potential hitches.

With custom action filters, elevate your logging process, enhancing efficiency and precision. 

Now, equip yourself with these insights and jumpstart a faultless Umbraco journey.

For further assistance or queries, contact us.

Eager to dive deeper into the world of Umbraco? Check out our blog for more insightful articles.

↑ Top ↑