|
| 1 | +using Serilog.Configuration; |
| 2 | +using SerilogWeb.Classic.Mvc.Enrichers; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Serilog |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Extends <see cref="LoggerConfiguration"/> to add enrichers for SerilogWeb.Classic.Mvc logging module |
| 13 | + /// </summary> |
| 14 | + public static class SerilogWebClassicMvcLoggerConfigurationExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Enrich log events with the name of the current MVC action. |
| 18 | + /// </summary> |
| 19 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 20 | + /// <param name="propertyName">Name of the property to log.</param> |
| 21 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 22 | + public static LoggerConfiguration WithMvcActionName( |
| 23 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 24 | + string propertyName = MvcActionNameEnricher.MvcActionPropertyName) |
| 25 | + { |
| 26 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 27 | + return enrichmentConfiguration.With(new MvcActionNameEnricher(propertyName)); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Enrich log events with the controller name for the current MVC action. |
| 32 | + /// </summary> |
| 33 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 34 | + /// <param name="propertyName">Name of the property to log.</param> |
| 35 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 36 | + public static LoggerConfiguration WithMvcControllerName( |
| 37 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 38 | + string propertyName = MvcControllerNameEnricher.MvcControllerPropertyName) |
| 39 | + { |
| 40 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 41 | + return enrichmentConfiguration.With(new MvcControllerNameEnricher(propertyName)); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Enrich log events with the route data for the current MVC action. |
| 46 | + /// </summary> |
| 47 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 48 | + /// <param name="propertyName">Name of the property to log.</param> |
| 49 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 50 | + public static LoggerConfiguration WithMvcRouteData( |
| 51 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 52 | + string propertyName = MvcRouteDataEnricher.MvcRouteDataPropertyName) |
| 53 | + { |
| 54 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 55 | + return enrichmentConfiguration.With(new MvcRouteDataEnricher(propertyName)); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Enrich log events with the template for the current MVC action. |
| 60 | + /// </summary> |
| 61 | + /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param> |
| 62 | + /// <param name="propertyName">Name of the property to log.</param> |
| 63 | + /// <returns>Configuration object allowing method chaining.</returns> |
| 64 | + public static LoggerConfiguration WithMvcRouteTemplate( |
| 65 | + this LoggerEnrichmentConfiguration enrichmentConfiguration, |
| 66 | + string propertyName = MvcRouteTemplateEnricher.MvcRouteTemplatePropertyName) |
| 67 | + { |
| 68 | + if (enrichmentConfiguration == null) throw new ArgumentNullException(nameof(enrichmentConfiguration)); |
| 69 | + return enrichmentConfiguration.With(new MvcRouteTemplateEnricher(propertyName)); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments