Skip to content

Commit 56f624b

Browse files
committed
* Separate SinkOptions methods also for audit sink (config extensions and ctor).
* Implemented reading SinkOptions for config systems Microsoft.Extensions.Configuration and System.Configuration. * Added some basic integration tests for reading SinkOptions from config.
1 parent fbb0852 commit 56f624b

24 files changed

+622
-227
lines changed

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

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
4444
/// https://gist.github.com/mivano/10429656
4545
/// or use the autoCreateSqlTable option.
4646
///
47-
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead of separate parameters.
47+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead.
4848
///
4949
/// </summary>
5050
/// <param name="loggerConfiguration">The logger configuration.</param>
@@ -74,9 +74,12 @@ public static LoggerConfiguration MSSqlServer(
7474
bool autoCreateSqlTable = false,
7575
ColumnOptions columnOptions = null,
7676
IConfigurationSection columnOptionsSection = null,
77-
string schemaName = "dbo",
77+
string schemaName = MSSqlServerSink.DefaultSchemaName,
7878
ITextFormatter logEventFormatter = null)
7979
{
80+
// Do not add new parameters here. This interface is considered legacy and will be deprecated in the future.
81+
// For adding new input parameters use the SinkOptions class and the method overload that accepts SinkOptions.
82+
8083
var sinkOptions = new SinkOptions(tableName, batchPostingLimit, period, autoCreateSqlTable, schemaName);
8184

8285
return loggerConfiguration.MSSqlServer(
@@ -161,7 +164,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
161164
{
162165
connectionString = applySystemConfiguration.GetConnectionString(connectionString);
163166
columnOptions = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, columnOptions);
164-
// TODO get sink options from System.Config
167+
sinkOptions = applySystemConfiguration.ConfigureSinkOptions(serviceConfigSection, sinkOptions);
165168

166169
if (appConfiguration != null || columnOptionsSection != null || sinkOptionsSection != null)
167170
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
@@ -189,6 +192,9 @@ internal static LoggerConfiguration MSSqlServerInternal(
189192

190193
/// <summary>
191194
/// Adds a sink that writes log events to a table in a MSSqlServer database.
195+
///
196+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead.
197+
///
192198
/// </summary>
193199
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
194200
/// <param name="connectionString">The connection string to the database where to store the events.</param>
@@ -201,8 +207,6 @@ internal static LoggerConfiguration MSSqlServerInternal(
201207
/// <param name="columnOptionsSection">A config section defining various column settings</param>
202208
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
203209
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
204-
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
205-
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
206210
/// <returns>Logger configuration, allowing configuration to continue.</returns>
207211
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
208212
public static LoggerConfiguration MSSqlServer(
@@ -215,23 +219,62 @@ public static LoggerConfiguration MSSqlServer(
215219
bool autoCreateSqlTable = false,
216220
ColumnOptions columnOptions = null,
217221
IConfigurationSection columnOptionsSection = null,
218-
string schemaName = "dbo",
219-
ITextFormatter logEventFormatter = null,
220-
SinkOptions sinkOptions = null,
221-
IConfigurationSection sinkOptionsSection = null) =>
222-
loggerAuditSinkConfiguration.MSSqlServerInternal(
222+
string schemaName = MSSqlServerSink.DefaultSchemaName,
223+
ITextFormatter logEventFormatter = null)
224+
{
225+
// Do not add new parameters here. This interface is considered legacy and will be deprecated in the future.
226+
// For adding new input parameters use the SinkOptions class and the method overload that accepts SinkOptions.
227+
228+
var sinkOptions = new SinkOptions(tableName, null, null, autoCreateSqlTable, schemaName);
229+
230+
return loggerAuditSinkConfiguration.MSSqlServer(
223231
connectionString: connectionString,
224-
tableName: tableName,
232+
sinkOptions: sinkOptions,
225233
appConfiguration: appConfiguration,
226234
restrictedToMinimumLevel: restrictedToMinimumLevel,
227235
formatProvider: formatProvider,
228-
autoCreateSqlTable: autoCreateSqlTable,
229236
columnOptions: columnOptions,
230237
columnOptionsSection: columnOptionsSection,
231-
schemaName: schemaName,
232238
logEventFormatter: logEventFormatter,
239+
sinkOptionsSection: null);
240+
}
241+
242+
/// <summary>
243+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
244+
/// </summary>
245+
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
246+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
247+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
248+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
249+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
250+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
251+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
252+
/// <param name="columnOptions">An externally-modified group of column settings</param>
253+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
254+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
255+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
256+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
257+
public static LoggerConfiguration MSSqlServer(
258+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
259+
string connectionString,
260+
SinkOptions sinkOptions = null,
261+
IConfigurationSection sinkOptionsSection = null,
262+
IConfiguration appConfiguration = null,
263+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
264+
IFormatProvider formatProvider = null,
265+
ColumnOptions columnOptions = null,
266+
IConfigurationSection columnOptionsSection = null,
267+
ITextFormatter logEventFormatter = null) =>
268+
loggerAuditSinkConfiguration.MSSqlServerInternal(
269+
connectionString: connectionString,
233270
sinkOptions: sinkOptions,
234271
sinkOptionsSection: sinkOptionsSection,
272+
appConfiguration: appConfiguration,
273+
restrictedToMinimumLevel: restrictedToMinimumLevel,
274+
formatProvider: formatProvider,
275+
columnOptions: columnOptions,
276+
columnOptionsSection: columnOptionsSection,
277+
logEventFormatter: logEventFormatter,
235278
applySystemConfiguration: new ApplySystemConfiguration(),
236279
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
237280
auditSinkFactory: new MSSqlServerAuditSinkFactory());
@@ -240,56 +283,51 @@ public static LoggerConfiguration MSSqlServer(
240283
internal static LoggerConfiguration MSSqlServerInternal(
241284
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
242285
string connectionString,
243-
string tableName,
286+
SinkOptions sinkOptions = null,
287+
IConfigurationSection sinkOptionsSection = null,
244288
IConfiguration appConfiguration = null,
245289
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
246290
IFormatProvider formatProvider = null,
247-
bool autoCreateSqlTable = false,
248291
ColumnOptions columnOptions = null,
249292
IConfigurationSection columnOptionsSection = null,
250-
string schemaName = "dbo",
251293
ITextFormatter logEventFormatter = null,
252-
SinkOptions sinkOptions = null,
253-
IConfigurationSection sinkOptionsSection = null,
254294
IApplySystemConfiguration applySystemConfiguration = null,
255295
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
256296
IMSSqlServerAuditSinkFactory auditSinkFactory = null)
257297
{
258298
if (loggerAuditSinkConfiguration == null)
259299
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
260300

261-
var colOpts = columnOptions ?? new ColumnOptions();
262-
var connStr = connectionString;
263-
var sinkOpts = sinkOptions ?? new SinkOptions();
301+
sinkOptions = sinkOptions ?? new SinkOptions();
302+
columnOptions = columnOptions ?? new ColumnOptions();
264303

265304
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
266305
if (serviceConfigSection != null)
267306
{
268-
connStr = applySystemConfiguration.GetConnectionString(connStr);
269-
colOpts = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
270-
// TODO get sink options from config
307+
connectionString = applySystemConfiguration.GetConnectionString(connectionString);
308+
columnOptions = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, columnOptions);
309+
sinkOptions = applySystemConfiguration.ConfigureSinkOptions(serviceConfigSection, sinkOptions);
271310

272311
if (appConfiguration != null || columnOptionsSection != null || sinkOptionsSection != null)
273312
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
274313
}
275314

276315
if (appConfiguration != null)
277316
{
278-
connStr = applyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
317+
connectionString = applyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
279318
}
280319

281320
if (columnOptionsSection != null)
282321
{
283-
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
322+
columnOptions = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
284323
}
285324

286325
if (sinkOptionsSection != null)
287326
{
288-
sinkOpts = applyMicrosoftExtensionsConfiguration.ConfigureSinkOptions(sinkOpts, sinkOptionsSection);
327+
sinkOptions = applyMicrosoftExtensionsConfiguration.ConfigureSinkOptions(sinkOptions, sinkOptionsSection);
289328
}
290329

291-
var auditSink = auditSinkFactory.Create(connStr, tableName, formatProvider, autoCreateSqlTable,
292-
colOpts, schemaName, logEventFormatter, sinkOpts);
330+
var auditSink = auditSinkFactory.Create(connectionString, sinkOptions, formatProvider, columnOptions, logEventFormatter);
293331

294332
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
295333
}

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

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
3636
/// https://gist.github.com/mivano/10429656
3737
/// or use the autoCreateSqlTable option.
3838
///
39-
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead of separate parameters.
39+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead.
4040
///
4141
/// </summary>
4242
/// <param name="loggerConfiguration">The logger configuration.</param>
@@ -66,9 +66,12 @@ public static LoggerConfiguration MSSqlServer(
6666
bool autoCreateSqlTable = false,
6767
ColumnOptions columnOptions = null,
6868
IConfigurationSection columnOptionsSection = null,
69-
string schemaName = "dbo",
69+
string schemaName = MSSqlServerSink.DefaultSchemaName,
7070
ITextFormatter logEventFormatter = null)
7171
{
72+
// Do not add new parameters here. This interface is considered legacy and will be deprecated in the future.
73+
// For adding new input parameters use the SinkOptions class and the method overload that accepts SinkOptions.
74+
7275
var sinkOptions = new SinkOptions(tableName, batchPostingLimit, period, autoCreateSqlTable, schemaName);
7376

7477
return loggerConfiguration.MSSqlServer(
@@ -104,7 +107,7 @@ public static LoggerConfiguration MSSqlServer(
104107
public static LoggerConfiguration MSSqlServer(
105108
this LoggerSinkConfiguration loggerConfiguration,
106109
string connectionString,
107-
SinkOptions sinkOptions,
110+
SinkOptions sinkOptions = null,
108111
IConfiguration appConfiguration = null,
109112
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
110113
IFormatProvider formatProvider = null,
@@ -132,6 +135,9 @@ public static LoggerConfiguration MSSqlServer(
132135

133136
/// <summary>
134137
/// Adds a sink that writes log events to a table in a MSSqlServer database.
138+
///
139+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead.
140+
///
135141
/// </summary>
136142
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
137143
/// <param name="connectionString">The connection string to the database where to store the events.</param>
@@ -144,8 +150,6 @@ public static LoggerConfiguration MSSqlServer(
144150
/// <param name="columnOptionsSection">A config section defining various column settings</param>
145151
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
146152
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
147-
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
148-
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
149153
/// <returns>Logger configuration, allowing configuration to continue.</returns>
150154
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
151155
public static LoggerConfiguration MSSqlServer(
@@ -158,22 +162,66 @@ public static LoggerConfiguration MSSqlServer(
158162
bool autoCreateSqlTable = false,
159163
ColumnOptions columnOptions = null,
160164
IConfigurationSection columnOptionsSection = null,
161-
string schemaName = "dbo",
162-
ITextFormatter logEventFormatter = null,
165+
string schemaName = MSSqlServerSink.DefaultSchemaName,
166+
ITextFormatter logEventFormatter = null)
167+
{
168+
// Do not add new parameters here. This interface is considered legacy and will be deprecated in the future.
169+
// For adding new input parameters use the SinkOptions class and the method overload that accepts SinkOptions.
170+
171+
var sinkOptions = new SinkOptions(tableName, null, null, autoCreateSqlTable, schemaName);
172+
173+
return loggerAuditSinkConfiguration.MSSqlServer(
174+
connectionString: connectionString,
175+
sinkOptions: sinkOptions,
176+
appConfiguration: appConfiguration,
177+
restrictedToMinimumLevel: restrictedToMinimumLevel,
178+
formatProvider: formatProvider,
179+
columnOptions: columnOptions,
180+
columnOptionsSection: columnOptionsSection,
181+
logEventFormatter: logEventFormatter,
182+
sinkOptionsSection: null);
183+
}
184+
185+
/// <summary>
186+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
187+
/// </summary>
188+
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
189+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
190+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
191+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
192+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
193+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
194+
/// <param name="columnOptions">An externally-modified group of column settings</param>
195+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
196+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
197+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
198+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
199+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
200+
public static LoggerConfiguration MSSqlServer(
201+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
202+
string connectionString,
163203
SinkOptions sinkOptions = null,
204+
IConfiguration appConfiguration = null,
205+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
206+
IFormatProvider formatProvider = null,
207+
ColumnOptions columnOptions = null,
208+
IConfigurationSection columnOptionsSection = null,
209+
ITextFormatter logEventFormatter = null,
164210
IConfigurationSection sinkOptionsSection = null)
165211
{
166212
if (loggerAuditSinkConfiguration == null)
167213
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
168214

215+
sinkOptions = sinkOptions ?? new SinkOptions();
216+
columnOptions = columnOptions ?? new ColumnOptions();
217+
169218
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
170219
connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
171220
columnOptions = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
172221
sinkOptions = microsoftExtensionsConfiguration.ConfigureSinkOptions(sinkOptions, sinkOptionsSection);
173222

174223
IMSSqlServerAuditSinkFactory auditSinkFactory = new MSSqlServerAuditSinkFactory();
175-
var auditSink = auditSinkFactory.Create(connectionString, tableName, formatProvider, autoCreateSqlTable,
176-
columnOptions, schemaName, logEventFormatter, sinkOptions);
224+
var auditSink = auditSinkFactory.Create(connectionString, sinkOptions, formatProvider, columnOptions, logEventFormatter);
177225

178226
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
179227
}

0 commit comments

Comments
 (0)