Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Serilog.Sinks.SqlServer/LoggerConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Data.SqlClient;

using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Sinks.SqlServer;
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -47,6 +48,7 @@ public static LoggerConfiguration SqlServer(
/// <param name="tableSchema">The schema of the table. Defaults to <see cref="MappingDefaults.TableSchema"/>.</param>
/// <param name="minimumLevel">The minimum log event level required to write an event to the sink. Defaults to <see cref="LevelAlias.Minimum"/>.</param>
/// <param name="bulkCopyOptions">Options for the SQL bulk copy operation. Defaults to <see cref="SqlBulkCopyOptions.Default"/>.</param>
/// <param name="levelSwitch">The <see cref="LoggingLevelSwitch"/> instance, allowing runtime adjustments to the filtering level.</param>
/// <returns>The logger configuration, allowing method chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="loggerConfiguration"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="connectionString"/> is null or empty.</exception>
Expand All @@ -59,20 +61,24 @@ 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 =>
{
sinkOptions.ConnectionString = connectionString;
sinkOptions.TableName = tableName;
sinkOptions.TableSchema = tableSchema;
sinkOptions.MinimumLevel = minimumLevel;
sinkOptions.LevelSwitch = levelSwitch;
sinkOptions.BulkCopyOptions = bulkCopyOptions;
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Data.SqlClient;

using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;

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

/// <summary>
/// Gets or sets the <see cref="LoggingLevelSwitch"/> that dynamically controls the log event level
/// required to write an event to the sink.
/// </summary>
/// <value>
/// The <see cref="LoggingLevelSwitch"/> instance, allowing runtime adjustments to the filtering level.
/// If null, the level is determined by the <see cref="MinimumLevel"/> property.
/// </value>
public LoggingLevelSwitch? LevelSwitch { get; set; }

/// <summary>
/// Gets or sets the connection string to the SQL Server database.
/// </summary>
Expand Down