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);
        }
Example #2
0
        static LoggerConfiguration WithAssemblyInformationalVersion(this LoggerEnrichmentConfiguration enrichmentConfiguration, Assembly assembly)
        {
            var versionString = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                                .InformationalVersion;

            return(enrichmentConfiguration.WithProperty("AssemblyVersion", versionString));
        }
Example #3
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));
        }
Example #4
0
        public static LoggerConfiguration WithSchrodersSettings(this LoggerEnrichmentConfiguration enrichmentConfiguration, string applicationName)
        {
            var loggerConfiguration = enrichmentConfiguration.WithProperty(LoggingConstants.ApplicationName, applicationName);

            var version = Assembly.GetExecutingAssembly().GetName().Version;

            loggerConfiguration.Enrich.WithProperty(LoggingConstants.ApplicationVersion, version);

            var processId = Process.GetCurrentProcess().Id;

            loggerConfiguration.Enrich.WithProperty(LoggingConstants.ApplicationInstanceId, processId);

            return(loggerConfiguration);
        }
        public static LoggerConfiguration WithApplicationVersion(
            this LoggerEnrichmentConfiguration enrichmentConfiguration,
            string versionString)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }
            if (string.IsNullOrWhiteSpace(versionString))
            {
                throw new ArgumentNullException(nameof(versionString));
            }

            return(enrichmentConfiguration.WithProperty("Version", versionString));
        }
        public static LoggerConfiguration WithServiceName(
            this LoggerEnrichmentConfiguration enrichmentConfiguration,
            string serviceName = null)
        {
            if (enrichmentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(enrichmentConfiguration));
            }

            return(enrichmentConfiguration.WithProperty(
                       "ServiceName",
                       string.IsNullOrWhiteSpace(serviceName) == false
                    ? serviceName
                    : Assembly.GetEntryAssembly().GetName().Name
                       ));
        }
        /// <summary>
        /// Enrich log events with a EnvironmentName property containing the value of the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT environment variable.
        /// </summary>
        /// <param name="configuration">Logger enrichment configuration.</param>
        /// <param name="defaultValue">Default environment name when no value found (default: Not Configured)</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration WithEnvironmentName(this LoggerEnrichmentConfiguration configuration,
                                                              string?defaultValue = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            string?environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            if (string.IsNullOrWhiteSpace(environmentName))
            {
                environmentName = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
            }
            if (string.IsNullOrWhiteSpace(environmentName))
            {
                environmentName = defaultValue ?? DefaultValue;
            }

            return(configuration.WithProperty(PropertyName, environmentName));
        }
Example #8
0
 static LoggerConfiguration WithAssemblyName(this LoggerEnrichmentConfiguration enrichmentConfiguration, Assembly assembly)
 {
     return(enrichmentConfiguration.WithProperty("AssemblyName", assembly.GetName().Name));
 }
 public static LoggerConfiguration WithEnvironmentName(this LoggerEnrichmentConfiguration config,
                                                       string environmentName)
 {
     return(config.WithProperty(EnvironmentPropertyName, environmentName));
 }
 public static LoggerConfiguration WithApplicationName(this LoggerEnrichmentConfiguration config,
                                                       string applicationName)
 {
     return(config.WithProperty(ApplicationPropertyName, applicationName));
 }