Logging in distributed system Part 3 — Exceptions

This is the third part of the Logging series. For your convenience you can find other parts in the table of contents in Part 1 – Correlations

So we need to handle errors and exceptions. Web jobs are already covered in the previous part so we need to take care of Sitefinity and WebAPI. Let’s begin with the former.

Table of Contents

Sitefinity

There are two paths: WebForms and MVC.

WebForms

This is easy, we only need to handle error:

We handle the case of headers already sent to the client and add them if possible. Now you need to call this method from Global.asax.cs and we are done.

MVC

This time we need to create another action filter:

The same stuff. You need to register this filter in your DI container and that is all. You could also consider adding HandleErrorAttribute globally.

WebAPI2

Now comes the tricky part. WebAPI2 actually has three different ways of handling exceptions: attribute, logger and handler. We can implement all of them:

We log everything we can, set headers and creates response if necessary.

You need to register your filters:

Summary

Now we handle exceptions and errors in our applications. In the next part we will see how to implement logic for passing correlation IDs and logical times to other systems using RestSharp.