Code your assets

Structured logging has never been easier

October 23, 2024 | by p.b@techtocapital.com

DALL·E 2024-10-24 06.31.37 – A cartoon depiction of a person standing in front of multiple holographic screens filled with code logs, inspired by the technology in Minority Report

All software developers know this one too well. You get this brilliant idea, fire up a new project, get in the zone and start dishing out some code.

However, just as everything starts out simple, it also gains complexity over time. Due to little to no quality of life capabilities, such as structured logging, efficiency is taking a heavy plunge due to emerging need for troubleshooting.

After all, having some logging printed to console seemed to do the job when starting out.

I get it, the thought of setting up yet another cloud service for logging or, even worse, trying to get a Grafana + Prometheus setup going is just too high of a threshold when entering the creative zone where most new projects are created.

What if I told you that you could have your cake and eat it too?

Keep it simple

This is not a cloud service

Although there are many trends out there lobbying for necessity of various cloud services, or other complex setups, what you are seeing above is not yet another cloud service or results of a time tedious Kubernetes setup.

No, the above is result of a free and minimal effort approach which will set you up for good, and future-proof, code insights right from the beginning.

This is result of your standard Microsoft logger and OpenTelemetry.

Do this Today, thank yourself Tomorrow

Get your docker engine fired up and run

Aspire dashboard component

You should see a URL, provided by docker logs, that looks like this

Dashboard url in docker

Clicking on the link will bring up Microsoft Aspire’s dashboard ready to visualize logs, tracing and other useful metrics.

If you are using Windows, replace the 0.0.0.0 IP address with ‘localhost’.

The example in this article will be using C#, but you can use any language which supports OpenTelemetry.

To start using this from a console project add these packages

OpenTelemetry.Exporter.OpenTelemetryProtocol
OpenTelemetry.Extensions.Hosting
Microsoft.Extensions.Hosting

Then update your Program.cs file to look something like this

Program.cs

Enjoy your new, low threshold and free, visualizations of logs, traces and metrics!

For further experimentation, read up on the docs regarding tracing, metrics, or how to setup up using your web projects, here.

And when your project is ready to ship into a bigger ecosystem, with more sophisticated systems for logging, it’s going to be ready for that too.

Happy coding.