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.

Top comments (0)