20
20
using Serilog . Formatting ;
21
21
using Serilog . Sinks . MSSqlServer ;
22
22
using Serilog . Sinks . MSSqlServer . Configuration . Factories ;
23
+ using Serilog . Sinks . MSSqlServer . Sinks . MSSqlServer . Options ;
23
24
24
25
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
25
26
// 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
56
57
/// <param name="columnOptionsSection">A config section defining various column settings</param>
57
58
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
58
59
/// <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>
59
62
/// <returns>Logger configuration, allowing configuration to continue.</returns>
60
63
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
61
64
public static LoggerConfiguration MSSqlServer (
@@ -71,7 +74,9 @@ public static LoggerConfiguration MSSqlServer(
71
74
ColumnOptions columnOptions = null ,
72
75
IConfigurationSection columnOptionsSection = null ,
73
76
string schemaName = "dbo" ,
74
- ITextFormatter logEventFormatter = null ) =>
77
+ ITextFormatter logEventFormatter = null ,
78
+ SinkOptions sinkOptions = null ,
79
+ IConfigurationSection sinkOptionsSection = null ) =>
75
80
loggerConfiguration . MSSqlServerInternal (
76
81
connectionString : connectionString ,
77
82
tableName : tableName ,
@@ -85,6 +90,8 @@ public static LoggerConfiguration MSSqlServer(
85
90
columnOptionsSection : columnOptionsSection ,
86
91
schemaName : schemaName ,
87
92
logEventFormatter : logEventFormatter ,
93
+ sinkOptions : sinkOptions ,
94
+ sinkOptionsSection : sinkOptionsSection ,
88
95
applySystemConfiguration : new ApplySystemConfiguration ( ) ,
89
96
applyMicrosoftExtensionsConfiguration : new ApplyMicrosoftExtensionsConfiguration ( ) ,
90
97
sinkFactory : new MSSqlServerSinkFactory ( ) ) ;
@@ -104,6 +111,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
104
111
IConfigurationSection columnOptionsSection = null ,
105
112
string schemaName = "dbo" ,
106
113
ITextFormatter logEventFormatter = null ,
114
+ SinkOptions sinkOptions = null ,
115
+ IConfigurationSection sinkOptionsSection = null ,
107
116
IApplySystemConfiguration applySystemConfiguration = null ,
108
117
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null ,
109
118
IMSSqlServerSinkFactory sinkFactory = null )
@@ -114,6 +123,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
114
123
var defaultedPeriod = period ?? MSSqlServerSink . DefaultPeriod ;
115
124
var colOpts = columnOptions ?? new ColumnOptions ( ) ;
116
125
var connStr = connectionString ;
126
+ var sinkOpts = sinkOptions ?? new SinkOptions ( ) ;
117
127
118
128
var serviceConfigSection = applySystemConfiguration . GetSinkConfigurationSection ( AppConfigSectionName ) ;
119
129
if ( serviceConfigSection != null )
@@ -135,8 +145,13 @@ internal static LoggerConfiguration MSSqlServerInternal(
135
145
colOpts = applyMicrosoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
136
146
}
137
147
148
+ if ( sinkOptionsSection != null )
149
+ {
150
+ // TODO read sink options from configuration
151
+ }
152
+
138
153
var sink = sinkFactory . Create ( connStr , tableName , batchPostingLimit , defaultedPeriod , formatProvider ,
139
- autoCreateSqlTable , colOpts , schemaName , logEventFormatter ) ;
154
+ autoCreateSqlTable , colOpts , schemaName , logEventFormatter , sinkOpts ) ;
140
155
141
156
return loggerConfiguration . Sink ( sink , restrictedToMinimumLevel ) ;
142
157
}
@@ -155,6 +170,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
155
170
/// <param name="columnOptionsSection">A config section defining various column settings</param>
156
171
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
157
172
/// <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>
158
175
/// <returns>Logger configuration, allowing configuration to continue.</returns>
159
176
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
160
177
public static LoggerConfiguration MSSqlServer (
@@ -168,7 +185,9 @@ public static LoggerConfiguration MSSqlServer(
168
185
ColumnOptions columnOptions = null ,
169
186
IConfigurationSection columnOptionsSection = null ,
170
187
string schemaName = "dbo" ,
171
- ITextFormatter logEventFormatter = null ) =>
188
+ ITextFormatter logEventFormatter = null ,
189
+ SinkOptions sinkOptions = null ,
190
+ IConfigurationSection sinkOptionsSection = null ) =>
172
191
loggerAuditSinkConfiguration . MSSqlServerInternal (
173
192
connectionString : connectionString ,
174
193
tableName : tableName ,
@@ -180,6 +199,8 @@ public static LoggerConfiguration MSSqlServer(
180
199
columnOptionsSection : columnOptionsSection ,
181
200
schemaName : schemaName ,
182
201
logEventFormatter : logEventFormatter ,
202
+ sinkOptions : sinkOptions ,
203
+ sinkOptionsSection : sinkOptionsSection ,
183
204
applySystemConfiguration : new ApplySystemConfiguration ( ) ,
184
205
applyMicrosoftExtensionsConfiguration : new ApplyMicrosoftExtensionsConfiguration ( ) ,
185
206
auditSinkFactory : new MSSqlServerAuditSinkFactory ( ) ) ;
@@ -197,6 +218,8 @@ internal static LoggerConfiguration MSSqlServerInternal(
197
218
IConfigurationSection columnOptionsSection = null ,
198
219
string schemaName = "dbo" ,
199
220
ITextFormatter logEventFormatter = null ,
221
+ SinkOptions sinkOptions = null ,
222
+ IConfigurationSection sinkOptionsSection = null ,
200
223
IApplySystemConfiguration applySystemConfiguration = null ,
201
224
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null ,
202
225
IMSSqlServerAuditSinkFactory auditSinkFactory = null )
@@ -206,6 +229,7 @@ internal static LoggerConfiguration MSSqlServerInternal(
206
229
207
230
var colOpts = columnOptions ?? new ColumnOptions ( ) ;
208
231
var connStr = connectionString ;
232
+ var sinkOpts = sinkOptions ?? new SinkOptions ( ) ;
209
233
210
234
var serviceConfigSection = applySystemConfiguration . GetSinkConfigurationSection ( AppConfigSectionName ) ;
211
235
if ( serviceConfigSection != null )
@@ -227,8 +251,13 @@ internal static LoggerConfiguration MSSqlServerInternal(
227
251
colOpts = applyMicrosoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
228
252
}
229
253
254
+ if ( sinkOptionsSection != null )
255
+ {
256
+ // TODO read sink options from configuration
257
+ }
258
+
230
259
var auditSink = auditSinkFactory . Create ( connStr , tableName , formatProvider , autoCreateSqlTable ,
231
- colOpts , schemaName , logEventFormatter ) ;
260
+ colOpts , schemaName , logEventFormatter , sinkOpts ) ;
232
261
233
262
return loggerAuditSinkConfiguration . Sink ( auditSink , restrictedToMinimumLevel ) ;
234
263
}
0 commit comments