Skip to content

Commit bcb79d2

Browse files
committed
Moved levelSwitch to sink options
Avoids making the already bloated config extensions method interface even more bloated (although levelSwitch is connected parameter restrictedToMinimumLevel which speaks for having levelSwitch next to it).
1 parent b79e7dc commit bcb79d2

File tree

6 files changed

+21
-59
lines changed

6 files changed

+21
-59
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ This setting is not used by the audit sink as it writes each event immediately a
281281
A Flag to eagerly write a batch to the database containing the first received event regardless of `BatchPostingLimit` or `BatchPeriod`. It defaults to `true`.
282282
This setting is not used by the audit sink as it writes each event immediately and not in a batched manner.
283283

284+
### LevelSwitch
285+
286+
A switch allowing the pass-through minimum level to be changed at runtime. If this is set, the parameter `restrictedToMinimumLevel` in the [sink configuration method](#sink-configuration) is ignored.
284287

285288
## ColumnOptions Object
286289

sample/CustomLogEventFormatterDemo/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public static void Main()
4242
{
4343
TableName = _tableName,
4444
SchemaName = _schemaName,
45-
AutoCreateSqlTable = true
45+
AutoCreateSqlTable = true,
46+
LevelSwitch = levelSwitch
4647
},
4748
appConfiguration: null,
4849
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
4950
formatProvider: null,
5051
columnOptions: options,
5152
columnOptionsSection: null,
52-
logEventFormatter: customFormatter,
53-
levelSwitch: levelSwitch)
53+
logEventFormatter: customFormatter)
5454
.CreateLogger();
5555

5656
try

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/Hybrid/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,11 @@ public static LoggerConfiguration MSSqlServer(
105105
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
106106
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
107107
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
108-
/// <param name="restrictedToMinimumLevel">The minimum level for events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
108+
/// <param name="restrictedToMinimumLevel">The minimum level for events passed through the sink. Ignored when LevelSwitch in <paramref name="sinkOptions"/> is specified.</param>
109109
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
110110
/// <param name="columnOptions">An externally-modified group of column settings</param>
111111
/// <param name="columnOptionsSection">A config section defining various column settings</param>
112112
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
113-
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
114113
/// <returns>Logger configuration, allowing configuration to continue.</returns>
115114
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
116115
public static LoggerConfiguration MSSqlServer(
@@ -123,15 +122,13 @@ public static LoggerConfiguration MSSqlServer(
123122
IFormatProvider formatProvider = null,
124123
ColumnOptions columnOptions = null,
125124
IConfigurationSection columnOptionsSection = null,
126-
ITextFormatter logEventFormatter = null,
127-
LoggingLevelSwitch levelSwitch = null) =>
125+
ITextFormatter logEventFormatter = null) =>
128126
loggerConfiguration.MSSqlServerInternal(
129127
connectionString: connectionString,
130128
sinkOptions: sinkOptions,
131129
sinkOptionsSection: sinkOptionsSection,
132130
appConfiguration: appConfiguration,
133131
restrictedToMinimumLevel: restrictedToMinimumLevel,
134-
levelSwitch: levelSwitch,
135132
formatProvider: formatProvider,
136133
columnOptions: columnOptions,
137134
columnOptionsSection: columnOptionsSection,
@@ -149,7 +146,6 @@ internal static LoggerConfiguration MSSqlServerInternal(
149146
IConfigurationSection sinkOptionsSection,
150147
IConfiguration appConfiguration,
151148
LogEventLevel restrictedToMinimumLevel,
152-
LoggingLevelSwitch levelSwitch,
153149
IFormatProvider formatProvider,
154150
ColumnOptions columnOptions,
155151
IConfigurationSection columnOptionsSection,
@@ -169,7 +165,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
169165

170166
var periodicBatchingSink = batchingSinkFactory.Create(sink, sinkOptions);
171167

172-
return loggerConfiguration.Sink(periodicBatchingSink, restrictedToMinimumLevel, levelSwitch);
168+
return loggerConfiguration.Sink(periodicBatchingSink, restrictedToMinimumLevel, sinkOptions?.LevelSwitch);
173169
}
174170

175171
/// <summary>
@@ -230,12 +226,11 @@ public static LoggerConfiguration MSSqlServer(
230226
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
231227
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
232228
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
233-
/// <param name="restrictedToMinimumLevel">The minimum level for events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
229+
/// <param name="restrictedToMinimumLevel">The minimum level for events passed through the sink. Ignored when LevelSwitch in <paramref name="sinkOptions"/> is specified.</param>
234230
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
235231
/// <param name="columnOptions">An externally-modified group of column settings</param>
236232
/// <param name="columnOptionsSection">A config section defining various column settings</param>
237233
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
238-
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
239234
/// <returns>Logger configuration, allowing configuration to continue.</returns>
240235
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
241236
public static LoggerConfiguration MSSqlServer(
@@ -248,15 +243,13 @@ public static LoggerConfiguration MSSqlServer(
248243
IFormatProvider formatProvider = null,
249244
ColumnOptions columnOptions = null,
250245
IConfigurationSection columnOptionsSection = null,
251-
ITextFormatter logEventFormatter = null,
252-
LoggingLevelSwitch levelSwitch = null) =>
246+
ITextFormatter logEventFormatter = null) =>
253247
loggerAuditSinkConfiguration.MSSqlServerInternal(
254248
connectionString: connectionString,
255249
sinkOptions: sinkOptions,
256250
sinkOptionsSection: sinkOptionsSection,
257251
appConfiguration: appConfiguration,
258252
restrictedToMinimumLevel: restrictedToMinimumLevel,
259-
levelSwitch: levelSwitch,
260253
formatProvider: formatProvider,
261254
columnOptions: columnOptions,
262255
columnOptionsSection: columnOptionsSection,
@@ -273,7 +266,6 @@ internal static LoggerConfiguration MSSqlServerInternal(
273266
IConfigurationSection sinkOptionsSection,
274267
IConfiguration appConfiguration,
275268
LogEventLevel restrictedToMinimumLevel,
276-
LoggingLevelSwitch levelSwitch,
277269
IFormatProvider formatProvider,
278270
ColumnOptions columnOptions,
279271
IConfigurationSection columnOptionsSection,
@@ -290,7 +282,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
290282

291283
var auditSink = auditSinkFactory.Create(connectionString, sinkOptions, formatProvider, columnOptions, logEventFormatter);
292284

293-
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel, levelSwitch);
285+
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel, sinkOptions?.LevelSwitch);
294286
}
295287

296288
private static void ReadConfiguration(

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSinkOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Serilog.Core;
23

34
namespace Serilog.Sinks.MSSqlServer
45
{
@@ -66,5 +67,10 @@ internal MSSqlServerSinkOptions(
6667
/// Flag to eagerly emit a batch containing the first received event (default: true)
6768
/// </summary>
6869
public bool EagerlyEmitFirstEvent { get; set; }
70+
71+
/// <summary>
72+
/// A switch allowing the pass-through minimum level to be changed at runtime
73+
/// </summary>
74+
public LoggingLevelSwitch LevelSwitch { get; set; }
6975
}
7076
}

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Extensions/Hybrid/ConfigurationExtensionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ public void LogLevelSwitchIsApplied()
169169
sinkOptions: new MSSqlServerSinkOptions
170170
{
171171
TableName = DatabaseFixture.LogTableName,
172-
AutoCreateSqlTable = true
172+
AutoCreateSqlTable = true,
173+
LevelSwitch = levelSwitch,
173174
},
174175
columnOptionsSection: columnOptionsSection,
175-
formatProvider: CultureInfo.InvariantCulture,
176-
levelSwitch: levelSwitch)
176+
formatProvider: CultureInfo.InvariantCulture)
177177
.CreateLogger();
178178

179179
Log.Information(message1);

0 commit comments

Comments
 (0)