Example #1
0
        /// <summary>
        /// Adds the <see cref="VersionEnricher"/> to the logger enrichment configuration which adds the current runtime version (i.e. 'version' = '1.0.0') of the assembly.
        /// </summary>
        /// <param name="enrichmentConfiguration">The configuration to add the enricher.</param>
        /// <param name="propertyName">The name of the property to enrich the log event with the current runtime version.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="enrichmentConfiguration"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when the <paramref name="propertyName"/> is blank.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the process executable in the default application domain cannot be retrieved.</exception>
        public static LoggerConfiguration WithVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration, string propertyName = VersionEnricher.DefaultPropertyName)
        {
            Guard.NotNull(enrichmentConfiguration, nameof(enrichmentConfiguration), "Requires an enrichment configuration to add the version enricher");
            Guard.NotNullOrWhitespace(propertyName, nameof(propertyName), "Requires a non-blank property name to enrich the log event with the current runtime version");

            return(enrichmentConfiguration.With(new VersionEnricher(propertyName)));
        }
Example #2
0
        static LoggerConfiguration WithAssemblyInformationalVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration, Assembly assembly)
        {
            var versionString = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                                .InformationalVersion;

            return(enrichmentConfiguration.WithProperty("AssemblyVersion", versionString));
        }
        public static Serilog.LoggerConfiguration WithLazyProperties(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            IDictionary <string, Func <object> > properties)
        {
            if (loggerEnrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }

            if (properties == null)
            {
                throw new ArgumentNullException(nameof(properties));
            }

            if (properties.Count == 0)
            {
                throw new ArgumentException("Logger properties count cannot be zero.", nameof(properties));
            }

            Serilog.LoggerConfiguration loggerConfiguration = null;

            foreach (var property in properties)
            {
                loggerConfiguration = loggerEnrichmentConfiguration.WithProperty(property.Key, property.Value());
            }

            return(loggerConfiguration);
        }
 public static LoggerConfiguration WithSensitiveDataMasking(
     this LoggerEnrichmentConfiguration loggerConfiguration, MaskingMode mode,
     IEnumerable <IMaskingOperator> operators)
 {
     return(loggerConfiguration
            .With(new SensitiveDataEnricher(mode, operators)));
 }
    public static LoggerConfiguration WithHttpContextContent(
        this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
    {
        var enricher = serviceProvider.GetService <HttpContextContentEnricher>();

        return(enrichmentConfiguration.With(enricher));
    }
    public static LoggerConfiguration WithDayOfWeek(
        this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
    {
        var enricher = serviceProvider.GetService <DayOfWeekEnricher>();

        return(enrichmentConfiguration.With(enricher));
    }
Example #7
0
        public static LoggerConfiguration FromServiceContext(this LoggerEnrichmentConfiguration enrichmentConfiguration, ServiceContext context)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var propertyEnrichers = new PropertyEnricher[]
            {
                new PropertyEnricher(nameof(context.ServiceName), context.ServiceName),
                new PropertyEnricher(nameof(context.ServiceTypeName), context.ServiceTypeName),
                new PropertyEnricher(nameof(context.ReplicaOrInstanceId), context.ReplicaOrInstanceId.ToString()),
                new PropertyEnricher(nameof(context.PartitionId), context.PartitionId.ToString()),
                new PropertyEnricher(nameof(context.CodePackageActivationContext.ApplicationName), context.CodePackageActivationContext.ApplicationName),
                new PropertyEnricher(nameof(context.CodePackageActivationContext.ApplicationTypeName), context.CodePackageActivationContext.ApplicationTypeName),
                new PropertyEnricher(nameof(context.CodePackageActivationContext.CodePackageName), context.CodePackageActivationContext.CodePackageName),
                new PropertyEnricher(nameof(context.CodePackageActivationContext.CodePackageVersion), context.CodePackageActivationContext.CodePackageVersion),
                new PropertyEnricher(nameof(context.NodeContext.NodeName), context.NodeContext.NodeName),
                new PropertyEnricher(nameof(context.TraceId), context.TraceId),
            };

            return(enrichmentConfiguration.With(propertyEnrichers));
        }
    public static LoggerConfiguration WithInternalCorrelationId(
        this LoggerEnrichmentConfiguration enrichmentConfiguration, IServiceProvider serviceProvider)
    {
        var enricher = serviceProvider.GetService <InternalCorrelationIdEnricher>();

        return(enrichmentConfiguration.With(enricher));
    }
Example #9
0
        public void WithCorrelationIdHeader_WhenLoggerEnrichmentConfigurationIsNull_ShouldThrowArgumentNullException()
        {
            LoggerEnrichmentConfiguration configuration = null;

            // ReSharper disable once ExpressionIsAlwaysNull
            Assert.Throws <ArgumentNullException>(() => configuration.WithCorrelationIdHeader());
        }
Example #10
0
        static LoggerConfiguration WithAssemblyVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration, Assembly assembly, bool useSemVer)
        {
            var version       = assembly.GetName().Version;
            var versionString = useSemVer ? $"{version.Major}.{version.Minor}.{version.Build}" : $"{version}";

            return(enrichmentConfiguration.WithProperty("AssemblyVersion", versionString));
        }
 public static LoggerConfiguration WithApplicationDetails(this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException("enrichmentConfiguration");
     }
     return(enrichmentConfiguration.With <ApplicationDetailsEnricher>());
 }
 public static LoggerConfiguration CustomerLogEnricher(this LoggerEnrichmentConfiguration enrich)
 {
     if (enrich == null)
     {
         throw new ArgumentNullException(nameof(enrich));
     }
     return(enrich.With <CustomLogEnricher>());
 }
Example #13
0
 public static LoggerConfiguration WithApplicationInfo(this LoggerEnrichmentConfiguration enrichmentConfiguration, Guid applicationUid)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With(new ApplicationInfoEnricher(applicationUid)));
 }
Example #14
0
 private static LoggerConfiguration WithCommonProperties(this LoggerEnrichmentConfiguration config)
 {
     return(config.FromLogContext()
            .Enrich.WithMachineName()
            .Enrich.WithProcessName()
            .Enrich.WithProcessId()
            .Enrich.WithThreadId());
 }
 /// <summary>
 /// Enrich log events in the specified configuration with information
 /// about the current ASP.NET HTTP request.
 /// </summary>
 /// <param name="loggerConfiguration">The configuration to modify.</param>
 /// <returns>Configuration object allowing configuration to continue.</returns>
 public static LoggerConfiguration WithHttpRequestProperties(this LoggerEnrichmentConfiguration loggerConfiguration)
 {
     if (loggerConfiguration == null)
     {
         throw new ArgumentNullException("loggerConfiguration");
     }
     return(loggerConfiguration.With(new HttpRequestLogEventEnricher()));
 }
 /// <summary>
 /// Enrich log events with a Environment variables.
 /// </summary>
 /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
 /// <param name="environmentVariable">Environment variable to enrich with.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public static LoggerConfiguration WithEnvironment(this LoggerEnrichmentConfiguration enrichmentConfiguration, string environmentVariable)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With(new EnvironmentEnricher(environmentVariable)));
 }
 /// <summary>
 /// 添加日志级别扩展属性
 /// </summary>
 /// <param name="source">日志扩展配置</param>
 public static LoggerConfiguration WithLogLevel(this LoggerEnrichmentConfiguration source)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     return(source.With <LogLevelEnricher>());
 }
 public static LoggerConfiguration WithTraceId(this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With <TraceIdEnricher>());
 }
Example #19
0
        /// <summary>
        /// Enrich logger output with a destuctured object containing
        /// exception's public properties.
        /// </summary>
        /// <param name="loggerEnrichmentConfiguration">The enrichment configuration</param>
        /// <param name="destructuringOptions">
        /// Options that will influence the process of destructuring
        /// exception's properties into result object.
        /// </param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static Serilog.LoggerConfiguration WithExceptionDetails(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            IDestructuringOptions destructuringOptions)
        {
            ILogEventEnricher enricher = new ExceptionEnricher(destructuringOptions);

            return(loggerEnrichmentConfiguration.With(enricher));
        }
Example #20
0
        public static Serilog.LoggerConfiguration WithExceptionDetails(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration,
            IEnumerable <IExceptionDestructurer> destructurers)
        {
            ILogEventEnricher enricher = new ExceptionEnricher(new DestructuringOptionsBuilder().WithDestructurers(destructurers));

            return(loggerEnrichmentConfiguration.With(enricher));
        }
 /// <summary>
 /// Enrich log events with properties from <see cref="Context.LogContext"/>.
 /// </summary>
 /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 /// <exception cref="ArgumentNullException"></exception>
 public static LoggerConfiguration FromLogContext(
     this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With <LogContextEnricher>());
 }
Example #22
0
 public static LoggerConfiguration WithCorrelationId(
     this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
 {
     if (loggerEnrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
     }
     return(loggerEnrichmentConfiguration.With <CorrelationIdEnricher>());
 }
 /// <summary>
 /// Enrich log events with a ProcessId property containing the current <see cref="Process.Id"/>.
 /// </summary>
 /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public static LoggerConfiguration WithProcessId(
     this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException("enrichmentConfiguration");
     }
     return(enrichmentConfiguration.With <ProcessIdEnricher>());
 }
        /// <summary>
        /// Enrich logger output with span information from the current <see cref="Activity"/>.
        /// </summary>
        /// <param name="loggerEnrichmentConfiguration">The enrichment configuration.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration WithHoneycomb(this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
        {
            if (loggerEnrichmentConfiguration is null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }

            return(loggerEnrichmentConfiguration.With <ActivityEnricher>());
        }
Example #25
0
        /// <summary>
        /// Enriches the Serilog logging data with Mass Transit context information.
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static LoggerConfiguration FromMassTransitMessage(this LoggerEnrichmentConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            return(configuration.With((ILogEventEnricher) new SerilogEnricher()));
        }
 /// <summary>
 /// Enrich log events with a MachineName property containing the current <see cref="Environment.MachineName"/>.
 /// </summary>
 /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public static LoggerConfiguration WithMachineName(
     this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException("enrichmentConfiguration");
     }
     return(enrichmentConfiguration.With <MachineNameEnricher>());
 }
Example #27
0
        public static LoggerConfiguration WithUtcTimestamp(this LoggerEnrichmentConfiguration enrich)
        {
            if (enrich == null)
            {
                throw new ArgumentNullException(nameof(enrich));
            }

            return(enrich.With <UtcTimestampEnricher>());
        }
        /// <summary>
        /// Enrich log events with a user name.
        /// </summary>
        /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration WithUserName(this LoggerEnrichmentConfiguration enrichmentConfiguration)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.With(new KeyValueEnricher(new KeyValuePair <string, object>("UserName", Environment.UserName))));
        }
Example #29
0
 public static LoggerConfiguration WithHangfireContext(
     this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With <HangfireConsoleSerilogEnricher>());
 }
 /// <summary>
 /// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
 /// </summary>
 /// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
 /// <returns>Configuration object allowing method chaining.</returns>
 public static LoggerConfiguration WithEnvironmentUserName(
     this LoggerEnrichmentConfiguration enrichmentConfiguration)
 {
     if (enrichmentConfiguration == null)
     {
         throw new ArgumentNullException(nameof(enrichmentConfiguration));
     }
     return(enrichmentConfiguration.With <EnvironmentUserNameEnricher>());
 }