Skip to content

Commit 19c6a39

Browse files
authored
Merge pull request #6 from gumbarros/main
Add support for dynamic log level control using `LoggingLevelSwitch`
2 parents 0460740 + 8c887f5 commit 19c6a39

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Data.SqlClient;
22

33
using Serilog.Configuration;
4+
using Serilog.Core;
45
using Serilog.Events;
56

67
namespace Serilog.Sinks.SqlServer;
@@ -35,7 +36,7 @@ public static LoggerConfiguration SqlServer(
3536

3637
var sink = new SqlServerSink(options);
3738

38-
return loggerConfiguration.Sink(sink, options, options.MinimumLevel);
39+
return loggerConfiguration.Sink(sink, options, options.MinimumLevel, levelSwitch: options.LevelSwitch);
3940
}
4041

4142
/// <summary>
@@ -47,6 +48,7 @@ public static LoggerConfiguration SqlServer(
4748
/// <param name="tableSchema">The schema of the table. Defaults to <see cref="MappingDefaults.TableSchema"/>.</param>
4849
/// <param name="minimumLevel">The minimum log event level required to write an event to the sink. Defaults to <see cref="LevelAlias.Minimum"/>.</param>
4950
/// <param name="bulkCopyOptions">Options for the SQL bulk copy operation. Defaults to <see cref="SqlBulkCopyOptions.Default"/>.</param>
51+
/// <param name="levelSwitch">The <see cref="LoggingLevelSwitch"/> instance, allowing runtime adjustments to the filtering level.</param>
5052
/// <returns>The logger configuration, allowing method chaining.</returns>
5153
/// <exception cref="ArgumentNullException">Thrown when <paramref name="loggerConfiguration"/> is null.</exception>
5254
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString"/> is null or empty.</exception>
@@ -59,20 +61,24 @@ public static LoggerConfiguration SqlServer(
5961
string tableName = MappingDefaults.TableName,
6062
string tableSchema = MappingDefaults.TableSchema,
6163
LogEventLevel minimumLevel = LevelAlias.Minimum,
62-
SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default)
64+
SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default,
65+
LoggingLevelSwitch? levelSwitch = null
66+
)
6367
{
6468
if (loggerConfiguration is null)
6569
throw new ArgumentNullException(nameof(loggerConfiguration));
6670

6771
if (string.IsNullOrEmpty(connectionString))
68-
throw new ArgumentException($"'{nameof(connectionString)}' cannot be null or empty.", nameof(connectionString));
72+
throw new ArgumentException($"'{nameof(connectionString)}' cannot be null or empty.",
73+
nameof(connectionString));
6974

7075
return SqlServer(loggerConfiguration, sinkOptions =>
7176
{
7277
sinkOptions.ConnectionString = connectionString;
7378
sinkOptions.TableName = tableName;
7479
sinkOptions.TableSchema = tableSchema;
7580
sinkOptions.MinimumLevel = minimumLevel;
81+
sinkOptions.LevelSwitch = levelSwitch;
7682
sinkOptions.BulkCopyOptions = bulkCopyOptions;
7783
});
7884
}

src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Data.SqlClient;
22

33
using Serilog.Configuration;
4+
using Serilog.Core;
45
using Serilog.Events;
56

67
namespace Serilog.Sinks.SqlServer;
@@ -20,6 +21,16 @@ public class SqlServerSinkOptions : BatchingOptions
2021
/// <value>The minimum log event level. Defaults to <see cref="LevelAlias.Minimum"/>.</value>
2122
public LogEventLevel MinimumLevel { get; set; } = LevelAlias.Minimum;
2223

24+
/// <summary>
25+
/// Gets or sets the <see cref="LoggingLevelSwitch"/> that dynamically controls the log event level
26+
/// required to write an event to the sink.
27+
/// </summary>
28+
/// <value>
29+
/// The <see cref="LoggingLevelSwitch"/> instance, allowing runtime adjustments to the filtering level.
30+
/// If null, the level is determined by the <see cref="MinimumLevel"/> property.
31+
/// </value>
32+
public LoggingLevelSwitch? LevelSwitch { get; set; }
33+
2334
/// <summary>
2435
/// Gets or sets the connection string to the SQL Server database.
2536
/// </summary>

0 commit comments

Comments
 (0)