We-Code Digital Studio

Software Development

NLog and Sentry’s breadcrumbs

--

Who has developed a bug-free software system, raise your hand?

As the mythical Dijkstra said:

Program testing can be used to show the presence of bugs, but never to show their absence!

And what usually saves us from very unpleasant situations once our product is released? A good log system and a robust error tracking system. Since I became a senior developer (20 years of experience is enough? I don’t know yet I have much to learn) during the initial stages of developing a new project I spend a lot of time configuring a good approach for logging and error tracking.

Almost all of my colleagues know how much I love, in the .Net world, the NLog logging library and the Sentry platform for error handling.

Configuring these two systems to work together is not such a complex job but doing it in the best way involves a couple of very particular aspects.

NLog + Sentry

First of all, we must remember that there is an official version of the library to integrate both released by Sentry team: https://github.com/getsentry/sentry-dotnet which works very well both as a .net logger and as a NLog target.

Its configuration is very simple and we have many guides and tutorials to integrate it with a few configuration lines.

PS: in a few weeks the new version should come out with important news including https://github.com/getsentry/sentry-dotnet/pull/336.

We come to my standard configuration which I use as the basis for each new project:

As already mentioned, the configuration file is really simple, we have various targets and the rule for Sentry is activated only when a log event with a minimum level equal to Error is written.

What happens at this point? Simple at each Error event NLog will use the Sentry target to transfer all the information to the remote tracking system.

But are we sure we have all the information necessary to have a real idea of what is going on in our software? With this simple configuration, a very useful functionality of Sentry has not exploited in the least: breadcrumbs!

Breadcrumbs are information that is collected and buffered by Sentry and sent when an error is raised. In the case of integration with NLog, breadcrumbs are the messages issued by the logger prior to the error event.

In order to use this cool feature, however, we need to change our NLog configuration file.

NLog config with Sentry’s breadcrumbs enabled

In particular, in order to activate this functionality, we must ensure that all the messages that make up our breadcrumbs and not just the errors reach the Sentry target. In order to have this effect, we only need to lower the level of our logger up to Debug and then configure two options: minimunBreadcrumbLevel and minimumEventLevel on the Sentry target node.

Here is an example of an event displayed by Sentry with the breadcrumbs enabled.

Breadcrumbs example on Sentry

Thanks and this simple configuration change we no longer need to find the necessary information from the log files local to our application.

As said, our software will always have bugs or at least we will never be sure of having them all correct and therefore why not prepare in the best way when the unexpected occurs?

What do you think? Tell me!

Please, Subscribe and Clap! Grazie!

--

--

Diego Bonura
Diego Bonura

Written by Diego Bonura

Software engineer with a strong sense of humor. Anyway yesterday it worked.

No responses yet