private static void ReadPropertiesColumnOptions(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.ExcludeAdditionalProperties), value => columnOptions.Properties.ExcludeAdditionalProperties = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.DictionaryElementName), value => columnOptions.Properties.DictionaryElementName = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.ItemElementName), value => columnOptions.Properties.ItemElementName = value); SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.OmitDictionaryContainerElement), value => columnOptions.Properties.OmitDictionaryContainerElement = value); SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.OmitSequenceContainerElement), value => columnOptions.Properties.OmitSequenceContainerElement = value); SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.OmitStructureContainerElement), value => columnOptions.Properties.OmitStructureContainerElement = value); SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.OmitElementIfEmpty), value => columnOptions.Properties.OmitElementIfEmpty = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.PropertyElementName), value => columnOptions.Properties.PropertyElementName = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.RootElementName), value => columnOptions.Properties.RootElementName = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.SequenceElementName), value => columnOptions.Properties.SequenceElementName = value); SetProperty.IfProvided <string>(config.PropertiesColumn, nameof(columnOptions.Properties.StructureElementName), value => columnOptions.Properties.StructureElementName = value); SetProperty.IfProvided <bool>(config.PropertiesColumn, nameof(columnOptions.Properties.UsePropertyKeyAsElementName), value => columnOptions.Properties.UsePropertyKeyAsElementName = value); }
private static void ReadBatchSettings(MSSqlServerConfigurationSection config, MSSqlServerSinkOptions sinkOptions) { SetProperty.IfProvided <int>(config.BatchPostingLimit, nameof(config.BatchPostingLimit.Value), value => sinkOptions.BatchPostingLimit = value); SetProperty.IfProvided <string>(config.BatchPeriod, nameof(config.BatchPeriod.Value), value => sinkOptions.BatchPeriod = TimeSpan.Parse(value, CultureInfo.InvariantCulture)); SetProperty.IfProvided <bool>(config.EagerlyEmitFirstEvent, nameof(config.EagerlyEmitFirstEvent.Value), value => sinkOptions.EagerlyEmitFirstEvent = value); }
private void AddRmoveStandardColumns(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { // add standard columns if (config.AddStandardColumns.Count > 0) { foreach (StandardColumnConfig col in config.AddStandardColumns) { if (Enum.TryParse(col.Name, ignoreCase: true, result: out StandardColumn stdcol) && !columnOptions.Store.Contains(stdcol)) { columnOptions.Store.Add(stdcol); } } } // remove standard columns if (config.RemoveStandardColumns.Count > 0) { foreach (StandardColumnConfig col in config.RemoveStandardColumns) { if (Enum.TryParse(col.Name, ignoreCase: true, result: out StandardColumn stdcol) && columnOptions.Store.Contains(stdcol)) { columnOptions.Store.Remove(stdcol); } } } }
private static void ReadAzureManagedIdentitiesOptions(MSSqlServerConfigurationSection config, MSSqlServerSinkOptions sinkOptions) { SetProperty.IfProvided <bool>(config.UseAzureManagedIdentity, nameof(config.UseAzureManagedIdentity.Value), value => sinkOptions.UseAzureManagedIdentity = value); SetProperty.IfProvided <string>(config.AzureServiceTokenProviderResource, nameof(config.AzureServiceTokenProviderResource.Value), value => sinkOptions.AzureServiceTokenProviderResource = value); }
public void MSSqlServerCallsApplySystemConfigurationGetColumnOptions() { // Arrange var columnOptions = new Serilog.Sinks.MSSqlServer.ColumnOptions(); var systemConfigSection = new MSSqlServerConfigurationSection(); _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny <string>())) .Returns(systemConfigSection); var sinkFactoryMock = new Mock <IMSSqlServerSinkFactory>(); var periodicBatchingSinkFactoryMock = new Mock <IPeriodicBatchingSinkFactory>(); periodicBatchingSinkFactoryMock.Setup(f => f.Create(It.IsAny <MSSqlServerSink>(), It.IsAny <MSSqlServerSinkOptions>())) .Returns(new Mock <ILogEventSink>().Object); // Act _loggerConfiguration.WriteTo.MSSqlServerInternal( configSectionName: "TestConfigSectionName", connectionString: "TestConnectionString", sinkOptions: new MSSqlServerSinkOptions { TableName = "TestTableName" }, restrictedToMinimumLevel: LevelAlias.Minimum, formatProvider: null, columnOptions: columnOptions, logEventFormatter: null, applySystemConfiguration: _applySystemConfigurationMock.Object, sinkFactory: sinkFactoryMock.Object, batchingSinkFactory: periodicBatchingSinkFactoryMock.Object); // Assert _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(systemConfigSection, columnOptions), Times.Once); }
public void MSSqlServerAuditCallsApplySystemConfigurationGetSinkOptions() { // Arrange var sinkOptions = new MSSqlServerSinkOptions(); var systemConfigSection = new MSSqlServerConfigurationSection(); _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny <string>())) .Returns(systemConfigSection); var auditSinkFactoryMock = new Mock <IMSSqlServerAuditSinkFactory>(); // Act _loggerConfiguration.AuditTo.MSSqlServerInternal( configSectionName: "TestConfigSectionName", connectionString: "TestConnectionString", sinkOptions: sinkOptions, restrictedToMinimumLevel: LevelAlias.Minimum, formatProvider: null, columnOptions: new Serilog.Sinks.MSSqlServer.ColumnOptions(), logEventFormatter: null, applySystemConfiguration: _applySystemConfigurationMock.Object, auditSinkFactory: auditSinkFactoryMock.Object); // Assert _applySystemConfigurationMock.Verify(c => c.ConfigureSinkOptions(systemConfigSection, sinkOptions), Times.Once); }
private void ReadTableOptions(MSSqlServerConfigurationSection config, SinkOptions sinkOptions) { SetProperty.IfProvided <string>(config.TableName, nameof(config.TableName.Value), value => sinkOptions.TableName = value); SetProperty.IfProvided <string>(config.SchemaName, nameof(config.SchemaName.Value), value => sinkOptions.SchemaName = value); SetProperty.IfProvided <bool>(config.AutoCreateSqlTable, nameof(config.AutoCreateSqlTable.Value), value => sinkOptions.AutoCreateSqlTable = value); }
public void MSSqlServerCallsApplySystemConfigurationGetColumnOptions() { // Arrange var columnOptions = new Serilog.Sinks.MSSqlServer.ColumnOptions(); var systemConfigSection = new MSSqlServerConfigurationSection(); _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny <string>())) .Returns(systemConfigSection); var sinkFactoryMock = new Mock <IMSSqlServerSinkFactory>(); // Act _loggerConfiguration.WriteTo.MSSqlServerInternal( configSectionName: "TestConfigSectionName", connectionString: "TestConnectionString", sinkOptions: new SinkOptions { TableName = "TestTableName" }, columnOptions: columnOptions, applySystemConfiguration: _applySystemConfigurationMock.Object, sinkFactory: sinkFactoryMock.Object); // Assert _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(systemConfigSection, columnOptions), Times.Once); }
public MSSqlServerSinkOptions ConfigureSinkOptions(MSSqlServerConfigurationSection config, MSSqlServerSinkOptions sinkOptions) { ReadTableOptions(config, sinkOptions); ReadBatchSettings(config, sinkOptions); ReadAzureManagedIdentitiesOptions(config, sinkOptions); return(sinkOptions); }
public ColumnOptions ConfigureColumnOptions(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { columnOptions = columnOptions ?? new ColumnOptions(); AddRmoveStandardColumns(config, columnOptions); AddAdditionalColumns(config, columnOptions); ReadStandardColumns(config, columnOptions); ReadMiscColumnOptions(config, columnOptions); return(columnOptions); }
/// <summary> /// Adds a sink that writes log events to a table in a MSSqlServer database. /// Create a database and execute the table creation script found here /// https://gist.github.com/mivano/10429656 /// or use the autoCreateSqlTable option. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="connectionString">The connection string to the database where to store the events.</param> /// <param name="tableName">Name of the table to store the events in.</param> /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> /// <param name="columnOptions"></param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration MSSqlServer( this LoggerSinkConfiguration loggerConfiguration, string connectionString, string tableName, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit, TimeSpan?period = null, IFormatProvider formatProvider = null, bool autoCreateSqlTable = false, ColumnOptions columnOptions = null, string schemaName = "dbo" ) { if (loggerConfiguration == null) { throw new ArgumentNullException("loggerConfiguration"); } var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod; #if NET45 MSSqlServerConfigurationSection serviceConfigSection = ConfigurationManager.GetSection("MSSqlServerSettingsSection") as MSSqlServerConfigurationSection; // If we have additional columns from config, load them as well if (serviceConfigSection != null && serviceConfigSection.Columns.Count > 0) { if (columnOptions == null) { columnOptions = new ColumnOptions(); } GenerateDataColumnsFromConfig(serviceConfigSection, columnOptions); } connectionString = GetConnectionString(connectionString); #endif return(loggerConfiguration.Sink( new MSSqlServerSink( connectionString, tableName, batchPostingLimit, defaultedPeriod, formatProvider, autoCreateSqlTable, columnOptions, schemaName ), restrictedToMinimumLevel)); }
/// <summary> /// Adds a sink that writes log events to a table in a MSSqlServer database. /// Create a database and execute the table creation script found here /// https://gist.github.com/mivano/10429656 /// or use the autoCreateSqlTable option. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="connectionString">The connection string to the database where to store the events.</param> /// <param name="tableName">Name of the table to store the events in.</param> /// <param name="storeProperties">Indicates if the additional properties need to be stored as well.</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="storeTimestampInUtc">Store Timestamp In UTC</param> /// <param name="additionalDataColumns">Additional columns for data storage.</param> /// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> /// <param name="excludeAdditionalProperties">Exclude properties from the Properties column if they are being saved to additional columns.</param> /// <param name="storeLogEvent">Save the entire log event to the LogEvent column (nvarchar) as JSON.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration MSSqlServer( this LoggerSinkConfiguration loggerConfiguration, string connectionString, string tableName, bool storeProperties = true, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit, TimeSpan?period = null, IFormatProvider formatProvider = null, bool storeTimestampInUtc = false, DataColumn[] additionalDataColumns = null, bool autoCreateSqlTable = false, bool excludeAdditionalProperties = false, bool storeLogEvent = false ) { if (loggerConfiguration == null) { throw new ArgumentNullException("loggerConfiguration"); } var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod; MSSqlServerConfigurationSection serviceConfigSection = ConfigurationManager.GetSection("MSSqlServerSettingsSection") as MSSqlServerConfigurationSection; // If we have additional columns from config, load them as well if (serviceConfigSection != null && serviceConfigSection.Columns.Count > 0) { additionalDataColumns = GenerateDataColumnsFromConfig(serviceConfigSection, additionalDataColumns); } return(loggerConfiguration.Sink( new MSSqlServerSink( connectionString, tableName, storeProperties, batchPostingLimit, defaultedPeriod, formatProvider, storeTimestampInUtc, additionalDataColumns, autoCreateSqlTable, excludeAdditionalProperties, storeLogEvent ), restrictedToMinimumLevel)); }
public void ConfigureSinkOptionsReadsEnlistInTransaction() { // Arrange var configSection = new MSSqlServerConfigurationSection(); configSection.EnlistInTransaction.Value = "true"; var sinkOptions = new MSSqlServerSinkOptions { EnlistInTransaction = false }; var sut = new SystemConfigurationSinkOptionsProvider(); // Act sut.ConfigureSinkOptions(configSection, sinkOptions); // Assert Assert.True(sinkOptions.EnlistInTransaction); }
public void ConfigureSinkOptionsReadsBatchSettings() { // Arrange var configSection = new MSSqlServerConfigurationSection(); configSection.EagerlyEmitFirstEvent.Value = "true"; var sinkOptions = new MSSqlServerSinkOptions { EagerlyEmitFirstEvent = false }; var sut = new SystemConfigurationSinkOptionsProvider(); // Act sut.ConfigureSinkOptions(configSection, sinkOptions); // Assert Assert.True(sinkOptions.EagerlyEmitFirstEvent); }
private void AddAdditionalColumns(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { if (config.Columns.Count > 0) { foreach (ColumnConfig c in config.Columns) { if (!string.IsNullOrWhiteSpace(c.ColumnName)) { if (columnOptions.AdditionalColumns == null) { columnOptions.AdditionalColumns = new Collection <SqlColumn>(); } columnOptions.AdditionalColumns.Add(c.AsSqlColumn()); } } } }
public void ConfigureSinkOptionsCallsAttachedSinkOptionsProvider() { // Arrange var inputConfigSection = new MSSqlServerConfigurationSection(); var inputSinkOptions = new MSSqlServerSinkOptions(); var expectedResult = new MSSqlServerSinkOptions(); var sinkOptionsProviderMock = new Mock <ISystemConfigurationSinkOptionsProvider>(); sinkOptionsProviderMock.Setup(p => p.ConfigureSinkOptions(It.IsAny <MSSqlServerConfigurationSection>(), It.IsAny <MSSqlServerSinkOptions>())) .Returns(expectedResult); var sut = new ApplySystemConfiguration(null, null, sinkOptionsProviderMock.Object); // Act var result = sut.ConfigureSinkOptions(inputConfigSection, inputSinkOptions); // Assert sinkOptionsProviderMock.Verify(p => p.ConfigureSinkOptions(inputConfigSection, inputSinkOptions), Times.Once); Assert.Same(expectedResult, result); }
private void ReadMiscColumnOptions(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { SetProperty.IfProvided <bool>(config, nameof(columnOptions.DisableTriggers), value => columnOptions.DisableTriggers = value); SetProperty.IfProvided <bool>(config, nameof(columnOptions.ClusteredColumnstoreIndex), value => columnOptions.ClusteredColumnstoreIndex = value); string pkName = null; SetProperty.IfProvidedNotEmpty <string>(config, "PrimaryKeyColumnName", value => pkName = value); if (pkName != null) { if (columnOptions.ClusteredColumnstoreIndex) { throw new ArgumentException("SQL Clustered Columnstore Indexes and primary key constraints are mutually exclusive."); } foreach (var standardCol in columnOptions.Store) { var stdColOpts = columnOptions.GetStandardColumnOptions(standardCol); if (pkName.Equals(stdColOpts.ColumnName, StringComparison.InvariantCultureIgnoreCase)) { columnOptions.PrimaryKey = stdColOpts; break; } } if (columnOptions.PrimaryKey == null && columnOptions.AdditionalColumns != null) { foreach (var col in columnOptions.AdditionalColumns) { if (pkName.Equals(col.ColumnName, StringComparison.InvariantCultureIgnoreCase)) { columnOptions.PrimaryKey = col; break; } } } if (columnOptions.PrimaryKey == null) { throw new ArgumentException($"Could not match the configured primary key column name \"{pkName}\" with a data column in the table."); } } }
public void MSSqlServerAuditCallsApplySystemConfigurationGetSinkOptions() { // Arrange var sinkOptions = new SinkOptions(); var systemConfigSection = new MSSqlServerConfigurationSection(); _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny <string>())) .Returns(systemConfigSection); var auditSinkFactoryMock = new Mock <IMSSqlServerAuditSinkFactory>(); // Act _loggerConfiguration.AuditTo.MSSqlServerInternal( configSectionName: "TestConfigSectionName", connectionString: "TestConnectionString", sinkOptions: sinkOptions, columnOptions: new ColumnOptions(), applySystemConfiguration: _applySystemConfigurationMock.Object, auditSinkFactory: auditSinkFactoryMock.Object); // Assert _applySystemConfigurationMock.Verify(c => c.ConfigureSinkOptions(systemConfigSection, sinkOptions), Times.Once); }
private void ReadStandardColumns(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { SetCommonColumnOptions(config.Exception, columnOptions.Exception); SetCommonColumnOptions(config.Id, columnOptions.Id); SetCommonColumnOptions(config.Level, columnOptions.Level); SetCommonColumnOptions(config.LogEvent, columnOptions.LogEvent); SetCommonColumnOptions(config.Message, columnOptions.Message); SetCommonColumnOptions(config.MessageTemplate, columnOptions.MessageTemplate); SetCommonColumnOptions(config.PropertiesColumn, columnOptions.Properties); SetCommonColumnOptions(config.TimeStamp, columnOptions.TimeStamp); SetProperty.IfProvided <bool>(config.Level, nameof(columnOptions.Level.StoreAsEnum), value => columnOptions.Level.StoreAsEnum = value); SetProperty.IfProvided <bool>(config.LogEvent, nameof(columnOptions.LogEvent.ExcludeStandardColumns), value => columnOptions.LogEvent.ExcludeStandardColumns = value); SetProperty.IfProvided <bool>(config.LogEvent, nameof(columnOptions.LogEvent.ExcludeAdditionalProperties), value => columnOptions.LogEvent.ExcludeAdditionalProperties = value); ReadPropertiesColumnOptions(config, columnOptions); SetProperty.IfProvided <bool>(config.TimeStamp, nameof(columnOptions.TimeStamp.ConvertToUtc), value => columnOptions.TimeStamp.ConvertToUtc = value); }
/// <summary> /// Generate an array of DataColumns using the supplied MSSqlServerConfigurationSection, /// which is an array of keypairs defining the SQL column name and SQL data type /// Entries are appended to a list of DataColumns in column options /// </summary> /// <param name="serviceConfigSection">A previously loaded configuration section</param> /// <param name="columnOptions">column options with existing array of columns to append our config columns to</param> private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSection serviceConfigSection, ColumnOptions columnOptions) { foreach (ColumnConfig c in serviceConfigSection.Columns) { // Set the type based on the defined SQL type from config var column = new DataColumn(c.ColumnName); Type dataType = null; switch (c.DataType) { case "bigint": dataType = Type.GetType("System.Int64"); break; case "bit": dataType = Type.GetType("System.Boolean"); break; case "char": case "nchar": case "ntext": case "nvarchar": case "text": case "varchar": dataType = Type.GetType("System.String"); break; case "date": case "datetime": case "datetime2": case "smalldatetime": dataType = Type.GetType("System.DateTime"); break; case "decimal": case "money": case "numeric": case "smallmoney": dataType = Type.GetType("System.Decimal"); break; case "float": dataType = Type.GetType("System.Double"); break; case "int": dataType = Type.GetType("System.Int32"); break; case "real": dataType = Type.GetType("System.Single"); break; case "smallint": dataType = Type.GetType("System.Int16"); break; case "time": dataType = Type.GetType("System.TimeSpan"); break; case "uniqueidentifier": dataType = Type.GetType("System.Guid"); break; } column.DataType = dataType; if (columnOptions.AdditionalDataColumns == null) { columnOptions.AdditionalDataColumns = new Collection <DataColumn>(); } columnOptions.AdditionalDataColumns.Add(column); } }
private void ReadBatchSettings(MSSqlServerConfigurationSection config, SinkOptions sinkOptions) { SetProperty.IfProvided <int>(config.BatchPostingLimit, nameof(config.BatchPostingLimit.Value), val => sinkOptions.BatchPostingLimit = val); SetProperty.IfProvided <string>(config.BatchPeriod, nameof(config.BatchPeriod.Value), val => sinkOptions.BatchPeriod = TimeSpan.Parse(val, CultureInfo.InvariantCulture)); }
/// <summary> /// Generate an array of DataColumns using the supplied MSSqlServerConfigurationSection, /// which is an array of keypairs defining the SQL column name and SQL data type /// Entries are appended to an incoming list of DataColumns in addiitonalColumns /// </summary> /// <param name="serviceConfigSection">A previously loaded configuration section</param> /// <param name="additionalColumns">Existing array of columns to append our config columns to</param> /// <returns></returns> private static DataColumn[] GenerateDataColumnsFromConfig(MSSqlServerConfigurationSection serviceConfigSection, DataColumn[] additionalColumns) { int i = 0; DataColumn[] returnColumns; if (additionalColumns == null) { returnColumns = new DataColumn[serviceConfigSection.Columns.Count]; } else { returnColumns = additionalColumns; Array.Resize <DataColumn>(ref returnColumns, serviceConfigSection.Columns.Count + additionalColumns.Length); i = additionalColumns.Length; } foreach (ColumnConfig c in serviceConfigSection.Columns) { // Set the type based on the defined SQL type from config DataColumn column = new DataColumn(c.ColumnName); Type dataType = null; switch (c.DataType) { case "bigint": dataType = Type.GetType("System.Int64"); break; case "bit": dataType = Type.GetType("System.Boolean"); break; case "char": case "nchar": case "ntext": case "nvarchar": case "text": case "varchar": dataType = Type.GetType("System.String"); break; case "date": case "datetime": case "datetime2": case "smalldatetime": dataType = Type.GetType("System.DateTime"); break; case "decimal": case "money": case "numeric": case "smallmoney": dataType = Type.GetType("System.Decimal"); break; case "float": dataType = Type.GetType("System.Double"); break; case "int": dataType = Type.GetType("System.Int32"); break; case "real": dataType = Type.GetType("System.Single"); break; case "smallint": dataType = Type.GetType("System.Int16"); break; case "time": dataType = Type.GetType("System.TimeSpan"); break; case "uniqueidentifier": dataType = Type.GetType("System.Guid"); break; } column.DataType = dataType; returnColumns[i++] = column; } return(returnColumns); }
public ColumnOptions ConfigureColumnOptions(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) => _columnOptionsProvider.ConfigureColumnOptions(config, columnOptions);
/// <summary> /// Generate an array of DataColumns using the supplied MSSqlServerConfigurationSection, /// which is an array of keypairs defining the SQL column name and SQL data type /// Entries are appended to a list of DataColumns in column options /// </summary> /// <param name="serviceConfigSection">A previously loaded configuration section</param> /// <param name="columnOptions">column options with existing array of columns to append our config columns to</param> private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSection serviceConfigSection, ColumnOptions columnOptions) { foreach (ColumnConfig c in serviceConfigSection.Columns) { // Set the type based on the defined SQL type from config DataColumn column = new DataColumn(c.ColumnName); Type dataType = null; switch (c.DataType) { case "bigint": dataType = typeof(long); break; case "bit": dataType = typeof(bool); break; case "char": case "nchar": case "ntext": case "nvarchar": case "text": case "varchar": dataType = typeof(string); break; case "date": case "datetime": case "datetime2": case "smalldatetime": dataType = typeof(DateTime); break; case "decimal": case "money": case "numeric": case "smallmoney": dataType = typeof(Decimal); break; case "float": dataType = typeof(double); break; case "int": dataType = typeof(int); break; case "real": dataType = typeof(float); break; case "smallint": dataType = typeof(short); break; case "time": dataType = typeof(TimeSpan); break; case "uniqueidentifier": dataType = typeof(Guid); break; } column.DataType = dataType; if (columnOptions.AdditionalDataColumns == null) { columnOptions.AdditionalDataColumns = new Collection <DataColumn>(); } columnOptions.AdditionalDataColumns.Add(column); } }
/// <summary> /// Populate ColumnOptions properties and collections from app config /// </summary> internal static ColumnOptions ConfigureColumnOptions(MSSqlServerConfigurationSection config, ColumnOptions columnOptions) { var opts = columnOptions ?? new ColumnOptions(); AddRmoveStandardColumns(); AddAdditionalColumns(); ReadStandardColumns(); ReadMiscColumnOptions(); return(opts); void AddRmoveStandardColumns() { // add standard columns if (config.AddStandardColumns.Count > 0) { foreach (StandardColumnConfig col in config.AddStandardColumns) { if (Enum.TryParse(col.Name, ignoreCase: true, result: out StandardColumn stdcol) && !opts.Store.Contains(stdcol)) { opts.Store.Add(stdcol); } } } // remove standard columns if (config.RemoveStandardColumns.Count > 0) { foreach (StandardColumnConfig col in config.RemoveStandardColumns) { if (Enum.TryParse(col.Name, ignoreCase: true, result: out StandardColumn stdcol) && opts.Store.Contains(stdcol)) { opts.Store.Remove(stdcol); } } } } void AddAdditionalColumns() { if (config.Columns.Count > 0) { foreach (ColumnConfig c in config.Columns) { if (!string.IsNullOrWhiteSpace(c.ColumnName)) { if (opts.AdditionalColumns == null) { opts.AdditionalColumns = new Collection <SqlColumn>(); } opts.AdditionalColumns.Add(c.AsSqlColumn()); } } } } void ReadStandardColumns() { SetCommonColumnOptions(config.Exception, opts.Exception); SetCommonColumnOptions(config.Id, opts.Id); SetCommonColumnOptions(config.Level, opts.Level); SetCommonColumnOptions(config.LogEvent, opts.LogEvent); SetCommonColumnOptions(config.Message, opts.Message); SetCommonColumnOptions(config.MessageTemplate, opts.MessageTemplate); SetCommonColumnOptions(config.PropertiesColumn, opts.Properties); SetCommonColumnOptions(config.TimeStamp, opts.TimeStamp); SetProperty.IfProvided <bool>(config.Level, "StoreAsEnum", (val) => opts.Level.StoreAsEnum = val); SetProperty.IfProvided <bool>(config.LogEvent, "ExcludeStandardColumns", (val) => opts.LogEvent.ExcludeStandardColumns = val); SetProperty.IfProvided <bool>(config.LogEvent, "ExcludeAdditionalProperties", (val) => opts.LogEvent.ExcludeAdditionalProperties = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "ExcludeAdditionalProperties", (val) => opts.Properties.ExcludeAdditionalProperties = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "DictionaryElementName", (val) => opts.Properties.DictionaryElementName = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "ItemElementName", (val) => opts.Properties.ItemElementName = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "OmitDictionaryContainerElement", (val) => opts.Properties.OmitDictionaryContainerElement = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "OmitSequenceContainerElement", (val) => opts.Properties.OmitSequenceContainerElement = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "OmitStructureContainerElement", (val) => opts.Properties.OmitStructureContainerElement = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "OmitElementIfEmpty", (val) => opts.Properties.OmitElementIfEmpty = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "PropertyElementName", (val) => opts.Properties.PropertyElementName = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "RootElementName", (val) => opts.Properties.RootElementName = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "SequenceElementName", (val) => opts.Properties.SequenceElementName = val); SetProperty.IfProvided <string>(config.PropertiesColumn, "StructureElementName", (val) => opts.Properties.StructureElementName = val); SetProperty.IfProvided <bool>(config.PropertiesColumn, "UsePropertyKeyAsElementName", (val) => opts.Properties.UsePropertyKeyAsElementName = val); SetProperty.IfProvided <bool>(config.TimeStamp, "ConvertToUtc", (val) => opts.TimeStamp.ConvertToUtc = val); // Standard Columns are subclasses of the SqlColumn class void SetCommonColumnOptions(ColumnConfig source, SqlColumn target) { SetProperty.IfProvidedNotEmpty <string>(source, "ColumnName", (val) => target.ColumnName = val); SetProperty.IfProvided <string>(source, "DataType", (val) => target.SetDataTypeFromConfigString(val)); SetProperty.IfProvided <bool>(source, "AllowNull", (val) => target.AllowNull = val); SetProperty.IfProvided <int>(source, "DataLength", (val) => target.DataLength = val); SetProperty.IfProvided <bool>(source, "NonClusteredIndex", (val) => target.NonClusteredIndex = val); } } void ReadMiscColumnOptions() { SetProperty.IfProvided <bool>(config, "DisableTriggers", (val) => opts.DisableTriggers = val); SetProperty.IfProvided <bool>(config, "ClusteredColumnstoreIndex", (val) => opts.ClusteredColumnstoreIndex = val); string pkName = null; SetProperty.IfProvidedNotEmpty <string>(config, "PrimaryKeyColumnName", (val) => pkName = val); if (pkName != null) { if (opts.ClusteredColumnstoreIndex) { throw new ArgumentException("SQL Clustered Columnstore Indexes and primary key constraints are mutually exclusive."); } foreach (var standardCol in opts.Store) { var stdColOpts = opts.GetStandardColumnOptions(standardCol); if (pkName.Equals(stdColOpts.ColumnName, StringComparison.InvariantCultureIgnoreCase)) { opts.PrimaryKey = stdColOpts; break; } } if (opts.PrimaryKey == null && opts.AdditionalColumns != null) { foreach (var col in opts.AdditionalColumns) { if (pkName.Equals(col.ColumnName, StringComparison.InvariantCultureIgnoreCase)) { opts.PrimaryKey = col; break; } } } if (opts.PrimaryKey == null) { throw new ArgumentException($"Could not match the configured primary key column name \"{pkName}\" with a data column in the table."); } } } }
public SinkOptions ConfigureSinkOptions(MSSqlServerConfigurationSection config, SinkOptions sinkOptions) => _sinkOptionsProvider.ConfigureSinkOptions(config, sinkOptions);