diff --git a/src/Correlation/NBB.Correlation.Serilog.SqlServer/LoggerConfigurationExtensions.cs b/src/Correlation/NBB.Correlation.Serilog.SqlServer/LoggerConfigurationExtensions.cs index 235778ff..2ce18ba1 100644 --- a/src/Correlation/NBB.Correlation.Serilog.SqlServer/LoggerConfigurationExtensions.cs +++ b/src/Correlation/NBB.Correlation.Serilog.SqlServer/LoggerConfigurationExtensions.cs @@ -1,82 +1,83 @@ // Copyright (c) TotalSoft. // This source code is licensed under the MIT license. -using Serilog; -using Serilog.Configuration; -using Serilog.Events; -using System; -using System.Collections.Generic; -using Serilog.Sinks.MSSqlServer; -using System.Linq; +using Serilog; +using Serilog.Configuration; +using Serilog.Events; +using System; +using System.Collections.Generic; +using Serilog.Sinks.MSSqlServer; +using System.Linq; using System.Data; +using Microsoft.Extensions.Configuration; -namespace NBB.Correlation.Serilog.SqlServer -{ - public static class LoggerConfigurationExtensions - { - /// - /// Adds a sink that writes log events to a table in a MSSqlServer database. - /// Adds a column named CorrelationId to the resulting table - /// Create a database and execute the table creation script found here - /// https://gist.github.com/mivano/10429656 - /// or use the autoCreateSqlTable option. - /// - /// The logger configuration. - /// The connection string to the database where to store the events. - /// Name of the table to store the events in. - /// Name of the schema for the table to store the data in. The default is 'dbo'. - /// The minimum log event level required in order to write an event to the sink. - /// The maximum number of events to post in a single batch. - /// The time to wait between checking for event batches. - /// Supplies culture-specific formatting information, or null. - /// Create log table with the provided name on destination sql server. - /// - /// Additional columns to be added - /// Logger configuration, allowing configuration to continue. - /// A required parameter is null. - public static LoggerConfiguration MsSqlServerWithAdditionalColumns( - this LoggerSinkConfiguration loggerConfiguration, - string connectionString, - string tableName, - LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, - int batchPostingLimit = 50, - TimeSpan? period = null, - IFormatProvider formatProvider = null, - bool autoCreateSqlTable = false, - ColumnOptions columnOptions = null, - string schemaName = "dbo", - Dictionary additionalColumns = null - ) - { - if (columnOptions == null) - { - columnOptions = new ColumnOptions(); - } - - if (columnOptions.AdditionalColumns == null) - { - columnOptions.AdditionalColumns = new List(); - } - - if (additionalColumns != null) - { - foreach (var columnName in additionalColumns.Keys) - { - if (columnOptions.AdditionalColumns.Any(x => x.ColumnName.Equals(columnName))) - { - continue; - } - columnOptions.AdditionalColumns.Add( - new SqlColumn - { - ColumnName = columnName, - DataType = additionalColumns[columnName] - }); - } +namespace NBB.Correlation.Serilog.SqlServer +{ + public static class LoggerConfigurationExtensions + { + /// + /// Adds a sink that writes log events to a table in a MSSqlServer database. + /// Adds a column named CorrelationId to the resulting table + /// Create a database and execute the table creation script found here + /// https://gist.github.com/mivano/10429656 + /// or use the autoCreateSqlTable option. + /// + /// The logger configuration. + /// The connection string to the database where to store the events. + /// Name of the table to store the events in. + /// Name of the schema for the table to store the data in. The default is 'dbo'. + /// The minimum log event level required in order to write an event to the sink. + /// The maximum number of events to post in a single batch. + /// The time to wait between checking for event batches. + /// Supplies culture-specific formatting information, or null. + /// Create log table with the provided name on destination sql server. + /// + /// Additional columns to be added + /// Logger configuration, allowing configuration to continue. + /// A required parameter is null. + public static LoggerConfiguration MsSqlServerWithAdditionalColumns( + this LoggerSinkConfiguration loggerConfiguration, + string connectionString, + string tableName, + LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, + int batchPostingLimit = 50, + TimeSpan? period = null, + IFormatProvider formatProvider = null, + bool autoCreateSqlTable = false, + ColumnOptions columnOptions = null, + string schemaName = "dbo", + Dictionary additionalColumns = null + ) + { + if (columnOptions == null) + { + columnOptions = new ColumnOptions(); } - return loggerConfiguration.MSSqlServer( - connectionString, + if (columnOptions.AdditionalColumns == null) + { + columnOptions.AdditionalColumns = new List(); + } + + if (additionalColumns != null) + { + foreach (var columnName in additionalColumns.Keys) + { + if (columnOptions.AdditionalColumns.Any(x => x.ColumnName.Equals(columnName))) + { + continue; + } + columnOptions.AdditionalColumns.Add( + new SqlColumn + { + ColumnName = columnName, + DataType = additionalColumns[columnName] + }); + } + } + + return loggerConfiguration.MSSqlServer( + connectionString, new MSSqlServerSinkOptions { TableName = tableName, @@ -88,66 +89,66 @@ public static LoggerConfiguration MsSqlServerWithAdditionalColumns( restrictedToMinimumLevel, formatProvider, columnOptions); - } - - /// - /// Adds a sink that writes log events to a table in a MSSqlServer database. - /// Adds a column named CorrelationId to the resulting table - /// Create a database and execute the table creation script found here - /// https://gist.github.com/mivano/10429656 - /// or use the autoCreateSqlTable option. - /// - /// The logger configuration. - /// The connection string to the database where to store the events. - /// Name of the table to store the events in. - /// Name of the schema for the table to store the data in. The default is 'dbo'. - /// The minimum log event level required in order to write an event to the sink. - /// The maximum number of events to post in a single batch. - /// The time to wait between checking for event batches. - /// Supplies culture-specific formatting information, or null. - /// Create log table with the provided name on destination sql server. - /// - /// CorrelationId parameter name. Default 'CorrelationId' - /// Type of the correlationId parameter. Default is System.Guid - /// Logger configuration, allowing configuration to continue. - /// A required parameter is null. - public static LoggerConfiguration MsSqlServerWithCorrelation( - this LoggerSinkConfiguration loggerConfiguration, - string connectionString, - string tableName, - LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, - int batchPostingLimit = 50, - TimeSpan? period = null, - IFormatProvider formatProvider = null, - bool autoCreateSqlTable = false, - ColumnOptions columnOptions = null, - string schemaName = "dbo", - string correlationId = "CorrelationId", - SqlDbType? correlationIdType = null - ) - { - if (columnOptions == null) - { - columnOptions = new ColumnOptions(); - } - - if (columnOptions.AdditionalColumns == null) - { - columnOptions.AdditionalColumns = new List(); + } + + /// + /// Adds a sink that writes log events to a table in a MSSqlServer database. + /// Adds a column named CorrelationId to the resulting table + /// Create a database and execute the table creation script found here + /// https://gist.github.com/mivano/10429656 + /// or use the autoCreateSqlTable option. + /// + /// The logger configuration. + /// The connection string to the database where to store the events. + /// Name of the table to store the events in. + /// Name of the schema for the table to store the data in. The default is 'dbo'. + /// The minimum log event level required in order to write an event to the sink. + /// The maximum number of events to post in a single batch. + /// The time to wait between checking for event batches. + /// Supplies culture-specific formatting information, or null. + /// Create log table with the provided name on destination sql server. + /// + /// CorrelationId parameter name. Default 'CorrelationId' + /// Type of the correlationId parameter. Default is System.Guid + /// Logger configuration, allowing configuration to continue. + /// A required parameter is null. + public static LoggerConfiguration MsSqlServerWithCorrelation( + this LoggerSinkConfiguration loggerConfiguration, + string connectionString, + string tableName, + LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, + int batchPostingLimit = 50, + TimeSpan? period = null, + IFormatProvider formatProvider = null, + bool autoCreateSqlTable = false, + ColumnOptions columnOptions = null, + string schemaName = "dbo", + string correlationId = "CorrelationId", + IConfigurationSection columnOptionsSection = null + ) + { + if (columnOptions == null) + { + columnOptions = new ColumnOptions(); + } + + if (columnOptions.AdditionalColumns == null) + { + columnOptions.AdditionalColumns = new List(); + } + + if (!columnOptions.AdditionalColumns.Any(x => x.ColumnName.Equals(correlationId))) + { + columnOptions.AdditionalColumns.Add( + new SqlColumn + { + ColumnName = correlationId, + DataType = SqlDbType.UniqueIdentifier + }); } - if (!columnOptions.AdditionalColumns.Any(x => x.ColumnName.Equals(correlationId))) - { - columnOptions.AdditionalColumns.Add( - new SqlColumn - { - ColumnName = correlationId, - DataType = correlationIdType ?? SqlDbType.UniqueIdentifier - }); - } - - return loggerConfiguration.MSSqlServer( - connectionString, + return loggerConfiguration.MSSqlServer( + connectionString, new MSSqlServerSinkOptions { TableName = tableName, @@ -158,7 +159,7 @@ public static LoggerConfiguration MsSqlServerWithCorrelation( }, null, null, restrictedToMinimumLevel, formatProvider, - columnOptions); - } - } -} \ No newline at end of file + columnOptions, columnOptionsSection); + } + } +}