13
13
// limitations under the License.
14
14
15
15
using System ;
16
- using System . Configuration ;
17
16
using Microsoft . Extensions . Configuration ;
18
17
using Serilog . Configuration ;
19
18
using Serilog . Debugging ;
20
19
using Serilog . Events ;
21
20
using Serilog . Formatting ;
22
21
using Serilog . Sinks . MSSqlServer ;
22
+ using Serilog . Sinks . MSSqlServer . Configuration . Factories ;
23
23
24
24
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
25
25
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
@@ -71,7 +71,42 @@ public static LoggerConfiguration MSSqlServer(
71
71
ColumnOptions columnOptions = null ,
72
72
IConfigurationSection columnOptionsSection = null ,
73
73
string schemaName = "dbo" ,
74
- ITextFormatter logEventFormatter = null )
74
+ ITextFormatter logEventFormatter = null ) =>
75
+ loggerConfiguration . MSSqlServerInternal (
76
+ connectionString : connectionString ,
77
+ tableName : tableName ,
78
+ appConfiguration : appConfiguration ,
79
+ restrictedToMinimumLevel : restrictedToMinimumLevel ,
80
+ batchPostingLimit : batchPostingLimit ,
81
+ period : period ,
82
+ formatProvider : formatProvider ,
83
+ autoCreateSqlTable : autoCreateSqlTable ,
84
+ columnOptions : columnOptions ,
85
+ columnOptionsSection : columnOptionsSection ,
86
+ schemaName : schemaName ,
87
+ logEventFormatter : logEventFormatter ,
88
+ applySystemConfiguration : new ApplySystemConfiguration ( ) ,
89
+ applyMicrosoftExtensionsConfiguration : new ApplyMicrosoftExtensionsConfiguration ( ) ,
90
+ sinkFactory : new MSSqlServerSinkFactory ( ) ) ;
91
+
92
+ // Internal overload with parameters used by tests to override the config section and inject mocks
93
+ internal static LoggerConfiguration MSSqlServerInternal (
94
+ this LoggerSinkConfiguration loggerConfiguration ,
95
+ string connectionString ,
96
+ string tableName ,
97
+ IConfiguration appConfiguration = null ,
98
+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
99
+ int batchPostingLimit = MSSqlServerSink . DefaultBatchPostingLimit ,
100
+ TimeSpan ? period = null ,
101
+ IFormatProvider formatProvider = null ,
102
+ bool autoCreateSqlTable = false ,
103
+ ColumnOptions columnOptions = null ,
104
+ IConfigurationSection columnOptionsSection = null ,
105
+ string schemaName = "dbo" ,
106
+ ITextFormatter logEventFormatter = null ,
107
+ IApplySystemConfiguration applySystemConfiguration = null ,
108
+ IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null ,
109
+ IMSSqlServerSinkFactory sinkFactory = null )
75
110
{
76
111
if ( loggerConfiguration == null )
77
112
throw new ArgumentNullException ( nameof ( loggerConfiguration ) ) ;
@@ -80,35 +115,30 @@ public static LoggerConfiguration MSSqlServer(
80
115
var colOpts = columnOptions ?? new ColumnOptions ( ) ;
81
116
var connStr = connectionString ;
82
117
83
- if ( ConfigurationManager . GetSection ( AppConfigSectionName ) is MSSqlServerConfigurationSection serviceConfigSection )
118
+ var serviceConfigSection = applySystemConfiguration . GetSinkConfigurationSection ( AppConfigSectionName ) ;
119
+ if ( serviceConfigSection != null )
84
120
{
85
- var systemConfiguration = new ApplySystemConfiguration ( ) ;
86
- colOpts = systemConfiguration . ConfigureColumnOptions ( serviceConfigSection , colOpts ) ;
87
- connStr = systemConfiguration . GetConnectionString ( connStr ) ;
121
+ colOpts = applySystemConfiguration . ConfigureColumnOptions ( serviceConfigSection , colOpts ) ;
122
+ connStr = applySystemConfiguration . GetConnectionString ( connStr ) ;
88
123
89
124
if ( appConfiguration != null || columnOptionsSection != null )
90
125
SelfLog . WriteLine ( "Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink." ) ;
91
126
}
92
127
93
- if ( appConfiguration != null || columnOptionsSection != null )
128
+ if ( appConfiguration != null )
129
+ {
130
+ connStr = applyMicrosoftExtensionsConfiguration . GetConnectionString ( connStr , appConfiguration ) ;
131
+ }
132
+
133
+ if ( columnOptionsSection != null )
94
134
{
95
- var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration ( ) ;
96
- connStr = microsoftExtensionsConfiguration . GetConnectionString ( connStr , appConfiguration ) ;
97
- colOpts = microsoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
135
+ colOpts = applyMicrosoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
98
136
}
99
137
100
- return loggerConfiguration . Sink (
101
- new MSSqlServerSink (
102
- connStr ,
103
- tableName ,
104
- batchPostingLimit ,
105
- defaultedPeriod ,
106
- formatProvider ,
107
- autoCreateSqlTable ,
108
- colOpts ,
109
- schemaName ,
110
- logEventFormatter ) ,
111
- restrictedToMinimumLevel ) ;
138
+ var sink = sinkFactory . Create ( connStr , tableName , batchPostingLimit , defaultedPeriod , formatProvider ,
139
+ autoCreateSqlTable , colOpts , schemaName , logEventFormatter ) ;
140
+
141
+ return loggerConfiguration . Sink ( sink , restrictedToMinimumLevel ) ;
112
142
}
113
143
114
144
/// <summary>
@@ -138,41 +168,69 @@ public static LoggerConfiguration MSSqlServer(
138
168
ColumnOptions columnOptions = null ,
139
169
IConfigurationSection columnOptionsSection = null ,
140
170
string schemaName = "dbo" ,
141
- ITextFormatter logEventFormatter = null )
171
+ ITextFormatter logEventFormatter = null ) =>
172
+ loggerAuditSinkConfiguration . MSSqlServerInternal (
173
+ connectionString : connectionString ,
174
+ tableName : tableName ,
175
+ appConfiguration : appConfiguration ,
176
+ restrictedToMinimumLevel : restrictedToMinimumLevel ,
177
+ formatProvider : formatProvider ,
178
+ autoCreateSqlTable : autoCreateSqlTable ,
179
+ columnOptions : columnOptions ,
180
+ columnOptionsSection : columnOptionsSection ,
181
+ schemaName : schemaName ,
182
+ logEventFormatter : logEventFormatter ,
183
+ applySystemConfiguration : new ApplySystemConfiguration ( ) ,
184
+ applyMicrosoftExtensionsConfiguration : new ApplyMicrosoftExtensionsConfiguration ( ) ,
185
+ auditSinkFactory : new MSSqlServerAuditSinkFactory ( ) ) ;
186
+
187
+ // Internal overload with parameters used by tests to override the config section and inject mocks
188
+ internal static LoggerConfiguration MSSqlServerInternal (
189
+ this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration ,
190
+ string connectionString ,
191
+ string tableName ,
192
+ IConfiguration appConfiguration = null ,
193
+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
194
+ IFormatProvider formatProvider = null ,
195
+ bool autoCreateSqlTable = false ,
196
+ ColumnOptions columnOptions = null ,
197
+ IConfigurationSection columnOptionsSection = null ,
198
+ string schemaName = "dbo" ,
199
+ ITextFormatter logEventFormatter = null ,
200
+ IApplySystemConfiguration applySystemConfiguration = null ,
201
+ IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null ,
202
+ IMSSqlServerAuditSinkFactory auditSinkFactory = null )
142
203
{
143
204
if ( loggerAuditSinkConfiguration == null )
144
205
throw new ArgumentNullException ( nameof ( loggerAuditSinkConfiguration ) ) ;
145
206
146
207
var colOpts = columnOptions ?? new ColumnOptions ( ) ;
147
208
var connStr = connectionString ;
148
209
149
- if ( ConfigurationManager . GetSection ( AppConfigSectionName ) is MSSqlServerConfigurationSection serviceConfigSection )
210
+ var serviceConfigSection = applySystemConfiguration . GetSinkConfigurationSection ( AppConfigSectionName ) ;
211
+ if ( serviceConfigSection != null )
150
212
{
151
- var systemConfiguration = new ApplySystemConfiguration ( ) ;
152
- colOpts = systemConfiguration . ConfigureColumnOptions ( serviceConfigSection , colOpts ) ;
153
- connStr = systemConfiguration . GetConnectionString ( connStr ) ;
213
+ colOpts = applySystemConfiguration . ConfigureColumnOptions ( serviceConfigSection , colOpts ) ;
214
+ connStr = applySystemConfiguration . GetConnectionString ( connStr ) ;
154
215
155
216
if ( appConfiguration != null || columnOptionsSection != null )
156
217
SelfLog . WriteLine ( "Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink." ) ;
157
218
}
158
219
159
- if ( appConfiguration != null || columnOptionsSection != null )
220
+ if ( appConfiguration != null )
160
221
{
161
- var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration ( ) ;
162
- connStr = microsoftExtensionsConfiguration . GetConnectionString ( connStr , appConfiguration ) ;
163
- colOpts = microsoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
222
+ connStr = applyMicrosoftExtensionsConfiguration . GetConnectionString ( connStr , appConfiguration ) ;
164
223
}
165
224
166
- return loggerAuditSinkConfiguration . Sink (
167
- new MSSqlServerAuditSink (
168
- connStr ,
169
- tableName ,
170
- formatProvider ,
171
- autoCreateSqlTable ,
172
- colOpts ,
173
- schemaName ,
174
- logEventFormatter ) ,
175
- restrictedToMinimumLevel ) ;
225
+ if ( columnOptionsSection != null )
226
+ {
227
+ colOpts = applyMicrosoftExtensionsConfiguration . ConfigureColumnOptions ( colOpts , columnOptionsSection ) ;
228
+ }
229
+
230
+ var auditSink = auditSinkFactory . Create ( connStr , tableName , formatProvider , autoCreateSqlTable ,
231
+ colOpts , schemaName , logEventFormatter ) ;
232
+
233
+ return loggerAuditSinkConfiguration . Sink ( auditSink , restrictedToMinimumLevel ) ;
176
234
}
177
235
}
178
236
}
0 commit comments