DEV Community

Wallism
Wallism

Posted on

Logging Headers with Serilog into Sumo

A quick post showing subtle changes to how request headers were being logged and the significant improvement on the end result.

Version 1

Log.Logger.Information("Request {headers}");

manifests like this in Sumo:
Image description

Ugly and larger than required.

Version 2

Log.Logger.Information("Request {@headers}");

Simply using the destructuring operator produces this:
Image description

Overall probably better but there's alot of expanding required to see all of the header values. Particularly annoying is the need to expand Value.

Version 3

Create a simple class
internal class Header
{
public string Key { get; set; }
public string Value { get; set; }
}

Extract the keyvalue pairs into a list of Headers:
var headerList = httpRequest.Headers.Select(kvp => new Header {Key = kvp.Key , Value = kvp.Value.FirstOrDefault()}).ToList();

Then log that list:
Log.Logger.Information("Request {@headersList}");

produces:
Image description

Marginally better, at least we don't need to expand Value.

Version 4

Still use the Header class and headerList from Version 3, but change the way we log it to:
Log.Logger.ForContext("Headers", headerList).Information("request")

The key difference being the use of ForContext, produces:
Image description

Which to my eye is perfect, all the headers, tightly grouped, one click to see them all.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay