@@ -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,48 +188,48 @@ 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 "bit" :
146
- dataType = Type . GetType ( "System.Boolean" ) ;
194
+ dataType = typeof ( bool ) ;
147
195
break ;
148
196
case "char" :
149
197
case "nchar" :
150
198
case "ntext" :
151
199
case "nvarchar" :
152
200
case "text" :
153
201
case "varchar" :
154
- dataType = Type . GetType ( "System.String" ) ;
202
+ dataType = typeof ( string ) ;
155
203
break ;
156
204
case "date" :
157
205
case "datetime" :
158
206
case "datetime2" :
159
207
case "smalldatetime" :
160
- dataType = Type . GetType ( "System. DateTime" ) ;
208
+ dataType = typeof ( DateTime ) ;
161
209
break ;
162
210
case "decimal" :
163
211
case "money" :
164
212
case "numeric" :
165
213
case "smallmoney" :
166
- dataType = Type . GetType ( "System. Decimal" ) ;
214
+ dataType = typeof ( Decimal ) ;
167
215
break ;
168
216
case "float" :
169
- dataType = Type . GetType ( "System.Double" ) ;
217
+ dataType = typeof ( double ) ;
170
218
break ;
171
219
case "int" :
172
- dataType = Type . GetType ( "System.Int32" ) ;
220
+ dataType = typeof ( int ) ;
173
221
break ;
174
222
case "real" :
175
- dataType = Type . GetType ( "System.Single" ) ;
223
+ dataType = typeof ( float ) ;
176
224
break ;
177
225
case "smallint" :
178
- dataType = Type . GetType ( "System.Int16" ) ;
226
+ dataType = typeof ( short ) ;
179
227
break ;
180
228
case "time" :
181
- dataType = Type . GetType ( "System. TimeSpan" ) ;
229
+ dataType = typeof ( TimeSpan ) ;
182
230
break ;
183
231
case "uniqueidentifier" :
184
- dataType = Type . GetType ( "System. Guid" ) ;
232
+ dataType = typeof ( Guid ) ;
185
233
break ;
186
234
}
187
235
column . DataType = dataType ;
0 commit comments