/// <summary>
        /// Adds the <see cref="CorrelationInfoEnricher{TCorrelationInfo}"/> to the logger enrichment configuration which adds the <see cref="CorrelationInfo"/> information
        /// from the current HTTP context, using the <see cref="HttpCorrelationInfoAccessor"/>.
        /// </summary>
        /// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
        /// <param name="serviceProvider">The provider to retrieve the <see cref="IHttpContextAccessor"/> service.</param>
        /// <remarks>
        ///     In order to use the <see cref="HttpCorrelationInfoAccessor"/>, the <see cref="IServiceCollectionExtensions.AddHttpCorrelation"/> must be called first.
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="enrichmentConfiguration"/> or <paramref name="serviceProvider"/> is <c>null</c>.</exception>
        public static LoggerConfiguration WithHttpCorrelationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
        {
            Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration));
            Guard.NotNull(serviceProvider, nameof(serviceProvider));

            return(enrichmentConfiguration.WithCorrelationInfo(
                       new HttpCorrelationInfoAccessor(
                           serviceProvider.GetRequiredService <IHttpContextAccessor>())));
        }
        public static LoggerConfiguration WithEnvironmentName(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.With <EnvironmentNameEnricher>());
        }
        public static LoggerConfiguration WithDiagnosticActivity(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.With <DiagnosticActivityEnricher>());
        }
        public static LoggerConfiguration WithApplicationVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.With <ApplicationVersionEnricher>());
        }
        public static LoggerConfiguration WithMessageTemplate(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.With <MessageTemplateEnricher>());
        }
 public static LoggerConfiguration WithDefaults(this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     return(enrichmentConfiguration
            .WithExceptionDetails()
            .Enrich.WithDiagnosticActivity()
            .Enrich.WithApplicationName()
            .Enrich.WithApplicationVersion()
            .Enrich.WithEnvironmentName()
            .Enrich.WithMachineName()
            .Enrich.WithThreadId()
            .Enrich.FromLogContext());
 }
Beispiel #7
0
        public static LoggerConfiguration Wrap(
            LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            Func <ILogEventEnricher, ILogEventEnricher> wrapEnricher,
            Action <LoggerEnrichmentConfiguration> configureWrappedEnricher)
        {
            if (loggerEnrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }
            if (wrapEnricher == null)
            {
                throw new ArgumentNullException(nameof(wrapEnricher));
            }
            if (configureWrappedEnricher == null)
            {
                throw new ArgumentNullException(nameof(configureWrappedEnricher));
            }

            var enrichersToWrap = new List <ILogEventEnricher>();

            var capturingConfiguration = new LoggerConfiguration();
            var capturingLoggerEnrichmentConfiguration = new LoggerEnrichmentConfiguration(
                capturingConfiguration,
                enrichersToWrap.Add);

            // `Enrich.With()` will return the capturing configuration; this ensures chained `Enrich` gets back
            // to the capturing enrichment configuration, enabling `Enrich.WithX().Enrich.WithY()`.
            capturingConfiguration.Enrich = capturingLoggerEnrichmentConfiguration;

            configureWrappedEnricher(capturingLoggerEnrichmentConfiguration);

            if (enrichersToWrap.Count == 0)
            {
                return(loggerEnrichmentConfiguration._loggerConfiguration);
            }

            var enclosed = enrichersToWrap.Count == 1 ?
                           enrichersToWrap.Single() :
                           // Enrichment failures are not considered blocking for auditing purposes.
                           new SafeAggregateEnricher(enrichersToWrap);

            var wrappedEnricher = wrapEnricher(enclosed);

            // ReSharper disable once SuspiciousTypeConversion.Global
            if (!(wrappedEnricher is IDisposable))
            {
                SelfLog.WriteLine("Wrapping enricher {0} does not implement IDisposable; to ensure " +
                                  "wrapped enrichers are properly disposed, wrappers should dispose " +
                                  "their wrapped contents", wrappedEnricher);
            }

            return(loggerEnrichmentConfiguration.With(wrappedEnricher));
        }
Beispiel #8
0
        /// <summary>
        /// Adds the <see cref="CorrelationInfoEnricher{TCorrelationInfo}"/> to the logger enrichment configuration which adds the <see cref="CorrelationInfo"/> information
        /// from the current HTTP context, using the <see cref="HttpCorrelationInfoAccessor"/>.
        /// </summary>
        /// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
        /// <param name="serviceProvider">The provider to retrieve the <see cref="IHttpContextAccessor"/> service.</param>
        /// <remarks>
        ///     In order to use the <see cref="HttpCorrelationInfoAccessor"/>, it first has to be added to the <see cref="IServiceCollection"/>.
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="enrichmentConfiguration"/> or <paramref name="serviceProvider"/> is <c>null</c>.</exception>
        public static LoggerConfiguration WithHttpCorrelationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
        {
            Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration), "Requires a Serilog logger enrichment configuration to register the HTTP correlation as enrichment");
            Guard.NotNull(serviceProvider, nameof(serviceProvider), "Requires a service provider to retrieve the HTTP correlation from the registered services when enriching the Serilog with the HTTP correlation");

            var httpContextAccessor = serviceProvider.GetService <IHttpContextAccessor>();

            if (httpContextAccessor is null)
            {
                throw new InvalidOperationException(
                          $"Cannot register the HTTP correlation as a Serilog enrichment because no {nameof(IHttpContextAccessor)} was available in the registered services,"
                          + "please make sure to call 'services.AddHttpCorrelation()' when configuring the services. "
                          + "For more information on HTTP correlation, see the official documentation: https://webapi.arcus-azure.net/features/correlation");
            }

            var correlationInfoAccessor = new HttpCorrelationInfoAccessor(httpContextAccessor);

            return(enrichmentConfiguration.WithCorrelationInfo(correlationInfoAccessor));
        }
		protected override void Configure( LoggerEnrichmentConfiguration configuration ) => configuration.With( Items.Fixed() );
		protected override void Configure( LoggerEnrichmentConfiguration configuration ) => configuration.WithProperty( PropertyName, Value, DestructureObjects );
Beispiel #11
0
 // Unlike the other configuration methods, FromLogContext is an instance method rather than an extension.
 internal static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
 {
     return loggerEnrichmentConfiguration.FromLogContext();
 }
Beispiel #12
0
 /// <summary>
 /// Adds the <see cref="CorrelationInfoEnricher{TCorrelationInfo}"/> to the logger enrichment configuration which adds the <see cref="CorrelationInfo"/> information
 /// from the current HTTP context, using the <see cref="HttpCorrelationInfoAccessor"/>.
 /// </summary>
 /// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
 /// <param name="serviceProvider">The provider to retrieve the <see cref="IHttpContextAccessor"/> service.</param>
 /// <remarks>
 ///     In order to use the <see cref="HttpCorrelationInfoAccessor"/>, the <see cref="IServiceCollectionExtensions.AddHttpCorrelation"/> must be called first.
 /// </remarks>
 public static LoggerConfiguration WithHttpCorrelationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
 {
     return(enrichmentConfiguration.WithCorrelationInfo(new HttpCorrelationInfoAccessor(serviceProvider.GetService <IHttpContextAccessor>())));
 }
		protected override void Configure( LoggerEnrichmentConfiguration configuration ) => configuration.FromLogContext();