|
| 1 | +using Serilog.Configuration; |
| 2 | +using Serilog.Core; |
| 3 | +using SerilogWeb.Classic.Enrichers; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Serilog |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Extends <see cref="LoggerConfiguration"/> to add enrichers for SerilogWeb.Classic's logging module |
| 14 | + /// </summary> |
| 15 | + public static class SerilogWebClassicLoggerConfigurationExtensions |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Enrich log events with the named Claim Value. |
| 19 | + /// </summary> |
| 20 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 21 | + /// <param name="claimProperty">The claim property name searched for value to enrich log events.</param> |
| 22 | + /// <param name="logEventProperty">The property name added to enriched log events. Leave null (default) to use the claim property name.</param> |
| 23 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 24 | + public static LoggerConfiguration WithClaimValue( |
| 25 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 26 | + string claimProperty, |
| 27 | + string logEventProperty = null) |
| 28 | + { |
| 29 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 30 | + return enrichmentConfiguration.With(new ClaimValueEnricher(claimProperty, logEventProperty)); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Enrich log events with the Client IP Address. |
| 35 | + /// </summary> |
| 36 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 37 | + /// <param name="checkForHttpProxies">if set to <c>true</c> this Enricher also checks for HTTP proxies and their X-FORWARDED-FOR header.</param> |
| 38 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 39 | + public static LoggerConfiguration WithHttpRequestClientHostIP( |
| 40 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 41 | + bool checkForHttpProxies = true) |
| 42 | + { |
| 43 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 44 | + return enrichmentConfiguration.With(new HttpRequestClientHostIPEnricher(checkForHttpProxies)); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Enrich log events with the Client Host Name. |
| 49 | + /// </summary> |
| 50 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 51 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 52 | + public static LoggerConfiguration WithHttpRequestClientHostName( |
| 53 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 54 | + { |
| 55 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 56 | + return enrichmentConfiguration.With<HttpRequestClientHostNameEnricher>(); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Enrich log events with a HttpRequestId GUID. |
| 61 | + /// </summary> |
| 62 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 63 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 64 | + public static LoggerConfiguration WithHttpRequestId( |
| 65 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 66 | + { |
| 67 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 68 | + return enrichmentConfiguration.With<HttpRequestIdEnricher>(); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Enrich log events with a HttpRequestNumber unique within the current |
| 73 | + /// logging session. |
| 74 | + /// </summary> |
| 75 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 76 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 77 | + public static LoggerConfiguration WithHttpRequestNumber( |
| 78 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 79 | + { |
| 80 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 81 | + return enrichmentConfiguration.With<HttpRequestNumberEnricher>(); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Enrich log events with the Url of the Request. |
| 86 | + /// </summary> |
| 87 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 88 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 89 | + public static LoggerConfiguration WithHttpRequestUrl( |
| 90 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 91 | + { |
| 92 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 93 | + return enrichmentConfiguration.With< HttpRequestUrlEnricher>(); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Enrich log events with the Raw Url of the Request. |
| 98 | + /// </summary> |
| 99 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 100 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 101 | + public static LoggerConfiguration WithHttpRequestRawUrl( |
| 102 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 103 | + { |
| 104 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 105 | + return enrichmentConfiguration.With<HttpRequestRawUrlEnricher>(); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Enrich log events with a HttpRequestTraceId GUID matching the |
| 110 | + /// RequestTraceIdentifier assigned by IIS and used throughout |
| 111 | + /// ASP.NET/ETW. IIS ETW tracing must be enabled for this to work. |
| 112 | + /// </summary> |
| 113 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 114 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 115 | + public static LoggerConfiguration WithHttpRequestTraceId( |
| 116 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 117 | + { |
| 118 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 119 | + return enrichmentConfiguration.With<HttpRequestTraceIdEnricher>(); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Enrich log events with the HTTP Request Type. |
| 124 | + /// </summary> |
| 125 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 126 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 127 | + public static LoggerConfiguration WithHttpRequestType( |
| 128 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 129 | + { |
| 130 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 131 | + return enrichmentConfiguration.With<HttpRequestTypeEnricher>(); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Enrich log events with the Url of the Referrer. |
| 136 | + /// </summary> |
| 137 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 138 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 139 | + public static LoggerConfiguration WithHttpRequestUrlReferrer( |
| 140 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 141 | + { |
| 142 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 143 | + return enrichmentConfiguration.With<HttpRequestUrlReferrerEnricher>(); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Enrich log events with the Client User Agent. |
| 148 | + /// </summary> |
| 149 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 150 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 151 | + public static LoggerConfiguration WithHttpRequestUserAgent( |
| 152 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 153 | + { |
| 154 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 155 | + return enrichmentConfiguration.With<HttpRequestUserAgentEnricher>(); |
| 156 | + } |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Enrich log events with the HttpSessionId property. |
| 160 | + /// </summary> |
| 161 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 162 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 163 | + public static LoggerConfiguration WithHttpSessionId( |
| 164 | + this LoggerEnrichmentConfiguration enrichmentConfiguration) |
| 165 | + { |
| 166 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 167 | + return enrichmentConfiguration.With<HttpSessionIdEnricher>(); |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Enrich log events with the UserName property when available in the HttpContext. |
| 172 | + /// </summary> |
| 173 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 174 | + /// <param name="anonymousUsername">The anonymous username. Leave null if you do not want to use anonymous user names. By default it is (anonymous).</param> |
| 175 | + /// <param name="noneUsername">The none username. If there is no username to be found, it will output this username. Leave null (default) to ignore non usernames.</param> |
| 176 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 177 | + public static LoggerConfiguration WithUserName( |
| 178 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 179 | + string anonymousUsername = "(anonymous)", |
| 180 | + string noneUsername = null) |
| 181 | + { |
| 182 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 183 | + return enrichmentConfiguration.With(new UserNameEnricher(anonymousUsername, noneUsername)); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments