Skip to content

Commit 0725821

Browse files
committed
* Added SinkOptions to config extensions classes (implementations yet to do) and to sink and audit sink contructors.
* Sink and audit sink configure sql authentication accoding to values in sinkOptions parameter.
1 parent 521a111 commit 0725821

File tree

13 files changed

+173
-50
lines changed

13 files changed

+173
-50
lines changed

sample/WorkerServiceDemo/appsettings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
},
2626
"autoCreateSqlTable": true,
2727
"restrictedToMinimumLevel": "Information",
28-
"logEventFormatter": "WorkerServiceDemo.CustomLogEventFormatter::Formatter, WorkerServiceDemo"
28+
"logEventFormatter": "WorkerServiceDemo.CustomLogEventFormatter::Formatter, WorkerServiceDemo",
29+
"sinkOptionsSection": {
30+
"useAzureManagedIdentity": false,
31+
"azureServiceTokenProviderResource": "TestAzureServiceTokenProviderResource"
32+
}
2933
}
3034
}
3135
]

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Serilog.Formatting;
2121
using Serilog.Sinks.MSSqlServer;
2222
using Serilog.Sinks.MSSqlServer.Configuration.Factories;
23+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
2324

2425
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
2526
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
@@ -56,6 +57,8 @@ public static class LoggerConfigurationMSSqlServerExtensions
5657
/// <param name="columnOptionsSection">A config section defining various column settings</param>
5758
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
5859
/// <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>
5962
/// <returns>Logger configuration, allowing configuration to continue.</returns>
6063
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
6164
public static LoggerConfiguration MSSqlServer(
@@ -71,7 +74,9 @@ public static LoggerConfiguration MSSqlServer(
7174
ColumnOptions columnOptions = null,
7275
IConfigurationSection columnOptionsSection = null,
7376
string schemaName = "dbo",
74-
ITextFormatter logEventFormatter = null) =>
77+
ITextFormatter logEventFormatter = null,
78+
SinkOptions sinkOptions = null,
79+
IConfigurationSection sinkOptionsSection = null) =>
7580
loggerConfiguration.MSSqlServerInternal(
7681
connectionString: connectionString,
7782
tableName: tableName,
@@ -85,6 +90,8 @@ public static LoggerConfiguration MSSqlServer(
8590
columnOptionsSection: columnOptionsSection,
8691
schemaName: schemaName,
8792
logEventFormatter: logEventFormatter,
93+
sinkOptions: sinkOptions,
94+
sinkOptionsSection: sinkOptionsSection,
8895
applySystemConfiguration: new ApplySystemConfiguration(),
8996
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
9097
sinkFactory: new MSSqlServerSinkFactory());
@@ -104,6 +111,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
104111
IConfigurationSection columnOptionsSection = null,
105112
string schemaName = "dbo",
106113
ITextFormatter logEventFormatter = null,
114+
SinkOptions sinkOptions = null,
115+
IConfigurationSection sinkOptionsSection = null,
107116
IApplySystemConfiguration applySystemConfiguration = null,
108117
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
109118
IMSSqlServerSinkFactory sinkFactory = null)
@@ -114,6 +123,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
114123
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
115124
var colOpts = columnOptions ?? new ColumnOptions();
116125
var connStr = connectionString;
126+
var sinkOpts = sinkOptions ?? new SinkOptions();
117127

118128
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
119129
if (serviceConfigSection != null)
@@ -135,8 +145,13 @@ internal static LoggerConfiguration MSSqlServerInternal(
135145
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
136146
}
137147

148+
if (sinkOptionsSection != null)
149+
{
150+
// TODO read sink options from configuration
151+
}
152+
138153
var sink = sinkFactory.Create(connStr, tableName, batchPostingLimit, defaultedPeriod, formatProvider,
139-
autoCreateSqlTable, colOpts, schemaName, logEventFormatter);
154+
autoCreateSqlTable, colOpts, schemaName, logEventFormatter, sinkOpts);
140155

141156
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
142157
}
@@ -155,6 +170,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
155170
/// <param name="columnOptionsSection">A config section defining various column settings</param>
156171
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
157172
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
173+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
174+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
158175
/// <returns>Logger configuration, allowing configuration to continue.</returns>
159176
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
160177
public static LoggerConfiguration MSSqlServer(
@@ -168,7 +185,9 @@ public static LoggerConfiguration MSSqlServer(
168185
ColumnOptions columnOptions = null,
169186
IConfigurationSection columnOptionsSection = null,
170187
string schemaName = "dbo",
171-
ITextFormatter logEventFormatter = null) =>
188+
ITextFormatter logEventFormatter = null,
189+
SinkOptions sinkOptions = null,
190+
IConfigurationSection sinkOptionsSection = null) =>
172191
loggerAuditSinkConfiguration.MSSqlServerInternal(
173192
connectionString: connectionString,
174193
tableName: tableName,
@@ -180,6 +199,8 @@ public static LoggerConfiguration MSSqlServer(
180199
columnOptionsSection: columnOptionsSection,
181200
schemaName: schemaName,
182201
logEventFormatter: logEventFormatter,
202+
sinkOptions: sinkOptions,
203+
sinkOptionsSection: sinkOptionsSection,
183204
applySystemConfiguration: new ApplySystemConfiguration(),
184205
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
185206
auditSinkFactory: new MSSqlServerAuditSinkFactory());
@@ -197,6 +218,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
197218
IConfigurationSection columnOptionsSection = null,
198219
string schemaName = "dbo",
199220
ITextFormatter logEventFormatter = null,
221+
SinkOptions sinkOptions = null,
222+
IConfigurationSection sinkOptionsSection = null,
200223
IApplySystemConfiguration applySystemConfiguration = null,
201224
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
202225
IMSSqlServerAuditSinkFactory auditSinkFactory = null)
@@ -206,6 +229,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
206229

207230
var colOpts = columnOptions ?? new ColumnOptions();
208231
var connStr = connectionString;
232+
var sinkOpts = sinkOptions ?? new SinkOptions();
209233

210234
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
211235
if (serviceConfigSection != null)
@@ -227,8 +251,13 @@ internal static LoggerConfiguration MSSqlServerInternal(
227251
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
228252
}
229253

254+
if (sinkOptionsSection != null)
255+
{
256+
// TODO read sink options from configuration
257+
}
258+
230259
var auditSink = auditSinkFactory.Create(connStr, tableName, formatProvider, autoCreateSqlTable,
231-
colOpts, schemaName, logEventFormatter);
260+
colOpts, schemaName, logEventFormatter, sinkOpts);
232261

233262
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
234263
}

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Serilog.Formatting;
2020
using Serilog.Sinks.MSSqlServer;
2121
using Serilog.Sinks.MSSqlServer.Configuration.Factories;
22+
using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options;
2223

2324
// M.E.C. support for .NET Standard 2.0 libraries.
2425

@@ -48,6 +49,8 @@ public static partial class LoggerConfigurationMSSqlServerExtensions
4849
/// <param name="columnOptionsSection">A config section defining various column settings</param>
4950
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
5051
/// <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>
5154
/// <returns>Logger configuration, allowing configuration to continue.</returns>
5255
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
5356
public static LoggerConfiguration MSSqlServer(
@@ -63,20 +66,25 @@ public static LoggerConfiguration MSSqlServer(
6366
ColumnOptions columnOptions = null,
6467
IConfigurationSection columnOptionsSection = null,
6568
string schemaName = "dbo",
66-
ITextFormatter logEventFormatter = null)
69+
ITextFormatter logEventFormatter = null,
70+
SinkOptions sinkOptions = null,
71+
#pragma warning disable CA1801
72+
IConfigurationSection sinkOptionsSection = null)
73+
#pragma warning restore CA1801
6774
{
6875
if (loggerConfiguration == null)
6976
throw new ArgumentNullException(nameof(loggerConfiguration));
7077

7178
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
7279

7380
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
74-
var connectionStr = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
75-
var colOpts = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
81+
connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
82+
columnOptions = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
83+
// TODO read sink options from configuration
7684

7785
IMSSqlServerSinkFactory sinkFactory = new MSSqlServerSinkFactory();
78-
var sink = sinkFactory.Create(connectionStr, tableName, batchPostingLimit, defaultedPeriod,
79-
formatProvider, autoCreateSqlTable, colOpts, schemaName, logEventFormatter);
86+
var sink = sinkFactory.Create(connectionString, tableName, batchPostingLimit, defaultedPeriod,
87+
formatProvider, autoCreateSqlTable, columnOptions, schemaName, logEventFormatter, sinkOptions);
8088

8189
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
8290
}
@@ -95,6 +103,8 @@ public static LoggerConfiguration MSSqlServer(
95103
/// <param name="columnOptionsSection">A config section defining various column settings</param>
96104
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
97105
/// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</param>
106+
/// <param name="sinkOptions">Supplies additional settings for the sink</param>
107+
/// <param name="sinkOptionsSection">A config section defining additional settings for the sink</param>
98108
/// <returns>Logger configuration, allowing configuration to continue.</returns>
99109
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
100110
public static LoggerConfiguration MSSqlServer(
@@ -108,18 +118,23 @@ public static LoggerConfiguration MSSqlServer(
108118
ColumnOptions columnOptions = null,
109119
IConfigurationSection columnOptionsSection = null,
110120
string schemaName = "dbo",
111-
ITextFormatter logEventFormatter = null)
121+
ITextFormatter logEventFormatter = null,
122+
SinkOptions sinkOptions = null,
123+
#pragma warning disable CA1801
124+
IConfigurationSection sinkOptionsSection = null)
125+
#pragma warning restore CA1801
112126
{
113127
if (loggerAuditSinkConfiguration == null)
114128
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
115129

116130
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
117131
connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
118132
columnOptions = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
133+
// TODO read sink options from configuration
119134

120135
IMSSqlServerAuditSinkFactory auditSinkFactory = new MSSqlServerAuditSinkFactory();
121136
var auditSink = auditSinkFactory.Create(connectionString, tableName, formatProvider, autoCreateSqlTable,
122-
columnOptions, schemaName, logEventFormatter);
137+
columnOptions, schemaName, logEventFormatter, sinkOptions);
123138

124139
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
125140
}

0 commit comments

Comments
 (0)