Skip to content

Commit fbb0852

Browse files
committed
Added new configuration extension and sink ctor that take SinkOptions instead of separate paramters. This works now when read from M.E.C. config in WorkerServiceDemo sample app and only for non-audit sink at this point.
1 parent 81947c5 commit fbb0852

File tree

11 files changed

+301
-151
lines changed

11 files changed

+301
-151
lines changed

sample/WorkerServiceDemo/appsettings.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
"Name": "MSSqlServer",
1515
"Args": {
1616
"connectionString": "Server=localhost;Database=LogTest;Integrated Security=SSPI;",
17-
"tableName": "LogEvents",
17+
"sinkOptionsSection": {
18+
"tableName": "LogEvents",
19+
"autoCreateSqlTable": true,
20+
"useAzureManagedIdentity": false,
21+
"azureServiceTokenProviderResource": "TestAzureServiceTokenProviderResource"
22+
},
23+
"restrictedToMinimumLevel": "Information",
1824
"columnOptionsSection": {
1925
"addStandardColumns": [ "LogEvent" ],
2026
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
@@ -23,13 +29,7 @@
2329
"convertToUtc": false
2430
}
2531
},
26-
"autoCreateSqlTable": true,
27-
"restrictedToMinimumLevel": "Information",
28-
"logEventFormatter": "WorkerServiceDemo.CustomLogEventFormatter::Formatter, WorkerServiceDemo",
29-
"sinkOptionsSection": {
30-
"useAzureManagedIdentity": false,
31-
"azureServiceTokenProviderResource": "TestAzureServiceTokenProviderResource"
32-
}
32+
"logEventFormatter": "WorkerServiceDemo.CustomLogEventFormatter::Formatter, WorkerServiceDemo"
3333
}
3434
}
3535
],

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

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public static class LoggerConfigurationMSSqlServerExtensions
4343
/// Create a database and execute the table creation script found here
4444
/// https://gist.github.com/mivano/10429656
4545
/// or use the autoCreateSqlTable option.
46+
///
47+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead of separate parameters.
48+
///
4649
/// </summary>
4750
/// <param name="loggerConfiguration">The logger configuration.</param>
4851
/// <param name="connectionString">The connection string to the database where to store the events.</param>
@@ -57,8 +60,6 @@ public static class LoggerConfigurationMSSqlServerExtensions
5760
/// <param name="columnOptionsSection">A config section defining various column settings</param>
5861
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
5962
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
60-
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
61-
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
6263
/// <returns>Logger configuration, allowing configuration to continue.</returns>
6364
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
6465
public static LoggerConfiguration MSSqlServer(
@@ -74,24 +75,61 @@ public static LoggerConfiguration MSSqlServer(
7475
ColumnOptions columnOptions = null,
7576
IConfigurationSection columnOptionsSection = null,
7677
string schemaName = "dbo",
77-
ITextFormatter logEventFormatter = null,
78-
SinkOptions sinkOptions = null,
79-
IConfigurationSection sinkOptionsSection = null) =>
80-
loggerConfiguration.MSSqlServerInternal(
78+
ITextFormatter logEventFormatter = null)
79+
{
80+
var sinkOptions = new SinkOptions(tableName, batchPostingLimit, period, autoCreateSqlTable, schemaName);
81+
82+
return loggerConfiguration.MSSqlServer(
8183
connectionString: connectionString,
82-
tableName: tableName,
84+
sinkOptions: sinkOptions,
8385
appConfiguration: appConfiguration,
8486
restrictedToMinimumLevel: restrictedToMinimumLevel,
85-
batchPostingLimit: batchPostingLimit,
86-
period: period,
8787
formatProvider: formatProvider,
88-
autoCreateSqlTable: autoCreateSqlTable,
8988
columnOptions: columnOptions,
9089
columnOptionsSection: columnOptionsSection,
91-
schemaName: schemaName,
9290
logEventFormatter: logEventFormatter,
91+
sinkOptionsSection: null);
92+
}
93+
94+
/// <summary>
95+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
96+
/// Create a database and execute the table creation script found here
97+
/// https://gist.github.com/mivano/10429656
98+
/// or use the autoCreateSqlTable option.
99+
/// </summary>
100+
/// <param name="loggerConfiguration">The logger configuration.</param>
101+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
102+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
103+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
104+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
105+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
106+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
107+
/// <param name="columnOptions">An externally-modified group of column settings</param>
108+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
109+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
110+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
111+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
112+
public static LoggerConfiguration MSSqlServer(
113+
this LoggerSinkConfiguration loggerConfiguration,
114+
string connectionString,
115+
SinkOptions sinkOptions = null,
116+
IConfigurationSection sinkOptionsSection = null,
117+
IConfiguration appConfiguration = null,
118+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
119+
IFormatProvider formatProvider = null,
120+
ColumnOptions columnOptions = null,
121+
IConfigurationSection columnOptionsSection = null,
122+
ITextFormatter logEventFormatter = null) =>
123+
loggerConfiguration.MSSqlServerInternal(
124+
connectionString: connectionString,
93125
sinkOptions: sinkOptions,
94126
sinkOptionsSection: sinkOptionsSection,
127+
appConfiguration: appConfiguration,
128+
restrictedToMinimumLevel: restrictedToMinimumLevel,
129+
formatProvider: formatProvider,
130+
columnOptions: columnOptions,
131+
columnOptionsSection: columnOptionsSection,
132+
logEventFormatter: logEventFormatter,
95133
applySystemConfiguration: new ApplySystemConfiguration(),
96134
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
97135
sinkFactory: new MSSqlServerSinkFactory());
@@ -100,59 +138,51 @@ public static LoggerConfiguration MSSqlServer(
100138
internal static LoggerConfiguration MSSqlServerInternal(
101139
this LoggerSinkConfiguration loggerConfiguration,
102140
string connectionString,
103-
string tableName,
141+
SinkOptions sinkOptions = null,
142+
IConfigurationSection sinkOptionsSection = null,
104143
IConfiguration appConfiguration = null,
105144
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
106-
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
107-
TimeSpan? period = null,
108145
IFormatProvider formatProvider = null,
109-
bool autoCreateSqlTable = false,
110146
ColumnOptions columnOptions = null,
111147
IConfigurationSection columnOptionsSection = null,
112-
string schemaName = "dbo",
113148
ITextFormatter logEventFormatter = null,
114-
SinkOptions sinkOptions = null,
115-
IConfigurationSection sinkOptionsSection = null,
116149
IApplySystemConfiguration applySystemConfiguration = null,
117150
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
118151
IMSSqlServerSinkFactory sinkFactory = null)
119152
{
120153
if (loggerConfiguration == null)
121154
throw new ArgumentNullException(nameof(loggerConfiguration));
122155

123-
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
124-
var colOpts = columnOptions ?? new ColumnOptions();
125-
var connStr = connectionString;
126-
var sinkOpts = sinkOptions ?? new SinkOptions();
156+
sinkOptions = sinkOptions ?? new SinkOptions();
157+
columnOptions = columnOptions ?? new ColumnOptions();
127158

128159
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
129160
if (serviceConfigSection != null)
130161
{
131-
connStr = applySystemConfiguration.GetConnectionString(connStr);
132-
colOpts = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
133-
// TODO get sink options from config
162+
connectionString = applySystemConfiguration.GetConnectionString(connectionString);
163+
columnOptions = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, columnOptions);
164+
// TODO get sink options from System.Config
134165

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

139170
if (appConfiguration != null)
140171
{
141-
connStr = applyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
172+
connectionString = applyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
142173
}
143174

144175
if (columnOptionsSection != null)
145176
{
146-
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
177+
columnOptions = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
147178
}
148179

149180
if (sinkOptionsSection != null)
150181
{
151-
sinkOpts = applyMicrosoftExtensionsConfiguration.ConfigureSinkOptions(sinkOpts, sinkOptionsSection);
182+
sinkOptions = applyMicrosoftExtensionsConfiguration.ConfigureSinkOptions(sinkOptions, sinkOptionsSection);
152183
}
153184

154-
var sink = sinkFactory.Create(connStr, tableName, batchPostingLimit, defaultedPeriod, formatProvider,
155-
autoCreateSqlTable, colOpts, schemaName, logEventFormatter, sinkOpts);
185+
var sink = sinkFactory.Create(connectionString, sinkOptions, formatProvider, columnOptions, logEventFormatter);
156186

157187
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
158188
}

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
3535
/// Create a database and execute the table creation script found here
3636
/// https://gist.github.com/mivano/10429656
3737
/// or use the autoCreateSqlTable option.
38+
///
39+
/// Note: this is the legacy version of the extension method. Please use the new one using SinkOptions instead of separate parameters.
40+
///
3841
/// </summary>
3942
/// <param name="loggerConfiguration">The logger configuration.</param>
4043
/// <param name="connectionString">The connection string to the database where to store the events.</param>
@@ -49,8 +52,6 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
4952
/// <param name="columnOptionsSection">A config section defining various column settings</param>
5053
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
5154
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
52-
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
53-
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
5455
/// <returns>Logger configuration, allowing configuration to continue.</returns>
5556
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
5657
public static LoggerConfiguration MSSqlServer(
@@ -66,23 +67,65 @@ public static LoggerConfiguration MSSqlServer(
6667
ColumnOptions columnOptions = null,
6768
IConfigurationSection columnOptionsSection = null,
6869
string schemaName = "dbo",
70+
ITextFormatter logEventFormatter = null)
71+
{
72+
var sinkOptions = new SinkOptions(tableName, batchPostingLimit, period, autoCreateSqlTable, schemaName);
73+
74+
return loggerConfiguration.MSSqlServer(
75+
connectionString: connectionString,
76+
sinkOptions: sinkOptions,
77+
appConfiguration: appConfiguration,
78+
restrictedToMinimumLevel: restrictedToMinimumLevel,
79+
formatProvider: formatProvider,
80+
columnOptions: columnOptions,
81+
columnOptionsSection: columnOptionsSection,
82+
logEventFormatter: logEventFormatter,
83+
sinkOptionsSection: null);
84+
}
85+
86+
/// <summary>
87+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
88+
/// Create a database and execute the table creation script found here
89+
/// https://gist.github.com/mivano/10429656
90+
/// or use the autoCreateSqlTable option.
91+
/// </summary>
92+
/// <param name="loggerConfiguration">The logger configuration.</param>
93+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
94+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
95+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
96+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
97+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
98+
/// <param name="columnOptions">An externally-modified group of column settings</param>
99+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
100+
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
101+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
102+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
103+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
104+
public static LoggerConfiguration MSSqlServer(
105+
this LoggerSinkConfiguration loggerConfiguration,
106+
string connectionString,
107+
SinkOptions sinkOptions,
108+
IConfiguration appConfiguration = null,
109+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
110+
IFormatProvider formatProvider = null,
111+
ColumnOptions columnOptions = null,
112+
IConfigurationSection columnOptionsSection = null,
69113
ITextFormatter logEventFormatter = null,
70-
SinkOptions sinkOptions = null,
71114
IConfigurationSection sinkOptionsSection = null)
72115
{
73116
if (loggerConfiguration == null)
74117
throw new ArgumentNullException(nameof(loggerConfiguration));
75118

76-
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
119+
sinkOptions = sinkOptions ?? new SinkOptions();
120+
columnOptions = columnOptions ?? new ColumnOptions();
77121

78122
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
79123
connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
80124
columnOptions = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
81125
sinkOptions = microsoftExtensionsConfiguration.ConfigureSinkOptions(sinkOptions, sinkOptionsSection);
82126

83127
IMSSqlServerSinkFactory sinkFactory = new MSSqlServerSinkFactory();
84-
var sink = sinkFactory.Create(connectionString, tableName, batchPostingLimit, defaultedPeriod,
85-
formatProvider, autoCreateSqlTable, columnOptions, schemaName, logEventFormatter, sinkOptions);
128+
var sink = sinkFactory.Create(connectionString, sinkOptions, formatProvider, columnOptions, logEventFormatter);
86129

87130
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
88131
}

0 commit comments

Comments
 (0)