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#

Integrating action filters for logging in Umbraco offers multiple advantages:

  • Effortless Integration: Add robust logging with minimal effort.
  • Granular Control: Apply filters globally or to specific API endpoints.
  • Performance-Friendly: Non-blocking, asynchronous operations ensure your application remains responsive.

These benefits make custom action filters a go-to solution for API logging in Umbraco.

Step 1: Crafting the Custom Logging Filter#

Start by creating a new class, UmbracoCustomLoggingAttribute, that inherits from ActionFilterAttribute.

The key method is OnActionExecutedAsync, which allows access to the HttpActionExecutedContext object, including the Exception property.

Here’s how to implement it:

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);
   }
}

Pro Tip: Use meaningful log messages to make troubleshooting faster.

Step 2: Integrating the Filter#

Now that your custom filter is ready, it’s time to apply it.

Depending on your needs, you can annotate an entire UmbracoApiController or specific endpoints.

Example: Annotating a Controller

[UmbracoCustomLogging]
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");
   }
}

This flexible approach lets you decide where logging should occur - globally for consistency or selectively for optimization.

Step 3: Optimizing Your Logging Configuration#

Logging can be a double-edged sword.

While detailed logs are invaluable in development, excessive logging in production can impact performance.

Adjusting Logging Levels in Umbraco 8

Umbraco 8 uses Serilog, a powerful and customizable logging library. 

Here’s how to configure it:

  1. Go to the \config directory in your project.
  2. Open the serilog.config file.
  3. Locate the <appSettings> section.
  4. Set serilog:minimum-level to Error to capture only critical logs.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- VALID Values: Verbose, Debug, Information, Warning, Error, Fatal -->
        <add key="serilog:minimum-level" value="Error" />
    </appSettings>
</configuration>

Why “Error” Level?
Logging only errors reduces clutter, making it easier to spot critical issues in production.

Conclusion#

Effective error logging is the backbone of a smooth user experience.

Implementing custom action filters and configuring optimal logging levels can ensure efficient debugging and stable performance for your Umbraco API.

Ready to take your Umbraco expertise to the next level?

Explore More: Visit our blog for additional insights into maximizing Umbraco's potential.

Need Help? Contact us for tailored support with your Umbraco projects.

↑ Top ↑