What is log correlation
Easy ASP.NET log correlation with Serilog and Seq
- February 19, 2014 8:21 am
It has been a while since I did any ASP.NET development, I have to admit. Lately I’ve had to dust off my MVC to transform http://getseq.net from a static site into an ASP.NET-based one with a few more smarts.
In the process I rediscovered a little Serilog gem that I’d completely forgotten about.
I’ve been working on a staging copy of the site, and I keep a little Seq query handy called “Exceptions” – it is a simple query: Has(@Exception). that finds events with an exception property attached.
Here’s one such nasty:
Yikes, where did this come from, and what’s wrong with my password?
Now, the exception is interesting and there are even a few properties attached that give a clue about where it is from. The apple of my eye however is HttpRequestId .
This is added by a little-known helper in the Serilog.Extras.Web package. Install it into your ASP.NET project, and you get access to some useful features for ASP.NET logging.
To attach a unique identifier for the current ASP.NET web request to your log events, add the enricher to your log configuration:
Log. Logger = new LoggerConfiguration ( )
Enrich. With ( new HttpRequestIdEnricher ( ) )
WriteTo. RollingFile ( "C: \L ogs \m y-site-
WriteTo. Seq ( "http://my-seq" )
CreateLogger ( ) ;
(Naturally I’m also using the Seq.Client.Serilog
package as well to push events to my Seq server.)
With that bit of configuration in place, any log events generated during a request will have a unique request ID attached.
Back to my exception; by filtering to the HTTP request ID (and closing the “Exceptions” query, if it is open):
I get the events logged during the request, showing the lead-up to the colossal crash, while generating a new trial key for myself :)
In this sorry case, the error message being generated by the Crypto API is completely wrong, and in fact I needed to enable user profile loading on the ASP.NET app pool. Ah, so many ways to shoot oneself in the foot!
Hopefully this little sample has convinced you to try Serilog and Seq, if you haven’t already!
There’s some documentation on setting up Serilog – it is a 5-minute task and even supports standard .NET format string logging if you want to convert from log4net or NLog.
There’s also now some documentation on the Seq setup process (another 5-minute job) and integrating Seq with various logging frameworks .
We’re closing in on a 1.0 release of Seq – there are still tweaks to be made and some final details of the editions and pricing to nail down, but the free developer edition will definitely make the cut and is perfect for many uses.
If you’re already on your way to log correlation nirvana I’d love to hear from you.Source: nblumhardt.com