Beispiel #1
0
        /// <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="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="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="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 LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
                                                      string connectionString,
                                                      string tableName,
                                                      LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
                                                      IFormatProvider formatProvider         = null,
                                                      bool autoCreateSqlTable     = false,
                                                      ColumnOptions columnOptions = null,
                                                      string schemaName           = "dbo")
        {
            if (loggerAuditSinkConfiguration == null)
            {
                throw new ArgumentNullException("loggerAuditSinkConfiguration");
            }

            var colOpts = columnOptions ?? new ColumnOptions();

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

            connectionString = ApplySystemConfiguration.GetConnectionString(connectionString);

            return(loggerAuditSinkConfiguration.Sink(
                       new MSSqlServerAuditSink(
                           connectionString,
                           tableName,
                           formatProvider,
                           autoCreateSqlTable,
                           colOpts,
                           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));
        }
Beispiel #3
0
        public void GetConfigurationStringCallsAttachedConfigurationStringProvider()
        {
            // Arrange
            const string connectionStringName         = "TestConnectionStringName";
            const string expectedResult               = "TestConnectionString";
            var          connectionStringProviderMock = new Mock <ISystemConfigurationConnectionStringProvider>();

            connectionStringProviderMock.Setup(p => p.GetConnectionString(It.IsAny <string>())).Returns(expectedResult);
            var sut = new ApplySystemConfiguration(connectionStringProviderMock.Object, null, null);

            // Act
            var result = sut.GetConnectionString(connectionStringName);

            // Assert
            connectionStringProviderMock.Verify(p => p.GetConnectionString(connectionStringName), Times.Once);
            Assert.Equal(expectedResult, result);
        }
Beispiel #4
0
        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);
        }
        /// <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>
        /// <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,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit            = MSSqlServerSink.DefaultBatchPostingLimit,
            TimeSpan?period                  = null,
            IFormatProvider formatProvider   = null,
            bool autoCreateSqlTable          = false,
            ColumnOptions columnOptions      = null,
            string schemaName                = "dbo",
            ITextFormatter logEventFormatter = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }

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

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

            connectionString = ApplySystemConfiguration.GetConnectionString(connectionString);

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