@@ -48,7 +48,6 @@ public class MSSqlServerSink : PeriodicBatchingSink
48
48
readonly DataTable _eventsTable ;
49
49
readonly IFormatProvider _formatProvider ;
50
50
readonly string _tableName ;
51
- readonly CancellationTokenSource _token = new CancellationTokenSource ( ) ;
52
51
private readonly ColumnOptions _columnOptions ;
53
52
54
53
private readonly HashSet < string > _additionalDataColumnNames ;
@@ -129,7 +128,7 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
129
128
{
130
129
using ( var cn = new SqlConnection ( _connectionString ) )
131
130
{
132
- await cn . OpenAsync ( _token . Token ) . ConfigureAwait ( false ) ;
131
+ await cn . OpenAsync ( ) . ConfigureAwait ( false ) ;
133
132
using ( var copy = new SqlBulkCopy ( cn ) )
134
133
{
135
134
copy . DestinationTableName = _tableName ;
@@ -140,9 +139,8 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
140
139
copy . ColumnMappings . Add ( mapping ) ;
141
140
}
142
141
143
- await copy . WriteToServerAsync ( _eventsTable , _token . Token ) . ConfigureAwait ( false ) ;
142
+ await copy . WriteToServerAsync ( _eventsTable ) . ConfigureAwait ( false ) ;
144
143
}
145
-
146
144
}
147
145
}
148
146
catch ( Exception ex )
@@ -163,95 +161,71 @@ DataTable CreateDataTable()
163
161
var id = new DataColumn
164
162
{
165
163
DataType = Type . GetType ( "System.Int32" ) ,
166
- ColumnName = "Id" ,
164
+ ColumnName = ! string . IsNullOrWhiteSpace ( _columnOptions . Id . ColumnName ) ? _columnOptions . Id . ColumnName : "Id" ,
167
165
AutoIncrement = true
168
166
} ;
169
167
eventsTable . Columns . Add ( id ) ;
170
168
171
- if ( _columnOptions . Store . Contains ( StandardColumn . Message ) )
172
- {
173
- var message = new DataColumn
174
- {
175
- DataType = typeof ( string ) ,
176
- MaxLength = - 1 ,
177
- ColumnName = "Message"
178
- } ;
179
- eventsTable . Columns . Add ( message ) ;
180
- }
181
-
182
- if ( _columnOptions . Store . Contains ( StandardColumn . MessageTemplate ) )
169
+ foreach ( var standardColumn in _columnOptions . Store )
183
170
{
184
- var messageTemplate = new DataColumn
185
- {
186
- DataType = typeof ( string ) ,
187
- MaxLength = - 1 ,
188
- ColumnName = "MessageTemplate" ,
189
-
190
- } ;
191
- eventsTable . Columns . Add ( messageTemplate ) ;
192
- }
193
-
194
- if ( _columnOptions . Store . Contains ( StandardColumn . Level ) )
195
- {
196
- var level = new DataColumn
197
- {
198
- ColumnName = "Level"
199
- } ;
200
-
201
- if ( _columnOptions . Level . StoreAsEnum )
202
- {
203
- level . DataType = typeof ( byte ) ;
204
- }
205
- else
171
+ switch ( standardColumn )
206
172
{
207
- level . DataType = typeof ( string ) ;
208
- level . MaxLength = 128 ;
173
+ case StandardColumn . Level :
174
+ eventsTable . Columns . Add ( new DataColumn
175
+ {
176
+ DataType = _columnOptions . Level . StoreAsEnum ? typeof ( byte ) : typeof ( string ) ,
177
+ MaxLength = _columnOptions . Level . StoreAsEnum ? 0 : 128 ,
178
+ ColumnName = _columnOptions . Level . ColumnName ?? StandardColumn . Level . ToString ( )
179
+ } ) ;
180
+ break ;
181
+ case StandardColumn . TimeStamp :
182
+ eventsTable . Columns . Add ( new DataColumn
183
+ {
184
+ DataType = typeof ( DateTime ) ,
185
+ ColumnName = _columnOptions . TimeStamp . ColumnName ?? StandardColumn . TimeStamp . ToString ( ) ,
186
+ AllowDBNull = false
187
+ } ) ;
188
+ break ;
189
+ case StandardColumn . LogEvent :
190
+ eventsTable . Columns . Add ( new DataColumn
191
+ {
192
+ DataType = typeof ( string ) ,
193
+ ColumnName = _columnOptions . LogEvent . ColumnName ?? StandardColumn . LogEvent . ToString ( )
194
+ } ) ;
195
+ break ;
196
+ case StandardColumn . Message :
197
+ eventsTable . Columns . Add ( new DataColumn
198
+ {
199
+ DataType = typeof ( string ) ,
200
+ MaxLength = - 1 ,
201
+ ColumnName = _columnOptions . Message . ColumnName ?? StandardColumn . Message . ToString ( )
202
+ } ) ;
203
+ break ;
204
+ case StandardColumn . MessageTemplate :
205
+ eventsTable . Columns . Add ( new DataColumn
206
+ {
207
+ DataType = typeof ( string ) ,
208
+ MaxLength = - 1 ,
209
+ ColumnName = _columnOptions . MessageTemplate . ColumnName ?? StandardColumn . MessageTemplate . ToString ( )
210
+ } ) ;
211
+ break ;
212
+ case StandardColumn . Exception :
213
+ eventsTable . Columns . Add ( new DataColumn
214
+ {
215
+ DataType = typeof ( string ) ,
216
+ MaxLength = - 1 ,
217
+ ColumnName = _columnOptions . Exception . ColumnName ?? StandardColumn . Exception . ToString ( )
218
+ } ) ;
219
+ break ;
220
+ case StandardColumn . Properties :
221
+ eventsTable . Columns . Add ( new DataColumn
222
+ {
223
+ DataType = typeof ( string ) ,
224
+ MaxLength = - 1 ,
225
+ ColumnName = _columnOptions . Properties . ColumnName ?? StandardColumn . Properties . ToString ( )
226
+ } ) ;
227
+ break ;
209
228
}
210
-
211
- eventsTable . Columns . Add ( level ) ;
212
- }
213
-
214
- if ( _columnOptions . Store . Contains ( StandardColumn . TimeStamp ) )
215
- {
216
- var timestamp = new DataColumn
217
- {
218
- DataType = Type . GetType ( "System.DateTime" ) ,
219
- ColumnName = "TimeStamp" ,
220
- AllowDBNull = false
221
- } ;
222
- eventsTable . Columns . Add ( timestamp ) ;
223
- }
224
-
225
- if ( _columnOptions . Store . Contains ( StandardColumn . Exception ) )
226
- {
227
- var exception = new DataColumn
228
- {
229
- DataType = typeof ( string ) ,
230
- MaxLength = - 1 ,
231
- ColumnName = "Exception"
232
- } ;
233
- eventsTable . Columns . Add ( exception ) ;
234
- }
235
-
236
- if ( _columnOptions . Store . Contains ( StandardColumn . Properties ) )
237
- {
238
- var props = new DataColumn
239
- {
240
- DataType = typeof ( string ) ,
241
- MaxLength = - 1 ,
242
- ColumnName = "Properties" ,
243
- } ;
244
- eventsTable . Columns . Add ( props ) ;
245
- }
246
-
247
- if ( _columnOptions . Store . Contains ( StandardColumn . LogEvent ) )
248
- {
249
- var eventData = new DataColumn
250
- {
251
- DataType = Type . GetType ( "System.String" ) ,
252
- ColumnName = "LogEvent"
253
- } ;
254
- eventsTable . Columns . Add ( eventData ) ;
255
229
}
256
230
257
231
if ( _columnOptions . AdditionalDataColumns != null )
@@ -288,9 +262,7 @@ void FillDataTable(IEnumerable<LogEvent> events)
288
262
row [ "Level" ] = logEvent . Level ;
289
263
break ;
290
264
case StandardColumn . TimeStamp :
291
- row [ "TimeStamp" ] = _columnOptions . TimeStamp . ConvertToUtc
292
- ? logEvent . Timestamp . DateTime . ToUniversalTime ( )
293
- : logEvent . Timestamp . DateTime ;
265
+ row [ "TimeStamp" ] = _columnOptions . TimeStamp . ConvertToUtc ? logEvent . Timestamp . DateTime . ToUniversalTime ( ) : logEvent . Timestamp . DateTime ;
294
266
break ;
295
267
case StandardColumn . Exception :
296
268
row [ "Exception" ] = logEvent . Exception != null ? logEvent . Exception . ToString ( ) : null ;
@@ -338,15 +310,11 @@ private string ConvertPropertiesToXmlStructure(IEnumerable<KeyValuePair<string,
338
310
339
311
if ( options . UsePropertyKeyAsElementName )
340
312
{
341
- sb . AppendFormat ( "<{0}>{1}</{0}>" , XmlPropertyFormatter . GetValidElementName ( property . Key ) ,
342
- value ) ;
313
+ sb . AppendFormat ( "<{0}>{1}</{0}>" , XmlPropertyFormatter . GetValidElementName ( property . Key ) , value ) ;
343
314
}
344
315
else
345
316
{
346
- sb . AppendFormat ( "<{0} key='{1}'>{2}</{0}>" ,
347
- options . PropertyElementName ,
348
- property . Key ,
349
- value ) ;
317
+ sb . AppendFormat ( "<{0} key='{1}'>{2}</{0}>" , options . PropertyElementName , property . Key , value ) ;
350
318
}
351
319
}
352
320
@@ -390,15 +358,15 @@ private void ConvertPropertiesToColumn(DataRow row, IReadOnlyDictionary<string,
390
358
if ( scalarValue == null )
391
359
{
392
360
row [ columnName ] = property . Value . ToString ( ) ;
393
- continue ;
361
+ continue ;
394
362
}
395
363
396
364
if ( scalarValue . Value == null && row . Table . Columns [ columnName ] . AllowDBNull )
397
365
{
398
366
row [ columnName ] = DBNull . Value ;
399
367
continue ;
400
368
}
401
-
369
+
402
370
if ( TryChangeType ( scalarValue . Value , columnType , out conversion ) )
403
371
{
404
372
row [ columnName ] = conversion ;
@@ -436,8 +404,6 @@ private static bool TryChangeType(object obj, Type type, out object conversion)
436
404
/// <param name="disposing"></param>
437
405
protected override void Dispose ( bool disposing )
438
406
{
439
- _token . Cancel ( ) ;
440
-
441
407
if ( _eventsTable != null )
442
408
_eventsTable . Dispose ( ) ;
443
409
0 commit comments