@@ -64,11 +64,8 @@ public static LoggerConfiguration MSSqlServer(
6464
6565            var  defaultedPeriod  =  period  ??  MSSqlServerSink . DefaultPeriod ; 
6666
67-             MSSqlServerConfigurationSection  serviceConfigSection  = 
68-                ConfigurationManager . GetSection ( "MSSqlServerSettingsSection" )  as  MSSqlServerConfigurationSection ; 
69- 
7067            // 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 ) 
7269            { 
7370                if  ( columnOptions  ==  null ) 
7471                { 
@@ -93,6 +90,57 @@ public static LoggerConfiguration MSSqlServer(
9390                restrictedToMinimumLevel ) ; 
9491        } 
9592
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+ 
96144        /// <summary> 
97145        /// Examine if supplied connection string is a reference to an item in the "ConnectionStrings" section of web.config 
98146        /// If it is, return the ConnectionStrings item, if not, return string as supplied. 
@@ -101,7 +149,7 @@ public static LoggerConfiguration MSSqlServer(
101149        /// <remarks>Pulled from review of Entity Framework 6 methodology for doing the same</remarks> 
102150        private  static string  GetConnectionString ( string  nameOrConnectionString ) 
103151        { 
104-              
152+ 
105153            // If there is an `=`, we assume this is a raw connection string not a named value 
106154            // If there are no `=`, attempt to pull the named value from config 
107155            if  ( nameOrConnectionString . IndexOf ( '=' )  <  0 ) 
@@ -140,48 +188,48 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
140188                switch  ( c . DataType ) 
141189                { 
142190                    case  "bigint" : 
143-                         dataType  =  Type . GetType ( "System.Int64" ) ; 
191+                         dataType  =  typeof ( long ) ; 
144192                        break ; 
145193                    case  "bit" : 
146-                         dataType  =  Type . GetType ( "System.Boolean" ) ; 
194+                         dataType  =  typeof ( bool ) ; 
147195                        break ; 
148196                    case  "char" : 
149197                    case  "nchar" : 
150198                    case  "ntext" : 
151199                    case  "nvarchar" : 
152200                    case  "text" : 
153201                    case  "varchar" : 
154-                         dataType  =  Type . GetType ( "System.String" ) ; 
202+                         dataType  =  typeof ( string ) ; 
155203                        break ; 
156204                    case  "date" : 
157205                    case  "datetime" : 
158206                    case  "datetime2" : 
159207                    case  "smalldatetime" : 
160-                         dataType  =  Type . GetType ( "System. DateTime" ) ; 
208+                         dataType  =  typeof ( DateTime ) ; 
161209                        break ; 
162210                    case  "decimal" : 
163211                    case  "money" : 
164212                    case  "numeric" : 
165213                    case  "smallmoney" : 
166-                         dataType  =  Type . GetType ( "System. Decimal" ) ; 
214+                         dataType  =  typeof ( Decimal ) ; 
167215                        break ; 
168216                    case  "float" : 
169-                         dataType  =  Type . GetType ( "System.Double" ) ; 
217+                         dataType  =  typeof ( double ) ; 
170218                        break ; 
171219                    case  "int" : 
172-                         dataType  =  Type . GetType ( "System.Int32" ) ; 
220+                         dataType  =  typeof ( int ) ; 
173221                        break ; 
174222                    case  "real" : 
175-                         dataType  =  Type . GetType ( "System.Single" ) ; 
223+                         dataType  =  typeof ( float ) ; 
176224                        break ; 
177225                    case  "smallint" : 
178-                         dataType  =  Type . GetType ( "System.Int16" ) ; 
226+                         dataType  =  typeof ( short ) ; 
179227                        break ; 
180228                    case  "time" : 
181-                         dataType  =  Type . GetType ( "System. TimeSpan" ) ; 
229+                         dataType  =  typeof ( TimeSpan ) ; 
182230                        break ; 
183231                    case  "uniqueidentifier" : 
184-                         dataType  =  Type . GetType ( "System. Guid" ) ; 
232+                         dataType  =  typeof ( Guid ) ; 
185233                        break ; 
186234                } 
187235                column . DataType  =  dataType ; 
0 commit comments