Skip to content

Commit 41f65c7

Browse files
authored
Merge pull request #170 from MV10/issue_161
Allow choice of config method for newer framework clients
2 parents 5d70bc7 + 1f1b457 commit 41f65c7

27 files changed

+695
-330
lines changed

README.md

Lines changed: 54 additions & 29 deletions
Large diffs are not rendered by default.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright 2014 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using Serilog.Configuration;
17+
using Serilog.Events;
18+
using Serilog.Sinks.MSSqlServer;
19+
using Microsoft.Extensions.Configuration;
20+
using System.Configuration;
21+
using Serilog.Debugging;
22+
23+
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
24+
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
25+
// older .NET Framework 4.5.2 only supports System.Configuration and .NET Standard 2.0 only supports M.E.C.
26+
27+
namespace Serilog
28+
{
29+
/// <summary>
30+
/// Adds the WriteTo.MSSqlServer() extension method to <see cref="LoggerConfiguration"/>.
31+
/// </summary>
32+
public static class LoggerConfigurationMSSqlServerExtensions
33+
{
34+
/// <summary>
35+
/// The configuration section name for app.config or web.config configuration files.
36+
/// </summary>
37+
public static string AppConfigSectionName = "MSSqlServerSettingsSection";
38+
39+
/// <summary>
40+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
41+
/// Create a database and execute the table creation script found here
42+
/// https://gist.github.com/mivano/10429656
43+
/// or use the autoCreateSqlTable option.
44+
/// </summary>
45+
/// <param name="loggerConfiguration">The logger configuration.</param>
46+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
47+
/// <param name="tableName">Name of the table to store the events in.</param>
48+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
49+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
50+
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
51+
/// <param name="period">The time to wait between checking for event batches.</param>
52+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
53+
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
54+
/// <param name="columnOptions">An externally-modified group of column settings</param>
55+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
56+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
57+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
58+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
59+
public static LoggerConfiguration MSSqlServer(
60+
this LoggerSinkConfiguration loggerConfiguration,
61+
string connectionString,
62+
string tableName,
63+
IConfiguration appConfiguration = null,
64+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
65+
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
66+
TimeSpan? period = null,
67+
IFormatProvider formatProvider = null,
68+
bool autoCreateSqlTable = false,
69+
ColumnOptions columnOptions = null,
70+
IConfigurationSection columnOptionsSection = null,
71+
string schemaName = "dbo"
72+
)
73+
{
74+
if(loggerConfiguration == null)
75+
throw new ArgumentNullException("loggerConfiguration");
76+
77+
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
78+
var colOpts = columnOptions ?? new ColumnOptions();
79+
var connStr = connectionString;
80+
81+
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
82+
{
83+
colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
84+
connStr = ApplySystemConfiguration.GetConnectionString(connStr);
85+
86+
if (appConfiguration != null || columnOptionsSection != null)
87+
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
88+
}
89+
90+
if (appConfiguration != null || columnOptionsSection != null)
91+
{
92+
connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
93+
colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
94+
}
95+
96+
return loggerConfiguration.Sink(
97+
new MSSqlServerSink(
98+
connStr,
99+
tableName,
100+
batchPostingLimit,
101+
defaultedPeriod,
102+
formatProvider,
103+
autoCreateSqlTable,
104+
colOpts,
105+
schemaName
106+
),
107+
restrictedToMinimumLevel);
108+
}
109+
110+
/// <summary>
111+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
112+
/// </summary>
113+
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
114+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
115+
/// <param name="tableName">Name of the table to store the events in.</param>
116+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
117+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
118+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
119+
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
120+
/// <param name="columnOptions">An externally-modified group of column settings</param>
121+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
122+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
123+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
124+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
125+
public static LoggerConfiguration MSSqlServer(
126+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
127+
string connectionString,
128+
string tableName,
129+
IConfiguration appConfiguration = null,
130+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
131+
IFormatProvider formatProvider = null,
132+
bool autoCreateSqlTable = false,
133+
ColumnOptions columnOptions = null,
134+
IConfigurationSection columnOptionsSection = null,
135+
string schemaName = "dbo"
136+
)
137+
{
138+
if(loggerAuditSinkConfiguration == null)
139+
throw new ArgumentNullException("loggerAuditSinkConfiguration");
140+
141+
var colOpts = columnOptions ?? new ColumnOptions();
142+
var connStr = connectionString;
143+
144+
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
145+
{
146+
colOpts = ApplySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
147+
connStr = ApplySystemConfiguration.GetConnectionString(connStr);
148+
149+
if (appConfiguration != null || columnOptionsSection != null)
150+
SelfLog.WriteLine("Warning: Both System.Configuration (app.config or web.config) and Microsoft.Extensions.Configuration are being applied to the MSSQLServer sink.");
151+
}
152+
153+
if (appConfiguration != null || columnOptionsSection != null)
154+
{
155+
connStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
156+
colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
157+
}
158+
159+
return loggerAuditSinkConfiguration.Sink(
160+
new MSSqlServerAuditSink(
161+
connStr,
162+
tableName,
163+
formatProvider,
164+
autoCreateSqlTable,
165+
colOpts,
166+
schemaName
167+
),
168+
restrictedToMinimumLevel);
169+
}
170+
}
171+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2014 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using Serilog.Configuration;
17+
using Serilog.Events;
18+
using Serilog.Sinks.MSSqlServer;
19+
using Microsoft.Extensions.Configuration;
20+
21+
// M.E.C. support for .NET Standard 2.0 libraries.
22+
23+
namespace Serilog
24+
{
25+
/// <summary>
26+
/// Adds the WriteTo.MSSqlServer() extension method to <see cref="LoggerConfiguration"/>.
27+
/// </summary>
28+
public static partial class LoggerConfigurationMSSqlServerExtensions
29+
{
30+
/// <summary>
31+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
32+
/// Create a database and execute the table creation script found here
33+
/// https://gist.github.com/mivano/10429656
34+
/// or use the autoCreateSqlTable option.
35+
/// </summary>
36+
/// <param name="loggerConfiguration">The logger configuration.</param>
37+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
38+
/// <param name="tableName">Name of the table to store the events in.</param>
39+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
40+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
41+
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
42+
/// <param name="period">The time to wait between checking for event batches.</param>
43+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
44+
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
45+
/// <param name="columnOptions">An externally-modified group of column settings</param>
46+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
47+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
48+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
49+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
50+
public static LoggerConfiguration MSSqlServer(
51+
this LoggerSinkConfiguration loggerConfiguration,
52+
string connectionString,
53+
string tableName,
54+
IConfiguration appConfiguration = null,
55+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
56+
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
57+
TimeSpan? period = null,
58+
IFormatProvider formatProvider = null,
59+
bool autoCreateSqlTable = false,
60+
ColumnOptions columnOptions = null,
61+
IConfigurationSection columnOptionsSection = null,
62+
string schemaName = "dbo"
63+
)
64+
{
65+
if(loggerConfiguration == null)
66+
throw new ArgumentNullException("loggerConfiguration");
67+
68+
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
69+
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
70+
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
71+
72+
return loggerConfiguration.Sink(
73+
new MSSqlServerSink(
74+
connectionStr,
75+
tableName,
76+
batchPostingLimit,
77+
defaultedPeriod,
78+
formatProvider,
79+
autoCreateSqlTable,
80+
colOpts,
81+
schemaName
82+
),
83+
restrictedToMinimumLevel);
84+
}
85+
86+
/// <summary>
87+
/// Adds a sink that writes log events to a table in a MSSqlServer database.
88+
/// </summary>
89+
/// <param name="loggerAuditSinkConfiguration">The logger configuration.</param>
90+
/// <param name="connectionString">The connection string to the database where to store the events.</param>
91+
/// <param name="tableName">Name of the table to store the events in.</param>
92+
/// <param name="appConfiguration">Additional application-level configuration. Required if connectionString is a name.</param>
93+
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
94+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
95+
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param>
96+
/// <param name="columnOptions">An externally-modified group of column settings</param>
97+
/// <param name="columnOptionsSection">A config section defining various column settings</param>
98+
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param>
99+
/// <returns>Logger configuration, allowing configuration to continue.</returns>
100+
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
101+
public static LoggerConfiguration MSSqlServer(
102+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
103+
string connectionString,
104+
string tableName,
105+
IConfiguration appConfiguration = null,
106+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
107+
IFormatProvider formatProvider = null,
108+
bool autoCreateSqlTable = false,
109+
ColumnOptions columnOptions = null,
110+
IConfigurationSection columnOptionsSection = null,
111+
string schemaName = "dbo"
112+
)
113+
{
114+
if(loggerAuditSinkConfiguration == null)
115+
throw new ArgumentNullException("loggerAuditSinkConfiguration");
116+
117+
var connectionStr = ApplyMicrosoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
118+
var colOpts = ApplyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
119+
120+
return loggerAuditSinkConfiguration.Sink(
121+
new MSSqlServerAuditSink(
122+
connectionString,
123+
tableName,
124+
formatProvider,
125+
autoCreateSqlTable,
126+
columnOptions,
127+
schemaName
128+
),
129+
restrictedToMinimumLevel);
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)