Ejemplo n.º 1
0
        /// <summary>
        /// Adds a sink that writes log events to a table in a MSSqlServer database.
        /// </summary>
        /// <param name="loggerAuditSinkConfiguration">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="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</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">An externally-modified group of column settings</param>
        /// <param name="columnOptionsSection">A config section defining various column settings</param>
        /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
        /// <returns>Logger configuration, allowing configuration to continue.</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration MSSqlServer(
            this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
            string connectionString,
            string tableName,
            IConfiguration appConfiguration        = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            IFormatProvider formatProvider         = null,
            bool autoCreateSqlTable     = false,
            ColumnOptions columnOptions = null,
            IConfigurationSection columnOptionsSection = null,
            string schemaName = "dbo"
            )
        {
            if (loggerAuditSinkConfiguration == null)
            {
                throw new ArgumentNullException("loggerAuditSinkConfiguration");
            }

            var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
            var colOpts       = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);

            return(loggerAuditSinkConfiguration.Sink(
                       new MSSqlServerAuditSink(
                           connectionString,
                           tableName,
                           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="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</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">An externally-modified group of column settings</param>
        /// <param name="columnOptionsSection">A config section defining various column settings</param>
        /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</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,
            IConfiguration appConfiguration        = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit                      = MSSqlServerSink.DefaultBatchPostingLimit,
            TimeSpan?period                            = null,
            IFormatProvider formatProvider             = null,
            bool autoCreateSqlTable                    = false,
            ColumnOptions columnOptions                = null,
            IConfigurationSection columnOptionsSection = null,
            string schemaName                          = "dbo"
            )
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException("loggerConfiguration");
            }

            var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
            var colOpts         = columnOptions ?? new ColumnOptions();
            var connStr         = connectionString;

            if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
            {
                colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
                connStr = ApplySystemConfiguration.GetConnectionString(connStr);

                if (appConfiguration != null || columnOptionsSection != null)
                {
                    SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
                }
            }

            if (appConfiguration != null || columnOptionsSection != null)
            {
                connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
                colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
            }

            return(loggerConfiguration.Sink(
                       new MSSqlServerSink(
                           connStr,
                           tableName,
                           batchPostingLimit,
                           defaultedPeriod,
                           formatProvider,
                           autoCreateSqlTable,
                           colOpts,
                           schemaName
                           ),
                       restrictedToMinimumLevel));
        }
        private static void ReadConfiguration(
            ref string connectionString,
            ref MSSqlServerSinkOptions sinkOptions,
            IConfiguration appConfiguration,
            ref ColumnOptions columnOptions,
            IConfigurationSection columnOptionsSection,
            IConfigurationSection sinkOptionsSection)
        {
            sinkOptions   = sinkOptions ?? new MSSqlServerSinkOptions();
            columnOptions = columnOptions ?? new ColumnOptions();

            IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();

            connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
            columnOptions    = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
            sinkOptions      = microsoftExtensionsConfiguration.ConfigureSinkOptions(sinkOptions, sinkOptionsSection);
        }
        public void ConfigureColumnOptionsCallsAttachedColumnOptionsProvider()
        {
            // Arrange
            var inputColumnOptions        = new ColumnOptions();
            var expectedResult            = new ColumnOptions();
            var configurationSectionMock  = new Mock <IConfigurationSection>();
            var columnOptionsProviderMock = new Mock <IMicrosoftExtensionsColumnOptionsProvider>();

            columnOptionsProviderMock.Setup(p => p.ConfigureColumnOptions(It.IsAny <ColumnOptions>(), It.IsAny <IConfigurationSection>()))
            .Returns(expectedResult);
            var sut = new ApplyMicrosoftExtensionsConfiguration(null, columnOptionsProviderMock.Object, null);

            // Act
            var result = sut.ConfigureColumnOptions(inputColumnOptions, configurationSectionMock.Object);

            // Assert
            columnOptionsProviderMock.Verify(p => p.ConfigureColumnOptions(inputColumnOptions, configurationSectionMock.Object), Times.Once);
            Assert.Same(expectedResult, result);
        }
        /// <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="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</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">An externally-modified group of column settings</param>
        /// <param name="columnOptionsSection">A config section defining various column settings</param>
        /// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
        /// <param name="logEventFormatter">Supplies custom formatter for the LogEvent column, or null</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,
            IConfiguration appConfiguration        = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit                      = MSSqlServerSink.DefaultBatchPostingLimit,
            TimeSpan?period                            = null,
            IFormatProvider formatProvider             = null,
            bool autoCreateSqlTable                    = false,
            ColumnOptions columnOptions                = null,
            IConfigurationSection columnOptionsSection = null,
            string schemaName                          = "dbo",
            ITextFormatter logEventFormatter           = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }

            var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
            var connectionStr   = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
            var colOpts         = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);

            return(loggerConfiguration.Sink(
                       new MSSqlServerSink(
                           connectionStr,
                           tableName,
                           batchPostingLimit,
                           defaultedPeriod,
                           formatProvider,
                           autoCreateSqlTable,
                           colOpts,
                           schemaName,
                           logEventFormatter),
                       restrictedToMinimumLevel));
        }