~/codewithstu

How to Setup OpenTelemetry Logging in .NET

Transcript

You've discovered that OpenTelemetry can handle logs as well as traces but you're not quite sure how to set this up. Well, you're in the right place. My name is Stu and in this video we're going to be taking you through how to set up an ASP.NET Core web application with OpenTelemetry logs.

So before we get started we need to add one package to our application. This is the OpenTelemetry.Exporter.Console package. The next thing that we need to do is import two namespaces. The first one is OpenTelemetry.Logs and the second is OpenTelemetry.Resources.

As with any OpenTelemetry application, we need to ensure that we set the resource. In this case we're using ResourceBuilder.CreateDefault and then adding the service code to "Stu". This will be attached to all of the logs that go out from the OpenTelemetry system. The next thing that we need to do is then call AddOpenTelemetry on builder.Logging. Now this provides us a lambda function where we can set a few different options. The first option that we're going to set is the ResourceBuilder. This provides the link between the resource that we created earlier and all of our OpenTelemetry logs. Earlier on I added the console exporter, but you might wish to choose something else like the OpenTelemetry Protocol exporter.

And now we run our application as normal. As you can see, the output on the console is slightly different to what we would normally have. Nevertheless, this means that our application is working as we expected it to. Naturally, if you pick a different exporter than the one I've used here, you may not get the same output and you may have to verify this in another way.

By default this does include quite a lot of information, but sometimes we don't actually want all of this information. Sometimes we want just something like warnings and above. All we need to do to set this up is call AddFilter with the OpenTelemetry logging provider and then set up the options that we want. In this case we're setting everything to be a warning or above for this logging provider. Now we just run our application as normal and you can see the amount of logs has massively reduced.

In some instances you may want to do things like alter the state of the log messages before they get published to your exporter of choice. In this case we would use a log processor. To do this we just create a new class as we would normally and just make it inherit from the BaseProcessor of LogRecord. Once this is done, this gives you access to various methods such as OnStart, OnEnd, OnShutdown, and OnFlush. Which one you pick is entirely dedicated to your scenario. One important thing to note is that these methods should not block and they should not throw exceptions.

To make use of this new log processor, we go back to our options earlier and we just call options.AddProcessor and pass in an instance of our class. If you want to receive some of the state values and the formatted messages, then you need to set these properties on the options class itself. If we run our application one more time, you'll be able to see the new log message appearing in the console.

If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.

// share_this