serilog-enrichers-clientinfo icon indicating copy to clipboard operation
serilog-enrichers-clientinfo copied to clipboard

NullReferenceException for CorrelationId when targeting .NET Framework

Open LucaMahler opened this issue 1 year ago • 1 comments

When using the CorrelationIdEnricher and targeting .NET framework, a NullReferenceException will occur here: var header = httpContext.Request.Headers[_headerKey].ToString();

Because httpContext.Request.Headers[_headerKey] is actually null. The next line is:

   var correlationId = !string.IsNullOrWhiteSpace(header)
       ? header
       : (_addValueIfHeaderAbsence ? Guid.NewGuid().ToString() : null);

This is a bit strange. You should call .ToString() after the null check, don't you?

LucaMahler avatar Jun 17 '24 11:06 LucaMahler

This code works fine in .NET Core and unfortunately, there is no way that I can implement unit tests for enrichers when the target framework is .NET Framework and the reason is that HttpCaontext.Current is a static class and mocking it is extremely hard. For the next release, I will drop support for .NET Framework.

mo-esmp avatar Jun 28 '24 08:06 mo-esmp