From fc30f7c9742c85c7e88589c9b62186dd475a1c38 Mon Sep 17 00:00:00 2001 From: Elliot Coyle Date: Fri, 8 Nov 2019 16:46:03 +0000 Subject: [PATCH] Fixing an exception when the header already exists --- .../Enrichers/CorrelationIdHeaderEnricher.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Enrichers.CorrelationId/Enrichers/CorrelationIdHeaderEnricher.cs b/src/Serilog.Enrichers.CorrelationId/Enrichers/CorrelationIdHeaderEnricher.cs index 66a1ea0..57b3517 100644 --- a/src/Serilog.Enrichers.CorrelationId/Enrichers/CorrelationIdHeaderEnricher.cs +++ b/src/Serilog.Enrichers.CorrelationId/Enrichers/CorrelationIdHeaderEnricher.cs @@ -52,8 +52,15 @@ private string GetCorrelationId() var correlationId = string.IsNullOrEmpty(header) ? Guid.NewGuid().ToString() : header; - - _contextAccessor.HttpContext.Response.Headers.Add(_headerKey, correlationId); + +#if NETFULL + if(!_contextAccessor.HttpContext.Response.Headers.AllKeys.Contains(_headerKey)) +#else + if (!_contextAccessor.HttpContext.Response.Headers.ContainsKey(_headerKey)) +#endif + { + _contextAccessor.HttpContext.Response.Headers.Add(_headerKey, correlationId); + } return correlationId; }