LoggerFilterConfiguration

Namespace: Serilog
We found 10 examples in language CSharp for this search. You will see 51 fragments of code.
        public static LoggerFilterConfiguration Filter(this LoggerConfiguration @this)
        {
            return new LoggerFilterConfiguration(@this);
        }


        public void Configure(LoggerConfiguration loggerConfiguration)
        {
            ApplyMinimumLevel(loggerConfiguration);
            ApplyEnrichment(loggerConfiguration);
            ApplyFilters(loggerConfiguration);
            ApplySinks(loggerConfiguration);
        }        

        void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
        {
            var minimumLevelDirective = _configuration.GetSection("MinimumLevel");

            var defaultMinLevelDirective = minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
            if (defaultMinLevelDirective.Value != null)
            {
                ApplyMinimumLevel(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
            }

            foreach (var overrideDirective in minimumLevelDirective.GetSection("Override").GetChildren())
            {
                ApplyMinimumLevel(overrideDirective, (configuration, levelSwitch) => configuration.Override(overrideDirective.Key, levelSwitch));
            }

            void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLevelConfiguration, LoggingLevelSwitch> applyConfigAction)
            {
                if (!Enum.TryParse(directive.Value, out LogEventLevel minimumLevel))
                    throw new InvalidOperationException($"The value {directive.Value} is not a valid Serilog level.");

                var levelSwitch = new LoggingLevelSwitch(minimumLevel);
                applyConfigAction(loggerConfiguration.MinimumLevel, levelSwitch);

                ChangeToken.OnChange(
                    directive.GetReloadToken,
                    () =>
                    {
                        if (Enum.TryParse(directive.Value, out minimumLevel))
                            levelSwitch.MinimumLevel = minimumLevel;
                        else
                            SelfLog.WriteLine($"The value {directive.Value} is not a valid Serilog level.");
                    });
            }
        }

        void ApplyFilters(LoggerConfiguration loggerConfiguration)
        {
            var filterDirective = _configuration.GetSection("Filter");
            if (filterDirective != null)
            {
                var methodCalls = GetMethodCalls(filterDirective);
                CallConfigurationMethods(methodCalls, FindFilterConfigurationMethods(_configurationAssemblies), loggerConfiguration.Filter);
            }
        }

        void ApplySinks(LoggerConfiguration loggerConfiguration)
        {
            var writeToDirective = _configuration.GetSection("WriteTo");
            if (writeToDirective != null)
            {
                var methodCalls = GetMethodCalls(writeToDirective);
                CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerConfiguration.WriteTo);
            }
        }

        void IConfigurationReader.ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration)
        {
            var methodCalls = GetMethodCalls(_configuration);
            CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerSinkConfiguration);
        }

        void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
        {
            var enrichDirective = _configuration.GetSection("Enrich");
            if (enrichDirective != null)
            {
                var methodCalls = GetMethodCalls(enrichDirective);
                CallConfigurationMethods(methodCalls, FindEventEnricherConfigurationMethods(_configurationAssemblies), loggerConfiguration.Enrich);
            }

            var propertiesDirective = _configuration.GetSection("Properties");
            if (propertiesDirective != null)
            {
                foreach (var enrichProperyDirective in propertiesDirective.GetChildren())
                {
                    loggerConfiguration.Enrich.WithProperty(enrichProperyDirective.Key, enrichProperyDirective.Value);
                }
            }
        }


        // Pass-through calls to various Serilog config methods which are implemented as instance methods rather than extension methods.
        // ConfigurationReader adds those to the already discovered extension methods so they can be invoked as well.
        
        // ======== WriteTo...
        static LoggerConfiguration Sink(
            LoggerSinkConfiguration loggerSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerSinkConfiguration loggerSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // ======== AuditTo...
        static LoggerConfiguration Sink(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // .Filter...
        // =======
        // TODO: add overload for array argument (ILogEventEnricher[])
        // expose `With(params ILogEventFilter[] filters)` as if it was `With(ILogEventFilter filter)`
        static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter) => loggerFilterConfiguration.With(filter);

        // .Destructure...
        // ============
        // TODO: add overload for array argument (IDestructuringPolicy[])
        // expose `With(params IDestructuringPolicy[] destructuringPolicies)` as if it was `With(IDestructuringPolicy policy)`
        static LoggerConfiguration With(LoggerDestructuringConfiguration loggerDestructuringConfiguration, IDestructuringPolicy policy) => loggerDestructuringConfiguration.With(policy);

        public void Configure(LoggerConfiguration loggerConfiguration)
        {
            ApplyMinimumLevel(loggerConfiguration);
            ApplyEnrichment(loggerConfiguration);
            ApplyFilters(loggerConfiguration);
            ApplySinks(loggerConfiguration);
        }        
        void ApplyMinimumLevel(LoggerConfiguration loggerConfiguration)
        {
            var minimumLevelDirective = _configuration.GetSection("MinimumLevel");

            var defaultMinLevelDirective = minimumLevelDirective.Value != null ? minimumLevelDirective : minimumLevelDirective.GetSection("Default");
            if (defaultMinLevelDirective.Value != null)
            {
                ApplyMinimumLevel(defaultMinLevelDirective, (configuration, levelSwitch) => configuration.ControlledBy(levelSwitch));
            }

            foreach (var overrideDirective in minimumLevelDirective.GetSection("Override").GetChildren())
            {
                ApplyMinimumLevel(overrideDirective, (configuration, levelSwitch) => configuration.Override(overrideDirective.Key, levelSwitch));
            }

            void ApplyMinimumLevel(IConfigurationSection directive, Action<LoggerMinimumLevelConfiguration, LoggingLevelSwitch> applyConfigAction)
            {
                if (!Enum.TryParse(directive.Value, out LogEventLevel minimumLevel))
                    throw new InvalidOperationException($"The value {directive.Value} is not a valid Serilog level.");

                var levelSwitch = new LoggingLevelSwitch(minimumLevel);
                applyConfigAction(loggerConfiguration.MinimumLevel, levelSwitch);

                ChangeToken.OnChange(
                    directive.GetReloadToken,
                    () =>
                    {
                        if (Enum.TryParse(directive.Value, out minimumLevel))
                        {
                            levelSwitch.MinimumLevel = minimumLevel;
                            SelfLog.WriteLine($"MinimumLevel is {directive.Value}.");
                        }
                        else
                            SelfLog.WriteLine($"The value {directive.Value} is not a valid Serilog level.");
                    });
            }
        }

        void ApplyFilters(LoggerConfiguration loggerConfiguration)
        {
            var filterDirective = _configuration.GetSection("Filter");
            if (filterDirective != null)
            {
                var methodCalls = GetMethodCalls(filterDirective);
                CallConfigurationMethods(methodCalls, FindFilterConfigurationMethods(_configurationAssemblies), loggerConfiguration.Filter);
            }
        }

        void ApplySinks(LoggerConfiguration loggerConfiguration)
        {
            var writeToDirective = _configuration.GetSection("WriteTo");
            if (writeToDirective != null)
            {
                var methodCalls = GetMethodCalls(writeToDirective);
                CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerConfiguration.WriteTo);
            }
        }

        void IConfigurationReader.ApplySinks(LoggerSinkConfiguration loggerSinkConfiguration)
        {
            var methodCalls = GetMethodCalls(_configuration);
            CallConfigurationMethods(methodCalls, FindSinkConfigurationMethods(_configurationAssemblies), loggerSinkConfiguration);
        }

        void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
        {
            var enrichDirective = _configuration.GetSection("Enrich");
            if (enrichDirective != null)
            {
                var methodCalls = GetMethodCalls(enrichDirective);
                CallConfigurationMethods(methodCalls, FindEventEnricherConfigurationMethods(_configurationAssemblies), loggerConfiguration.Enrich);
            }

            var propertiesDirective = _configuration.GetSection("Properties");
            if (propertiesDirective != null)
            {
                foreach (var enrichProperyDirective in propertiesDirective.GetChildren())
                {
                    loggerConfiguration.Enrich.WithProperty(enrichProperyDirective.Key, enrichProperyDirective.Value);
                }
            }
        }


        public LoggerConfiguration MinimumLevel(LogEventLevel minimumLevel = LogEventLevel.Verbose)
        {
            configuration.MinimumLevel.Is(minimumLevel);

            return configuration;
        }

        public LoggerConfiguration Overrides(LoggerSourceContextLevelOverrides switches)
        {
            configuration.Filter.ByExcluding(e => EventsBelowCertainLevel(e, switches));
            return configuration;
        }

        public static bool EventsBelowCertainLevel(LogEvent logEvent, LoggerSourceContextLevelOverrides globalSwitches)
        {
            (LogEventLevel defaultLevel, System.Collections.Generic.KeyValuePair<System.Func<LogEvent, bool>, LogEventLevel>[] matchers) = globalSwitches.Current;

            for (int i = 0; i < matchers.Length; i++)
            {
                System.Collections.Generic.KeyValuePair<System.Func<LogEvent, bool>, LogEventLevel> filter = matchers[i];
                if (filter.Key(logEvent))
                {
                    return logEvent.Level < filter.Value;
                }
            }

            return logEvent.Level < defaultLevel;
        }


        /*
        Pass-through calls to various Serilog config methods which are
        implemented as instance methods rather than extension methods.
        ConfigurationReader adds those to the already discovered extension methods
        so they can be invoked as well.
        */

        // ReSharper disable UnusedMember.Local
        // those methods are discovered through reflection by `SurrogateMethodCandidates`
        // ReSharper has no way to see that they are actually used ...

        // .WriteTo...
        // ========
        static LoggerConfiguration Sink(
            LoggerSinkConfiguration loggerSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerSinkConfiguration loggerSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // .AuditTo...
        // ========
        static LoggerConfiguration Sink(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // .Filter...
        // =======
        // TODO: add overload for array argument (ILogEventEnricher[])
        // expose `With(params ILogEventFilter[] filters)` as if it was `With(ILogEventFilter filter)`
        static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter)
            => loggerFilterConfiguration.With(filter);

        // .Destructure...
        // ============
        // TODO: add overload for array argument (IDestructuringPolicy[])
        // expose `With(params IDestructuringPolicy[] destructuringPolicies)` as if it was `With(IDestructuringPolicy policy)`
        static LoggerConfiguration With(LoggerDestructuringConfiguration loggerDestructuringConfiguration, IDestructuringPolicy policy)
            => loggerDestructuringConfiguration.With(policy);


        /*
        Pass-through calls to various Serilog config methods which are
        implemented as instance methods rather than extension methods.
        ConfigurationReader adds those to the already discovered extension methods
        so they can be invoked as well.
        */

        // ReSharper disable UnusedMember.Local
        // those methods are discovered through reflection by `SurrogateMethodCandidates`
        // ReSharper has no way to see that they are actually used ...

        // .WriteTo...
        // ========
        static LoggerConfiguration Sink(
            LoggerSinkConfiguration loggerSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerSinkConfiguration loggerSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => loggerSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // .AuditTo...
        // ========
        static LoggerConfiguration Sink(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

        static LoggerConfiguration Logger(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            Action<LoggerConfiguration> configureLogger,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
            => auditSinkConfiguration.Logger(configureLogger, restrictedToMinimumLevel, levelSwitch);

        // .Filter...
        // =======
        // TODO: add overload for array argument (ILogEventEnricher[])
        // expose `With(params ILogEventFilter[] filters)` as if it was `With(ILogEventFilter filter)`
        static LoggerConfiguration With(LoggerFilterConfiguration loggerFilterConfiguration, ILogEventFilter filter)
            => loggerFilterConfiguration.With(filter);

        // .Destructure...
        // ============
        // TODO: add overload for array argument (IDestructuringPolicy[])
        // expose `With(params IDestructuringPolicy[] destructuringPolicies)` as if it was `With(IDestructuringPolicy policy)`
        static LoggerConfiguration With(LoggerDestructuringConfiguration loggerDestructuringConfiguration, IDestructuringPolicy policy)
            => loggerDestructuringConfiguration.With(policy);

        /// <summary>
        /// Filter log events to include only those that has specified property
        /// </summary>
        /// <param name="configuration">configuration</param>
        /// <param name="propertyName">Name of property</param>
        /// <param name="propertyValue">Expected property value</param>
        /// <returns>LoggerConfiguration</returns>
        public static LoggerConfiguration FilterByProperty(this LoggerFilterConfiguration configuration, string propertyName, string propertyValue)
        {
            return configuration.ByIncludingOnly(Matching.WithProperty(propertyName, propertyValue)); //new ScalarValue(propertyValue)

            //var scalarValue = new ScalarValue(propertyValue);
            //return configuration.ByIncludingOnly(logEvent =>
            //{
            //    if (logEvent.Properties.TryGetValue(propertyName, out var propertyValue) && propertyValue is ScalarValue stValue)
            //        return scalarValue.Equals(stValue);
            //    return false;
            //});
        }

        /// <summary>
        /// Filter log events to include only those that has specified property
        /// </summary>
        /// <param name="configuration">configuration</param>
        /// <param name="propertyName">Name of property</param>
        /// <returns>LoggerConfiguration</returns>
        public static LoggerConfiguration FilterByHasProperty(this LoggerFilterConfiguration configuration, string propertyName)
        {
            return configuration.ByIncludingOnly(Matching.WithProperty(propertyName));

            //return configuration.ByIncludingOnly(logEvent => logEvent.Properties.ContainsKey(propertyName));
        }

        /// <summary>
        /// Filter log events to include only those that has specified property
        /// </summary>
        /// <param name="configuration">configuration</param>
        /// <param name="propertyName">Name of property</param>
        /// <param name="propertyValue">Expected property value</param>
        /// <returns>LoggerConfiguration</returns>
        public static LoggerConfiguration FilterOnlyLogRequests(this LoggerFilterConfiguration configuration)
        {
            return configuration.ByIncludingOnly(logEvent =>
            {
                var propertyName = nameof(LogRequestAttribute);
                var contains = logEvent.Properties.ContainsKey(propertyName); //Matching.WithProperty(propertyName)(logEvent);
                logEvent.RemovePropertyIfPresent(propertyName);
                return contains;
            });
        }
        public static LoggerConfiguration WriteLogRequestsToSqlServer(this LoggerConfiguration loggerConfiguration, string connectionString, string tableName = "RequestLogs")
        {
           
            loggerConfiguration.WriteTo.Logger(subLoggerConfigurations =>
            {
                var options = new ColumnOptions();
                options.Store.Remove(StandardColumn.Properties);
                options.Store.Add(StandardColumn.LogEvent);
                options.LogEvent.ExcludeAdditionalProperties = true;
                options.LogEvent.ExcludeStandardColumns = true;
                //options.TimeStamp.ConvertToUtc = true;
                options.AdditionalColumns = new Collection<SqlColumn>
                {
                    new SqlColumn {ColumnName = "RequestPath", DataType = SqlDbType.NVarChar, DataLength = 500, AllowNull = false},
                    new SqlColumn {ColumnName = "RequestMethod", DataType = SqlDbType.NVarChar, DataLength = 10, AllowNull = false},
                    new SqlColumn {ColumnName = "StatusCode", DataType = SqlDbType.Int, AllowNull = false},
                    new SqlColumn {ColumnName = "Elapsed", DataType = SqlDbType.Float, AllowNull = false},
                    new SqlColumn {ColumnName = "IpAddress", DataType = SqlDbType.NVarChar, DataLength = 100, AllowNull = false},
                    new SqlColumn {ColumnName = "UserKey", DataType = SqlDbType.NVarChar, DataLength = 100, AllowNull = true},
                };

                subLoggerConfigurations
                    .Filter.FilterOnlyLogRequests()
                    //.Enrich.FromLogContext()
                    .WriteTo.MSSqlServer(
                        connectionString: connectionString,
                        tableName: tableName,
                        restrictedToMinimumLevel: LogEventLevel.Information,
                        columnOptions: options,
                        autoCreateSqlTable: true);
            });

            return loggerConfiguration;
        }
        public static LoggerConfiguration WriteLogsToSqlServer(this LoggerConfiguration loggerConfiguration, string connectionString, string tableName = "Logs")
        {
            loggerConfiguration
                            .WriteTo.Logger(subLoggerConfiguration =>
                            {
                                var options = new ColumnOptions();
                                options.Store.Remove(StandardColumn.Properties);
                                options.Store.Add(StandardColumn.LogEvent);

                                subLoggerConfiguration
                                    .Filter.ByExcluding(Matching.WithProperty(nameof(LogRequestAttribute)))
                                    .WriteTo.MSSqlServer(
                                        connectionString: connectionString,
                                        tableName: tableName,
                                        restrictedToMinimumLevel: LogEventLevel.Warning,
                                        columnOptions: options,
                                        autoCreateSqlTable: true);
                            });
            return loggerConfiguration;
        }


        internal static LoggerConfiguration Sink(
            LoggerSinkConfiguration loggerSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
        {
            return loggerSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
        }

        internal static LoggerConfiguration Sink(
            LoggerAuditSinkConfiguration auditSinkConfiguration,
            ILogEventSink sink,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch = null)
        {
            return auditSinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);
        }

        internal static LoggerConfiguration With(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration, ILogEventEnricher enricher)
        {
            return loggerEnrichmentConfiguration.With(enricher);
        }

        internal static LoggerConfiguration FromLogContext(LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
        {
            return loggerEnrichmentConfiguration.FromLogContext();
        }

        internal static LoggerConfiguration With(LoggerDestructuringConfiguration loggerDestructuringConfiguration,
            IDestructuringPolicy policy)
        {
            return loggerDestructuringConfiguration.With(policy);
        }

        internal static LoggerConfiguration AsScalar(LoggerDestructuringConfiguration loggerDestructuringConfiguration,
            Type scalarType)
        {
            return loggerDestructuringConfiguration.AsScalar(scalarType);
        }

        public static void Configure(LoggerConfiguration loggerConfiguration, IConfiguration configuration, GitVersionAttribute? gitVersion)
        {
            Configure(loggerConfiguration, configuration, configuration.GetSection("Serilog").Get<SerilogConfiguration>(), gitVersion);
        }

        public static void Configure(LoggerConfiguration loggerConfiguration, IConfiguration configuration, SerilogConfiguration? serilogConfiguration, GitVersionAttribute? gitVersion)
        {
            serilogConfiguration ??= new SerilogConfiguration();

            loggerConfiguration
                .Enrich.FromLogContext()
                .Enrich.WithMachineName()
                .Enrich.WithEnvironmentUserName();

            if (serilogConfiguration.RequestLogging.Enabled)
            {
                loggerConfiguration.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning);
            }

            if (gitVersion != null)
            {
                loggerConfiguration
                    .Enrich.WithProperty("GitBranch", gitVersion.Branch)
                    .Enrich.WithProperty("GitCommit", gitVersion.Commit);
            }

            if (configuration != null)
            {
                loggerConfiguration.ReadFrom.Configuration(configuration);
            }

            SetupConsole(loggerConfiguration, serilogConfiguration.Console);
            SetupElasticSearch(loggerConfiguration, serilogConfiguration.ElasticSearch);
            SetupFileLog(loggerConfiguration, serilogConfiguration.FileLog);

            SetupSelfLog(serilogConfiguration.SelfLog);
        }

        private static void SetupSelfLog(SelfLogConfiguration selfLogConfiguration)
        {
            if (!selfLogConfiguration.Enabled)
            {
                return;
            }

            var selfLog = File.CreateText(selfLogConfiguration.Path);
            selfLog.WriteLine($"----------- INITIALIZED SELFLOG ----------- {DateTimeOffset.UtcNow} -----------");
            selfLog.AutoFlush = true;
            Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(selfLog));
        }

        private static void SetupConsole(LoggerConfiguration loggerConfiguration, ConsoleConfiguration consoleConfiguration)
        {
            if (!consoleConfiguration.Enabled)
            {
                return;
            }

            loggerConfiguration
                .WriteTo.Logger(c => c.Filter.LogLevelFromConfiguration(consoleConfiguration.MinimumLevel)
                    .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <{SourceContext}>{NewLine}{Exception}"));
        }

        private static void SetupElasticSearch(LoggerConfiguration loggerConfiguration, ElasticSearchConfiguration elasticSearchConfiguration)
        {
            if (!elasticSearchConfiguration.Enabled)
            {
                return;
            }

            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            void ValidateConfiguration(string property, string? value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception($"Serilog.ElasticSearch.Enabled is true but Serilog.ElasticSearch.{property} is not set");
                }
            }

            ValidateConfiguration(nameof(ElasticSearchConfiguration.BaseUrl), elasticSearchConfiguration.BaseUrl);
            ValidateConfiguration(nameof(ElasticSearchConfiguration.Username), elasticSearchConfiguration.Username);
            ValidateConfiguration(nameof(ElasticSearchConfiguration.Password), elasticSearchConfiguration.Password);
            ValidateConfiguration(nameof(ElasticSearchConfiguration.BaseUrl), elasticSearchConfiguration.BaseUrl);
            ValidateConfiguration(nameof(ElasticSearchConfiguration.IndexFormat), elasticSearchConfiguration.IndexFormat);

            var uri = string.Format(elasticSearchConfiguration.BaseUrl!, elasticSearchConfiguration.Username!,
                elasticSearchConfiguration.Password!);

            loggerConfiguration
                .WriteTo.Logger(c => c
                    .Filter.LogLevelFromConfiguration(elasticSearchConfiguration.MinimumLevel)
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(uri))
                    {
                        AutoRegisterTemplate = true,
                        AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                        IndexFormat = elasticSearchConfiguration.IndexFormat,
                        // BufferBaseFilename = "./logs/buffer"
                    }));
        }

        private static void SetupFileLog(LoggerConfiguration loggerConfiguration, FileLogConfiguration fileLogConfiguration)
        {
            if (!fileLogConfiguration.Enabled)
            {
                return;
            }

            if (string.IsNullOrEmpty(fileLogConfiguration.Path))
            {
                throw new Exception("Serilog.ElasticSearch.Enabled is true but Serilog.FileLog.Path is not set");
            }

            loggerConfiguration.WriteTo.Logger(c => c
                .Filter.LogLevelFromConfiguration(fileLogConfiguration.MinimumLevel)
                .WriteTo.File(
                    fileLogConfiguration.Path,
                    rollingInterval: RollingInterval.Day,
                    outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}]{NewLine}{Message:lj}{NewLine}{Exception}"));
        }

Serilog.Configuration.LoggerFilterConfiguration : Object

Methods :

public LoggerConfiguration With(ILogEventFilter[] filters = )
public LoggerConfiguration With()
public LoggerConfiguration ByExcluding(Func<LogEvent, Boolean> exclusionPredicate = )
public LoggerConfiguration ByIncludingOnly(Func<LogEvent, Boolean> inclusionPredicate = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods