diff --git a/src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs b/src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs index 557f83e..a26d1e9 100644 --- a/src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs +++ b/src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs @@ -1,6 +1,7 @@ using Microsoft.Data.SqlClient; using Serilog.Configuration; +using Serilog.Core; using Serilog.Events; namespace Serilog.Sinks.SqlServer; @@ -35,7 +36,7 @@ public static LoggerConfiguration SqlServer( var sink = new SqlServerSink(options); - return loggerConfiguration.Sink(sink, options, options.MinimumLevel); + return loggerConfiguration.Sink(sink, options, options.MinimumLevel, levelSwitch: options.LevelSwitch); } /// @@ -47,6 +48,7 @@ public static LoggerConfiguration SqlServer( /// The schema of the table. Defaults to . /// The minimum log event level required to write an event to the sink. Defaults to . /// Options for the SQL bulk copy operation. Defaults to . + /// The instance, allowing runtime adjustments to the filtering level. /// The logger configuration, allowing method chaining. /// Thrown when is null. /// Thrown when is null or empty. @@ -59,13 +61,16 @@ public static LoggerConfiguration SqlServer( string tableName = MappingDefaults.TableName, string tableSchema = MappingDefaults.TableSchema, LogEventLevel minimumLevel = LevelAlias.Minimum, - SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default) + SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default, + LoggingLevelSwitch? levelSwitch = null + ) { if (loggerConfiguration is null) throw new ArgumentNullException(nameof(loggerConfiguration)); if (string.IsNullOrEmpty(connectionString)) - throw new ArgumentException($"'{nameof(connectionString)}' cannot be null or empty.", nameof(connectionString)); + throw new ArgumentException($"'{nameof(connectionString)}' cannot be null or empty.", + nameof(connectionString)); return SqlServer(loggerConfiguration, sinkOptions => { @@ -73,6 +78,7 @@ public static LoggerConfiguration SqlServer( sinkOptions.TableName = tableName; sinkOptions.TableSchema = tableSchema; sinkOptions.MinimumLevel = minimumLevel; + sinkOptions.LevelSwitch = levelSwitch; sinkOptions.BulkCopyOptions = bulkCopyOptions; }); } diff --git a/src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs b/src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs index e65c065..152b204 100644 --- a/src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs +++ b/src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs @@ -1,6 +1,7 @@ using Microsoft.Data.SqlClient; using Serilog.Configuration; +using Serilog.Core; using Serilog.Events; namespace Serilog.Sinks.SqlServer; @@ -20,6 +21,16 @@ public class SqlServerSinkOptions : BatchingOptions /// The minimum log event level. Defaults to . public LogEventLevel MinimumLevel { get; set; } = LevelAlias.Minimum; + /// + /// Gets or sets the that dynamically controls the log event level + /// required to write an event to the sink. + /// + /// + /// The instance, allowing runtime adjustments to the filtering level. + /// If null, the level is determined by the property. + /// + public LoggingLevelSwitch? LevelSwitch { get; set; } + /// /// Gets or sets the connection string to the SQL Server database. ///