-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathClientInfoLoggerConfigurationExtensions.cs
More file actions
92 lines (83 loc) · 4.52 KB
/
ClientInfoLoggerConfigurationExtensions.cs
File metadata and controls
92 lines (83 loc) · 4.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using Microsoft.AspNetCore.Http;
using Serilog.Configuration;
using Serilog.Enrichers;
namespace Serilog;
/// <summary>
/// Extension methods for setting up client IP, client agent and correlation identifier enrichers
/// <see cref="LoggerEnrichmentConfiguration" />.
/// </summary>
public static class ClientInfoLoggerConfigurationExtensions
{
/// <summary>
/// Registers the client IP enricher to enrich logs with
/// <see cref="Microsoft.AspNetCore.Http.ConnectionInfo.RemoteIpAddress" /> value.
/// </summary>
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
public static LoggerConfiguration WithClientIp(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
ArgumentNullException.ThrowIfNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
return enrichmentConfiguration.With<ClientIpEnricher>();
}
/// <summary>
/// Registers the client IP enricher to enrich logs with
/// <see cref="Microsoft.AspNetCore.Http.ConnectionInfo.RemoteIpAddress" /> value.
/// </summary>
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
/// <param name="ipVersionPreference">The IP version preference for filtering IP addresses.</param>
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
public static LoggerConfiguration WithClientIp(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
IpVersionPreference ipVersionPreference)
{
ArgumentNullException.ThrowIfNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new ClientIpEnricher(ipVersionPreference));
}
/// <summary>
/// Registers the correlation id enricher to enrich logs with correlation id with
/// 'x-correlation-id' header information.
/// </summary>
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
/// <param name="headerName">
/// Set the 'X-Correlation-Id' header in case if service is behind proxy server. Default value
/// is 'x-correlation-id'.
/// </param>
/// <param name="addValueIfHeaderAbsence">
/// Add generated correlation id value if correlation id header not available in
/// <see cref="HttpContext" /> header collection.
/// </param>
/// <param name="addCorrelationIdToResponse">
/// Add correlation id value to <see cref="HttpContext.Response" /> header collection.
/// </param>
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
public static LoggerConfiguration WithCorrelationId(
this LoggerEnrichmentConfiguration enrichmentConfiguration,
string headerName = "x-correlation-id",
bool addValueIfHeaderAbsence = false,
bool addCorrelationIdToResponse = false)
{
ArgumentNullException.ThrowIfNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
return enrichmentConfiguration.With(new CorrelationIdEnricher(headerName, addValueIfHeaderAbsence, addCorrelationIdToResponse));
}
/// <summary>
/// Registers the HTTP request header enricher to enrich logs with the header value.
/// </summary>
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
/// <param name="propertyName">The property name of log</param>
/// <param name="headerName">The header name to log its value</param>
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
/// <exception cref="ArgumentNullException">headerName</exception>
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration,
string headerName, string propertyName = null)
{
ArgumentNullException.ThrowIfNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
ArgumentNullException.ThrowIfNull(headerName, nameof(headerName));
return enrichmentConfiguration.With(new ClientHeaderEnricher(headerName, propertyName));
}
}