@@ -28,15 +28,15 @@ namespace Serilog.Sinks.MSSqlServer
28
28
/// <summary>Contains common functionality and properties used by both MSSqlServerSinks.</summary>
29
29
internal sealed class MSSqlServerSinkTraits : IDisposable
30
30
{
31
- public string connectionString { get ; }
32
- public string tableName { get ; }
33
- public string schemaName { get ; }
34
- public ColumnOptions columnOptions { get ; }
35
- public IFormatProvider formatProvider { get ; }
36
- public ITextFormatter logEventFormatter { get ; }
37
- public ISet < string > additionalColumnNames { get ; }
38
- public DataTable eventTable { get ; }
39
- public ISet < string > standardColumnNames { get ; }
31
+ public string ConnectionString { get ; }
32
+ public string TableName { get ; }
33
+ public string SchemaName { get ; }
34
+ public ColumnOptions ColumnOptions { get ; }
35
+ public IFormatProvider FormatProvider { get ; }
36
+ public ITextFormatter LogEventFormatter { get ; }
37
+ public ISet < string > AdditionalColumnNames { get ; }
38
+ public DataTable EventTable { get ; }
39
+ public ISet < string > StandardColumnNames { get ; }
40
40
41
41
public MSSqlServerSinkTraits (
42
42
string connectionString ,
@@ -53,41 +53,40 @@ public MSSqlServerSinkTraits(
53
53
if ( string . IsNullOrWhiteSpace ( tableName ) )
54
54
throw new ArgumentNullException ( nameof ( tableName ) ) ;
55
55
56
- this . connectionString = connectionString ;
57
- this . tableName = tableName ;
58
- this . schemaName = schemaName ;
59
- this . columnOptions = columnOptions ?? new ColumnOptions ( ) ;
60
- this . formatProvider = formatProvider ;
56
+ ConnectionString = connectionString ;
57
+ TableName = tableName ;
58
+ SchemaName = schemaName ;
59
+ ColumnOptions = columnOptions ?? new ColumnOptions ( ) ;
60
+ FormatProvider = formatProvider ;
61
61
62
- standardColumnNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
63
- foreach ( var stdCol in this . columnOptions . Store )
62
+ StandardColumnNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
63
+ foreach ( var stdCol in ColumnOptions . Store )
64
64
{
65
- var col = this . columnOptions . GetStandardColumnOptions ( stdCol ) ;
66
- standardColumnNames . Add ( col . ColumnName ) ;
65
+ var col = ColumnOptions . GetStandardColumnOptions ( stdCol ) ;
66
+ StandardColumnNames . Add ( col . ColumnName ) ;
67
67
}
68
68
69
- additionalColumnNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
70
- if ( this . columnOptions . AdditionalColumns != null )
71
- foreach ( var col in this . columnOptions . AdditionalColumns )
72
- additionalColumnNames . Add ( col . ColumnName ) ;
69
+ AdditionalColumnNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
70
+ if ( ColumnOptions . AdditionalColumns != null )
71
+ foreach ( var col in ColumnOptions . AdditionalColumns )
72
+ AdditionalColumnNames . Add ( col . ColumnName ) ;
73
73
74
- if ( this . columnOptions . Store . Contains ( StandardColumn . LogEvent ) )
75
- this . logEventFormatter = logEventFormatter ?? new JsonLogEventFormatter ( this ) ;
74
+ if ( ColumnOptions . Store . Contains ( StandardColumn . LogEvent ) )
75
+ LogEventFormatter = logEventFormatter ?? new JsonLogEventFormatter ( this ) ;
76
76
77
- eventTable = CreateDataTable ( ) ;
77
+ EventTable = CreateDataTable ( ) ;
78
78
79
79
if ( autoCreateSqlTable )
80
80
{
81
81
try
82
82
{
83
- SqlTableCreator tableCreator = new SqlTableCreator ( this . connectionString , this . schemaName , this . tableName , eventTable , this . columnOptions ) ;
83
+ SqlTableCreator tableCreator = new SqlTableCreator ( ConnectionString , SchemaName , TableName , EventTable , ColumnOptions ) ;
84
84
tableCreator . CreateTable ( ) ; // return code ignored, 0 = failure?
85
85
}
86
86
catch ( Exception ex )
87
87
{
88
88
SelfLog . WriteLine ( $ "Exception creating table { tableName } :\n { ex } ") ;
89
89
}
90
-
91
90
}
92
91
}
93
92
@@ -98,14 +97,14 @@ public MSSqlServerSinkTraits(
98
97
/// </returns>
99
98
public IEnumerable < KeyValuePair < string , object > > GetColumnsAndValues ( LogEvent logEvent )
100
99
{
101
- foreach ( var column in columnOptions . Store )
100
+ foreach ( var column in ColumnOptions . Store )
102
101
{
103
102
// skip Id (auto-incrementing identity)
104
103
if ( column != StandardColumn . Id )
105
104
yield return GetStandardColumnNameAndValue ( column , logEvent ) ;
106
105
}
107
106
108
- if ( columnOptions . AdditionalColumns != null )
107
+ if ( ColumnOptions . AdditionalColumns != null )
109
108
{
110
109
foreach ( var columnValuePair in ConvertPropertiesToColumn ( logEvent . Properties ) )
111
110
yield return columnValuePair ;
@@ -114,62 +113,62 @@ public IEnumerable<KeyValuePair<string, object>> GetColumnsAndValues(LogEvent lo
114
113
115
114
public void Dispose ( )
116
115
{
117
- eventTable . Dispose ( ) ;
116
+ EventTable . Dispose ( ) ;
118
117
}
119
118
120
119
internal KeyValuePair < string , object > GetStandardColumnNameAndValue ( StandardColumn column , LogEvent logEvent )
121
120
{
122
121
switch ( column )
123
122
{
124
123
case StandardColumn . Message :
125
- return new KeyValuePair < string , object > ( columnOptions . Message . ColumnName , logEvent . RenderMessage ( formatProvider ) ) ;
124
+ return new KeyValuePair < string , object > ( ColumnOptions . Message . ColumnName , logEvent . RenderMessage ( FormatProvider ) ) ;
126
125
case StandardColumn . MessageTemplate :
127
- return new KeyValuePair < string , object > ( columnOptions . MessageTemplate . ColumnName , logEvent . MessageTemplate . Text ) ;
126
+ return new KeyValuePair < string , object > ( ColumnOptions . MessageTemplate . ColumnName , logEvent . MessageTemplate . Text ) ;
128
127
case StandardColumn . Level :
129
- return new KeyValuePair < string , object > ( columnOptions . Level . ColumnName , columnOptions . Level . StoreAsEnum ? ( object ) logEvent . Level : logEvent . Level . ToString ( ) ) ;
128
+ return new KeyValuePair < string , object > ( ColumnOptions . Level . ColumnName , ColumnOptions . Level . StoreAsEnum ? ( object ) logEvent . Level : logEvent . Level . ToString ( ) ) ;
130
129
case StandardColumn . TimeStamp :
131
130
return GetTimeStampStandardColumnNameAndValue ( logEvent ) ;
132
131
case StandardColumn . Exception :
133
- return new KeyValuePair < string , object > ( columnOptions . Exception . ColumnName , logEvent . Exception != null ? logEvent . Exception . ToString ( ) : null ) ;
132
+ return new KeyValuePair < string , object > ( ColumnOptions . Exception . ColumnName , logEvent . Exception ? . ToString ( ) ) ;
134
133
case StandardColumn . Properties :
135
- return new KeyValuePair < string , object > ( columnOptions . Properties . ColumnName , ConvertPropertiesToXmlStructure ( logEvent . Properties ) ) ;
134
+ return new KeyValuePair < string , object > ( ColumnOptions . Properties . ColumnName , ConvertPropertiesToXmlStructure ( logEvent . Properties ) ) ;
136
135
case StandardColumn . LogEvent :
137
- return new KeyValuePair < string , object > ( columnOptions . LogEvent . ColumnName , RenderLogEventColumn ( logEvent ) ) ;
136
+ return new KeyValuePair < string , object > ( ColumnOptions . LogEvent . ColumnName , RenderLogEventColumn ( logEvent ) ) ;
138
137
default :
139
138
throw new ArgumentOutOfRangeException ( ) ;
140
139
}
141
140
}
142
141
143
142
private KeyValuePair < string , object > GetTimeStampStandardColumnNameAndValue ( LogEvent logEvent )
144
143
{
145
- var dateTimeOffset = columnOptions . TimeStamp . ConvertToUtc ? logEvent . Timestamp . ToUniversalTime ( ) : logEvent . Timestamp ;
144
+ var dateTimeOffset = ColumnOptions . TimeStamp . ConvertToUtc ? logEvent . Timestamp . ToUniversalTime ( ) : logEvent . Timestamp ;
146
145
147
- if ( columnOptions . TimeStamp . DataType == SqlDbType . DateTimeOffset )
148
- return new KeyValuePair < string , object > ( columnOptions . TimeStamp . ColumnName , dateTimeOffset ) ;
146
+ if ( ColumnOptions . TimeStamp . DataType == SqlDbType . DateTimeOffset )
147
+ return new KeyValuePair < string , object > ( ColumnOptions . TimeStamp . ColumnName , dateTimeOffset ) ;
149
148
150
- return new KeyValuePair < string , object > ( columnOptions . TimeStamp . ColumnName , dateTimeOffset . DateTime ) ;
149
+ return new KeyValuePair < string , object > ( ColumnOptions . TimeStamp . ColumnName , dateTimeOffset . DateTime ) ;
151
150
}
152
151
153
152
private string RenderLogEventColumn ( LogEvent logEvent )
154
153
{
155
- if ( columnOptions . LogEvent . ExcludeAdditionalProperties )
154
+ if ( ColumnOptions . LogEvent . ExcludeAdditionalProperties )
156
155
{
157
- var filteredProperties = logEvent . Properties . Where ( p => ! additionalColumnNames . Contains ( p . Key ) ) ;
156
+ var filteredProperties = logEvent . Properties . Where ( p => ! AdditionalColumnNames . Contains ( p . Key ) ) ;
158
157
logEvent = new LogEvent ( logEvent . Timestamp , logEvent . Level , logEvent . Exception , logEvent . MessageTemplate , filteredProperties . Select ( x => new LogEventProperty ( x . Key , x . Value ) ) ) ;
159
158
}
160
159
161
160
var sb = new StringBuilder ( ) ;
162
161
using ( var writer = new System . IO . StringWriter ( sb ) )
163
- logEventFormatter . Format ( logEvent , writer ) ;
162
+ LogEventFormatter . Format ( logEvent , writer ) ;
164
163
return sb . ToString ( ) ;
165
164
}
166
165
167
166
private string ConvertPropertiesToXmlStructure ( IEnumerable < KeyValuePair < string , LogEventPropertyValue > > properties )
168
167
{
169
- var options = columnOptions . Properties ;
168
+ var options = ColumnOptions . Properties ;
170
169
171
170
if ( options . ExcludeAdditionalProperties )
172
- properties = properties . Where ( p => ! additionalColumnNames . Contains ( p . Key ) ) ;
171
+ properties = properties . Where ( p => ! AdditionalColumnNames . Contains ( p . Key ) ) ;
173
172
174
173
if ( options . PropertiesFilter != null )
175
174
{
@@ -220,19 +219,19 @@ private IEnumerable<KeyValuePair<string, object>> ConvertPropertiesToColumn(IRea
220
219
{
221
220
foreach ( var property in properties )
222
221
{
223
- if ( ! eventTable . Columns . Contains ( property . Key ) || standardColumnNames . Contains ( property . Key ) )
222
+ if ( ! EventTable . Columns . Contains ( property . Key ) || StandardColumnNames . Contains ( property . Key ) )
224
223
continue ;
225
224
226
225
var columnName = property . Key ;
227
- var columnType = eventTable . Columns [ columnName ] . DataType ;
226
+ var columnType = EventTable . Columns [ columnName ] . DataType ;
228
227
229
228
if ( ! ( property . Value is ScalarValue scalarValue ) )
230
229
{
231
230
yield return new KeyValuePair < string , object > ( columnName , property . Value . ToString ( ) ) ;
232
231
continue ;
233
232
}
234
233
235
- if ( scalarValue . Value == null && eventTable . Columns [ columnName ] . AllowDBNull )
234
+ if ( scalarValue . Value == null && EventTable . Columns [ columnName ] . AllowDBNull )
236
235
{
237
236
yield return new KeyValuePair < string , object > ( columnName , DBNull . Value ) ;
238
237
continue ;
@@ -271,24 +270,24 @@ private static bool TryChangeType(object obj, Type type, out object conversion)
271
270
272
271
private DataTable CreateDataTable ( )
273
272
{
274
- var eventsTable = new DataTable ( tableName ) ;
273
+ var eventsTable = new DataTable ( TableName ) ;
275
274
276
- foreach ( var standardColumn in columnOptions . Store )
275
+ foreach ( var standardColumn in ColumnOptions . Store )
277
276
{
278
- var standardOpts = columnOptions . GetStandardColumnOptions ( standardColumn ) ;
277
+ var standardOpts = ColumnOptions . GetStandardColumnOptions ( standardColumn ) ;
279
278
var dataColumn = standardOpts . AsDataColumn ( ) ;
280
279
eventsTable . Columns . Add ( dataColumn ) ;
281
- if ( standardOpts == columnOptions . PrimaryKey )
280
+ if ( standardOpts == ColumnOptions . PrimaryKey )
282
281
eventsTable . PrimaryKey = new DataColumn [ ] { dataColumn } ;
283
282
}
284
283
285
- if ( columnOptions . AdditionalColumns != null )
284
+ if ( ColumnOptions . AdditionalColumns != null )
286
285
{
287
- foreach ( var addCol in columnOptions . AdditionalColumns )
286
+ foreach ( var addCol in ColumnOptions . AdditionalColumns )
288
287
{
289
288
var dataColumn = addCol . AsDataColumn ( ) ;
290
289
eventsTable . Columns . Add ( dataColumn ) ;
291
- if ( addCol == columnOptions . PrimaryKey )
290
+ if ( addCol == ColumnOptions . PrimaryKey )
292
291
eventsTable . PrimaryKey = new DataColumn [ ] { dataColumn } ;
293
292
}
294
293
}
0 commit comments