11using Serilog . Configuration ;
22using Serilog . Enrichers ;
33using System ;
4+ using System . Web ;
45
5- namespace Serilog
6+ #if NETFULL
7+
8+ using Serilog . Enrichers . ClientInfo . Accessors ;
9+
10+ #else
11+ using Microsoft . AspNetCore . Http ;
12+ #endif
13+
14+ namespace Serilog ;
15+
16+ /// <summary>
17+ /// Extension methods for setting up client IP, client agent and correlation identifier enrichers <see cref="LoggerEnrichmentConfiguration"/>.
18+ /// </summary>
19+ public static class ClientInfoLoggerConfigurationExtensions
620{
721 /// <summary>
8- /// Extension methods for setting up client IP and client agent enrichers <see cref="LoggerEnrichmentConfiguration"/>.
22+ /// Registers the client IP enricher to enrich logs with client IP with 'X-forwarded-for'
23+ /// header information.
924 /// </summary>
10- public static class ClientInfoLoggerConfigurationExtensions
25+ /// <param name="enrichmentConfiguration">The enrichment configuration.</param>
26+ /// <param name="xForwardHeaderName">
27+ /// Set the 'X-Forwarded-For' header in case if service is behind proxy server. Default value
28+ /// is 'X-forwarded-for'.
29+ /// </param>
30+ /// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
31+ /// <returns>The logger configuration so that multiple calls can be chained.</returns>
32+ public static LoggerConfiguration WithClientIp ( this LoggerEnrichmentConfiguration enrichmentConfiguration , string xForwardHeaderName = null )
1133 {
12- /// <summary>
13- /// Registers the client IP enricher to enrich logs with client IP with 'X-forwarded-for' header information.
14- /// </summary>
15- /// <param name="enrichmentConfiguration"> The enrichment configuration. </param>
16- /// <param name="xForwardHeaderName">
17- /// Set the 'X-Forwarded-For' header in case if service is behind proxy server. Default value is 'X-forwarded-for'.
18- /// </param>
19- /// <exception cref="ArgumentNullException"> enrichmentConfiguration </exception>
20- /// <returns> The logger configuration so that multiple calls can be chained. </returns>
21- public static LoggerConfiguration WithClientIp ( this LoggerEnrichmentConfiguration enrichmentConfiguration , string xForwardHeaderName = null )
22- {
23- if ( enrichmentConfiguration == null )
24- throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
34+ if ( enrichmentConfiguration == null )
35+ throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
36+
37+ if ( ! string . IsNullOrEmpty ( xForwardHeaderName ) )
38+ ClinetIpConfiguration . XForwardHeaderName = xForwardHeaderName ;
2539
26- if ( ! string . IsNullOrEmpty ( xForwardHeaderName ) )
27- ClinetIpConfiguration . XForwardHeaderName = xForwardHeaderName ;
40+ return enrichmentConfiguration . With < ClientIpEnricher > ( ) ;
41+ }
42+
43+ /// <summary>
44+ /// Registers the client Agent enricher to enrich logs with 'User-Agent' header information.
45+ /// </summary>
46+ /// <param name="enrichmentConfiguration">The enrichment configuration.</param>
47+ /// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
48+ /// <returns>The logger configuration so that multiple calls can be chained.</returns>
49+ public static LoggerConfiguration WithClientAgent ( this LoggerEnrichmentConfiguration enrichmentConfiguration )
50+ {
51+ if ( enrichmentConfiguration == null )
52+ throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
53+
54+ return enrichmentConfiguration . With < ClientAgentEnricher > ( ) ;
55+ }
2856
29- return enrichmentConfiguration . With < ClientIpEnricher > ( ) ;
57+ /// <summary>
58+ /// Registers the correlation id enricher to enrich logs with correlation id with
59+ /// 'x-correlation-id' header information.
60+ /// </summary>
61+ /// <param name="enrichmentConfiguration">The enrichment configuration.</param>
62+ /// <param name="headerName">
63+ /// Set the 'X-Correlation-Id' header in case if service is behind proxy server. Default value
64+ /// is 'x-correlation-id'.
65+ /// </param>
66+ /// <param name="addValueIfHeaderAbsence">
67+ /// Add generated correlation id value if correlation id header not available in
68+ /// <see cref="HttpContext"/> header collection.
69+ /// </param>
70+ /// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
71+ /// <returns>The logger configuration so that multiple calls can be chained.</returns>
72+ public static LoggerConfiguration WithCorrelationId (
73+ this LoggerEnrichmentConfiguration enrichmentConfiguration ,
74+ string headerName = "x-correlation-id" ,
75+ bool addValueIfHeaderAbsence = false )
76+ {
77+ if ( enrichmentConfiguration == null )
78+ {
79+ throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
3080 }
3181
32- /// <summary>
33- /// Registers the client Agent enricher to enrich logs with 'User-Agent' header information.
34- /// </summary>
35- /// <param name="enrichmentConfiguration"> The enrichment configuration. </param>
36- /// <exception cref="ArgumentNullException"> enrichmentConfiguration </exception>
37- /// <returns> The logger configuration so that multiple calls can be chained. </returns>
38- public static LoggerConfiguration WithClientAgent ( this LoggerEnrichmentConfiguration enrichmentConfiguration )
82+ return enrichmentConfiguration . With ( new CorrelationIdEnricher ( headerName , addValueIfHeaderAbsence ) ) ;
83+ }
84+
85+ /// <summary>
86+ /// Registers the HTTP request header enricher to enrich logs with the header value.
87+ /// </summary>
88+ /// <param name="enrichmentConfiguration">The enrichment configuration.</param>
89+ /// <param name="headerName">The header name to log its value</param>
90+ /// <param name="addValueIfHeaderAbsence">
91+ /// Add generated correlation id value if correlation id header not available in
92+ /// <see cref="HttpContext"/> header collection.
93+ /// </param>
94+ /// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
95+ /// <exception cref="ArgumentNullException">headerName</exception>
96+ /// <returns>The logger configuration so that multiple calls can be chained.</returns>
97+ public static LoggerConfiguration WithRequestHeader ( this LoggerEnrichmentConfiguration enrichmentConfiguration , string headerName )
98+ {
99+ if ( enrichmentConfiguration == null )
39100 {
40- if ( enrichmentConfiguration == null )
41- throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
101+ throw new ArgumentNullException ( nameof ( enrichmentConfiguration ) ) ;
102+ }
42103
43- return enrichmentConfiguration . With < ClientAgentEnricher > ( ) ;
104+ if ( headerName == null )
105+ {
106+ throw new ArgumentNullException ( nameof ( headerName ) ) ;
44107 }
108+
109+ return enrichmentConfiguration . With ( new ClientHeaderEnricher ( headerName ) ) ;
45110 }
46111}
0 commit comments