Skip to content

Commit e27d473

Browse files
authored
Merge pull request #254 from ckadluba/refactoring-cleanup-tests
Use factories in config extensions and added unit tests
2 parents b44e9ef + 133f0a2 commit e27d473

File tree

15 files changed

+509
-135
lines changed

15 files changed

+509
-135
lines changed

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/Hybrid/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
// limitations under the License.
1414

1515
using System;
16-
using System.Configuration;
1716
using Microsoft.Extensions.Configuration;
1817
using Serilog.Configuration;
1918
using Serilog.Debugging;
2019
using Serilog.Events;
2120
using Serilog.Formatting;
2221
using Serilog.Sinks.MSSqlServer;
22+
using Serilog.Sinks.MSSqlServer.Configuration.Factories;
2323

2424
// The "Hybrid" configuration system supports both Microsoft.Extensions.Configuration and System.Configuration.
2525
// This is necessary because .NET Framework 4.6.1+ and .NET Core 2.0+ apps support both approaches, whereas the
@@ -71,7 +71,42 @@ public static LoggerConfiguration MSSqlServer(
7171
ColumnOptions columnOptions = null,
7272
IConfigurationSection columnOptionsSection = null,
7373
string schemaName = "dbo",
74-
ITextFormatter logEventFormatter = null)
74+
ITextFormatter logEventFormatter = null) =>
75+
loggerConfiguration.MSSqlServerInternal(
76+
connectionString: connectionString,
77+
tableName: tableName,
78+
appConfiguration: appConfiguration,
79+
restrictedToMinimumLevel: restrictedToMinimumLevel,
80+
batchPostingLimit: batchPostingLimit,
81+
period: period,
82+
formatProvider: formatProvider,
83+
autoCreateSqlTable: autoCreateSqlTable,
84+
columnOptions: columnOptions,
85+
columnOptionsSection: columnOptionsSection,
86+
schemaName: schemaName,
87+
logEventFormatter: logEventFormatter,
88+
applySystemConfiguration: new ApplySystemConfiguration(),
89+
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
90+
sinkFactory: new MSSqlServerSinkFactory());
91+
92+
// Internal overload with parameters used by tests to override the config section and inject mocks
93+
internal static LoggerConfiguration MSSqlServerInternal(
94+
this LoggerSinkConfiguration loggerConfiguration,
95+
string connectionString,
96+
string tableName,
97+
IConfiguration appConfiguration = null,
98+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
99+
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
100+
TimeSpan? period = null,
101+
IFormatProvider formatProvider = null,
102+
bool autoCreateSqlTable = false,
103+
ColumnOptions columnOptions = null,
104+
IConfigurationSection columnOptionsSection = null,
105+
string schemaName = "dbo",
106+
ITextFormatter logEventFormatter = null,
107+
IApplySystemConfiguration applySystemConfiguration = null,
108+
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
109+
IMSSqlServerSinkFactory sinkFactory = null)
75110
{
76111
if (loggerConfiguration == null)
77112
throw new ArgumentNullException(nameof(loggerConfiguration));
@@ -80,35 +115,30 @@ public static LoggerConfiguration MSSqlServer(
80115
var colOpts = columnOptions ?? new ColumnOptions();
81116
var connStr = connectionString;
82117

83-
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
118+
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
119+
if (serviceConfigSection != null)
84120
{
85-
var systemConfiguration = new ApplySystemConfiguration();
86-
colOpts = systemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
87-
connStr = systemConfiguration.GetConnectionString(connStr);
121+
colOpts = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
122+
connStr = applySystemConfiguration.GetConnectionString(connStr);
88123

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

93-
if (appConfiguration != null || columnOptionsSection != null)
128+
if (appConfiguration != null)
129+
{
130+
connStr = applyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
131+
}
132+
133+
if (columnOptionsSection != null)
94134
{
95-
var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
96-
connStr = microsoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
97-
colOpts = microsoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
135+
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
98136
}
99137

100-
return loggerConfiguration.Sink(
101-
new MSSqlServerSink(
102-
connStr,
103-
tableName,
104-
batchPostingLimit,
105-
defaultedPeriod,
106-
formatProvider,
107-
autoCreateSqlTable,
108-
colOpts,
109-
schemaName,
110-
logEventFormatter),
111-
restrictedToMinimumLevel);
138+
var sink = sinkFactory.Create(connStr, tableName, batchPostingLimit, defaultedPeriod, formatProvider,
139+
autoCreateSqlTable, colOpts, schemaName, logEventFormatter);
140+
141+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
112142
}
113143

114144
/// <summary>
@@ -138,41 +168,69 @@ public static LoggerConfiguration MSSqlServer(
138168
ColumnOptions columnOptions = null,
139169
IConfigurationSection columnOptionsSection = null,
140170
string schemaName = "dbo",
141-
ITextFormatter logEventFormatter = null)
171+
ITextFormatter logEventFormatter = null) =>
172+
loggerAuditSinkConfiguration.MSSqlServerInternal(
173+
connectionString: connectionString,
174+
tableName: tableName,
175+
appConfiguration: appConfiguration,
176+
restrictedToMinimumLevel: restrictedToMinimumLevel,
177+
formatProvider: formatProvider,
178+
autoCreateSqlTable: autoCreateSqlTable,
179+
columnOptions: columnOptions,
180+
columnOptionsSection: columnOptionsSection,
181+
schemaName: schemaName,
182+
logEventFormatter: logEventFormatter,
183+
applySystemConfiguration: new ApplySystemConfiguration(),
184+
applyMicrosoftExtensionsConfiguration: new ApplyMicrosoftExtensionsConfiguration(),
185+
auditSinkFactory: new MSSqlServerAuditSinkFactory());
186+
187+
// Internal overload with parameters used by tests to override the config section and inject mocks
188+
internal static LoggerConfiguration MSSqlServerInternal(
189+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
190+
string connectionString,
191+
string tableName,
192+
IConfiguration appConfiguration = null,
193+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
194+
IFormatProvider formatProvider = null,
195+
bool autoCreateSqlTable = false,
196+
ColumnOptions columnOptions = null,
197+
IConfigurationSection columnOptionsSection = null,
198+
string schemaName = "dbo",
199+
ITextFormatter logEventFormatter = null,
200+
IApplySystemConfiguration applySystemConfiguration = null,
201+
IApplyMicrosoftExtensionsConfiguration applyMicrosoftExtensionsConfiguration = null,
202+
IMSSqlServerAuditSinkFactory auditSinkFactory = null)
142203
{
143204
if (loggerAuditSinkConfiguration == null)
144205
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
145206

146207
var colOpts = columnOptions ?? new ColumnOptions();
147208
var connStr = connectionString;
148209

149-
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
210+
var serviceConfigSection = applySystemConfiguration.GetSinkConfigurationSection(AppConfigSectionName);
211+
if (serviceConfigSection != null)
150212
{
151-
var systemConfiguration = new ApplySystemConfiguration();
152-
colOpts = systemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
153-
connStr = systemConfiguration.GetConnectionString(connStr);
213+
colOpts = applySystemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
214+
connStr = applySystemConfiguration.GetConnectionString(connStr);
154215

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

159-
if (appConfiguration != null || columnOptionsSection != null)
220+
if (appConfiguration != null)
160221
{
161-
var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
162-
connStr = microsoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
163-
colOpts = microsoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
222+
connStr = applyMicrosoftExtensionsConfiguration.GetConnectionString(connStr, appConfiguration);
164223
}
165224

166-
return loggerAuditSinkConfiguration.Sink(
167-
new MSSqlServerAuditSink(
168-
connStr,
169-
tableName,
170-
formatProvider,
171-
autoCreateSqlTable,
172-
colOpts,
173-
schemaName,
174-
logEventFormatter),
175-
restrictedToMinimumLevel);
225+
if (columnOptionsSection != null)
226+
{
227+
colOpts = applyMicrosoftExtensionsConfiguration.ConfigureColumnOptions(colOpts, columnOptionsSection);
228+
}
229+
230+
var auditSink = auditSinkFactory.Create(connStr, tableName, formatProvider, autoCreateSqlTable,
231+
colOpts, schemaName, logEventFormatter);
232+
233+
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
176234
}
177235
}
178236
}

src/Serilog.Sinks.MSSqlServer/Configuration/Extensions/Microsoft.Extensions.Configuration/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Serilog.Events;
1919
using Serilog.Formatting;
2020
using Serilog.Sinks.MSSqlServer;
21+
using Serilog.Sinks.MSSqlServer.Configuration.Factories;
2122

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

@@ -69,22 +70,15 @@ public static LoggerConfiguration MSSqlServer(
6970

7071
var defaultedPeriod = period ?? MSSqlServerSink.DefaultPeriod;
7172

72-
var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
73+
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
7374
var connectionStr = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
7475
var colOpts = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
7576

76-
return loggerConfiguration.Sink(
77-
new MSSqlServerSink(
78-
connectionStr,
79-
tableName,
80-
batchPostingLimit,
81-
defaultedPeriod,
82-
formatProvider,
83-
autoCreateSqlTable,
84-
colOpts,
85-
schemaName,
86-
logEventFormatter),
87-
restrictedToMinimumLevel);
77+
IMSSqlServerSinkFactory sinkFactory = new MSSqlServerSinkFactory();
78+
var sink = sinkFactory.Create(connectionStr, tableName, batchPostingLimit, defaultedPeriod,
79+
formatProvider, autoCreateSqlTable, colOpts, schemaName, logEventFormatter);
80+
81+
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
8882
}
8983

9084
/// <summary>
@@ -119,20 +113,15 @@ public static LoggerConfiguration MSSqlServer(
119113
if (loggerAuditSinkConfiguration == null)
120114
throw new ArgumentNullException(nameof(loggerAuditSinkConfiguration));
121115

122-
var microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
116+
IApplyMicrosoftExtensionsConfiguration microsoftExtensionsConfiguration = new ApplyMicrosoftExtensionsConfiguration();
123117
connectionString = microsoftExtensionsConfiguration.GetConnectionString(connectionString, appConfiguration);
124118
columnOptions = microsoftExtensionsConfiguration.ConfigureColumnOptions(columnOptions, columnOptionsSection);
125119

126-
return loggerAuditSinkConfiguration.Sink(
127-
new MSSqlServerAuditSink(
128-
connectionString,
129-
tableName,
130-
formatProvider,
131-
autoCreateSqlTable,
132-
columnOptions,
133-
schemaName,
134-
logEventFormatter),
135-
restrictedToMinimumLevel);
120+
IMSSqlServerAuditSinkFactory auditSinkFactory = new MSSqlServerAuditSinkFactory();
121+
var auditSink = auditSinkFactory.Create(connectionString, tableName, formatProvider, autoCreateSqlTable,
122+
columnOptions, schemaName, logEventFormatter);
123+
124+
return loggerAuditSinkConfiguration.Sink(auditSink, restrictedToMinimumLevel);
136125
}
137126
}
138127
}

0 commit comments

Comments
 (0)