-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCorrelationIdPreparer.cs
More file actions
52 lines (43 loc) · 1.52 KB
/
CorrelationIdPreparer.cs
File metadata and controls
52 lines (43 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
#nullable enable
namespace Serilog.Preparers.CorrelationIds
{
internal class CorrelationIdPreparer : ICorrelationIdPreparer
{
protected string? CorrelationId { get; set; }
/// <inheritdoc/>
public string? PrepareCorrelationId(
HttpContext httpContext,
CorrelationIdPreparerOptions correlationIdPreparerOptions)
{
if (string.IsNullOrEmpty(CorrelationId))
{
CorrelationId = PrepareValueForCorrelationId(httpContext, correlationIdPreparerOptions);
}
return CorrelationId;
}
protected string? PrepareValueForCorrelationId(
HttpContext httpContext,
CorrelationIdPreparerOptions correlationIdPreparerOptions)
{
StringValues requestHeader = httpContext.Request.Headers[correlationIdPreparerOptions.HeaderKey];
if (!string.IsNullOrWhiteSpace(requestHeader))
{
return requestHeader;
}
StringValues responseHeader = httpContext.Response.Headers[correlationIdPreparerOptions.HeaderKey];
if (!string.IsNullOrWhiteSpace(responseHeader))
{
return responseHeader;
}
if (correlationIdPreparerOptions.AddValueIfHeaderAbsence)
{
return Guid.NewGuid().ToString();
}
return null;
}
}
}
#nullable disable