@@ -64,11 +64,8 @@ public static LoggerConfiguration MSSqlServer(
64
64
65
65
var defaultedPeriod = period ?? MSSqlServerSink . DefaultPeriod ;
66
66
67
- MSSqlServerConfigurationSection serviceConfigSection =
68
- ConfigurationManager . GetSection ( "MSSqlServerSettingsSection" ) as MSSqlServerConfigurationSection ;
69
-
70
67
// If we have additional columns from config, load them as well
71
- if ( serviceConfigSection != null && serviceConfigSection . Columns . Count > 0 )
68
+ if ( ConfigurationManager . GetSection ( "MSSqlServerSettingsSection" ) is MSSqlServerConfigurationSection serviceConfigSection && serviceConfigSection . Columns . Count > 0 )
72
69
{
73
70
if ( columnOptions == null )
74
71
{
@@ -93,6 +90,57 @@ public static LoggerConfiguration MSSqlServer(
93
90
restrictedToMinimumLevel ) ;
94
91
}
95
92
93
+ /// <summary>
94
+ /// Adds a sink that writes log events to a table in a MSSqlServer database.
95
+ /// Create a database and execute the table creation script found here
96
+ /// https://gist.github.com/mivano/10429656
97
+ /// or use the autoCreateSqlTable option.
98
+ /// </summary>
99
+ /// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
100
+ /// <param name="connectionString">The connection string to the database where to store the events.</param>
101
+ /// <param name="tableName">Name of the table to store the events in.</param>
102
+ /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
103
+ /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
104
+ /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
105
+ /// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
106
+ /// <param name="columnOptions"></param>
107
+ /// <returns>Logger configuration, allowing configuration to continue.</returns>
108
+ /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
109
+ public static LoggerConfiguration MSSqlServer ( this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration ,
110
+ string connectionString ,
111
+ string tableName ,
112
+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
113
+ IFormatProvider formatProvider = null ,
114
+ bool autoCreateSqlTable = false ,
115
+ ColumnOptions columnOptions = null ,
116
+ string schemaName = "dbo" )
117
+ {
118
+ if ( loggerAuditSinkConfiguration == null ) throw new ArgumentNullException ( "loggerAuditSinkConfiguration" ) ;
119
+
120
+ // If we have additional columns from config, load them as well
121
+ if ( ConfigurationManager . GetSection ( "MSSqlServerSettingsSection" ) is MSSqlServerConfigurationSection serviceConfigSection && serviceConfigSection . Columns . Count > 0 )
122
+ {
123
+ if ( columnOptions == null )
124
+ {
125
+ columnOptions = new ColumnOptions ( ) ;
126
+ }
127
+ GenerateDataColumnsFromConfig ( serviceConfigSection , columnOptions ) ;
128
+ }
129
+
130
+ connectionString = GetConnectionString ( connectionString ) ;
131
+
132
+ return loggerAuditSinkConfiguration . Sink (
133
+ new MSSqlServerAuditSink (
134
+ connectionString ,
135
+ tableName ,
136
+ formatProvider ,
137
+ autoCreateSqlTable ,
138
+ columnOptions ,
139
+ schemaName
140
+ ) ,
141
+ restrictedToMinimumLevel ) ;
142
+ }
143
+
96
144
/// <summary>
97
145
/// Examine if supplied connection string is a reference to an item in the "ConnectionStrings" section of web.config
98
146
/// If it is, return the ConnectionStrings item, if not, return string as supplied.
@@ -101,7 +149,7 @@ public static LoggerConfiguration MSSqlServer(
101
149
/// <remarks>Pulled from review of Entity Framework 6 methodology for doing the same</remarks>
102
150
private static string GetConnectionString ( string nameOrConnectionString )
103
151
{
104
-
152
+
105
153
// If there is an `=`, we assume this is a raw connection string not a named value
106
154
// If there are no `=`, attempt to pull the named value from config
107
155
if ( nameOrConnectionString . IndexOf ( '=' ) < 0 )
@@ -140,15 +188,15 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
140
188
switch ( c . DataType )
141
189
{
142
190
case "bigint" :
143
- dataType = Type . GetType ( "System.Int64" ) ;
191
+ dataType = typeof ( long ) ;
144
192
break ;
145
193
case "varbinary" :
146
194
case "binary" :
147
195
dataType = Type . GetType ( "System.Byte[]" ) ;
148
196
column . ExtendedProperties [ "DataLength" ] = c . DataLength ;
149
197
break ;
150
198
case "bit" :
151
- dataType = Type . GetType ( "System.Boolean" ) ;
199
+ dataType = typeof ( bool ) ;
152
200
break ;
153
201
case "char" :
154
202
case "nchar" :
@@ -163,31 +211,31 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
163
211
case "datetime" :
164
212
case "datetime2" :
165
213
case "smalldatetime" :
166
- dataType = Type . GetType ( "System. DateTime" ) ;
214
+ dataType = typeof ( DateTime ) ;
167
215
break ;
168
216
case "decimal" :
169
217
case "money" :
170
218
case "numeric" :
171
219
case "smallmoney" :
172
- dataType = Type . GetType ( "System. Decimal" ) ;
220
+ dataType = typeof ( Decimal ) ;
173
221
break ;
174
222
case "float" :
175
- dataType = Type . GetType ( "System.Double" ) ;
223
+ dataType = typeof ( double ) ;
176
224
break ;
177
225
case "int" :
178
- dataType = Type . GetType ( "System.Int32" ) ;
226
+ dataType = typeof ( int ) ;
179
227
break ;
180
228
case "real" :
181
- dataType = Type . GetType ( "System.Single" ) ;
229
+ dataType = typeof ( float ) ;
182
230
break ;
183
231
case "smallint" :
184
- dataType = Type . GetType ( "System.Int16" ) ;
232
+ dataType = typeof ( short ) ;
185
233
break ;
186
234
case "time" :
187
- dataType = Type . GetType ( "System. TimeSpan" ) ;
235
+ dataType = typeof ( TimeSpan ) ;
188
236
break ;
189
237
case "uniqueidentifier" :
190
- dataType = Type . GetType ( "System. Guid" ) ;
238
+ dataType = typeof ( Guid ) ;
191
239
break ;
192
240
}
193
241
0 commit comments