Skip to content

Allow choice of config method for newer framework clients #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Copyright 2014 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using Microsoft.Extensions.Configuration;
using System.Configuration;
using Serilog.Debugging;

// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
// older .NET Framework 4.5.2 only supports System.Configuration and .NET Standard 2.0 only supports M.E.C.

namespace Serilog
{
/// <summary>
/// Adds the WriteTo.MSSqlServer() extension method to <see cref="LoggerConfiguration"/>.
/// </summary>
public static class LoggerConfigurationMSSqlServerExtensions
{
/// <summary>
/// The configuration section name for app.config or web.config configuration files.
/// </summary>
public static string AppConfigSectionName = "MSSqlServerSettingsSection";

/// <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);
}

/// <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 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 loggerAuditSinkConfiguration.Sink(
new MSSqlServerAuditSink(
connStr,
tableName,
formatProvider,
autoCreateSqlTable,
colOpts,
schemaName
),
restrictedToMinimumLevel);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2014 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using Microsoft.Extensions.Configuration;

// M.E.C. support for .NET Standard 2.0 libraries.

namespace Serilog
{
/// <summary>
/// Adds the WriteTo.MSSqlServer() extension method to <see cref="LoggerConfiguration"/>.
/// </summary>
public static partial class LoggerConfigurationMSSqlServerExtensions
{
/// <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 connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);

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

/// <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);
}
}
}
Loading