Skip to content

Commit 31e9ea4

Browse files
authored
Merge pull request #252 from ckadluba/refactoring-cleanup-tests
Fixed code analysis warnings and set version 5.3.0
2 parents 23c9d79 + 41d47ec commit 31e9ea4

File tree

9 files changed

+200
-100
lines changed

9 files changed

+200
-100
lines changed

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

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using Serilog.Sinks.MSSqlServer;
1919
using System.Configuration;
2020
using Serilog.Formatting;
21-
using System.Diagnostics.CodeAnalysis;
2221

2322
// System.Configuration support for .NET Framework 4.5.2 libraries and apps.
2423

@@ -32,9 +31,7 @@ public static class LoggerConfigurationMSSqlServerExtensions
3231
/// <summary>
3332
/// The configuration section name for app.config or web.config configuration files.
3433
/// </summary>
35-
// TODO: Make this non-static. It could cause race condition in unit tests!
36-
[SuppressMessage("Usage", "CA2211:Non-constant fields should not be visible", Justification = "Too hard to change. Accepted for now.")]
37-
public static string AppConfigSectionName = "MSSqlServerSettingsSection";
34+
public const string AppConfigSectionName = "MSSqlServerSettingsSection";
3835

3936
/// <summary>
4037
/// Adds a sink that writes log events to a table in a MSSqlServer database.
@@ -66,6 +63,33 @@ public static LoggerConfiguration MSSqlServer(
6663
bool autoCreateSqlTable = false,
6764
ColumnOptions columnOptions = null,
6865
string schemaName = "dbo",
66+
ITextFormatter logEventFormatter = null) =>
67+
loggerConfiguration.MSSqlServer(
68+
configSectionName: AppConfigSectionName,
69+
connectionString,
70+
tableName,
71+
restrictedToMinimumLevel,
72+
batchPostingLimit,
73+
period,
74+
formatProvider,
75+
autoCreateSqlTable,
76+
columnOptions,
77+
schemaName,
78+
logEventFormatter);
79+
80+
// Internal overload with parameter configSectionName used by tests to override the config section
81+
internal static LoggerConfiguration MSSqlServer(
82+
this LoggerSinkConfiguration loggerConfiguration,
83+
string configSectionName,
84+
string connectionString,
85+
string tableName,
86+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
87+
int batchPostingLimit = MSSqlServerSink.DefaultBatchPostingLimit,
88+
TimeSpan? period = null,
89+
IFormatProvider formatProvider = null,
90+
bool autoCreateSqlTable = false,
91+
ColumnOptions columnOptions = null,
92+
string schemaName = "dbo",
6993
ITextFormatter logEventFormatter = null)
7094
{
7195
if (loggerConfiguration == null)
@@ -75,7 +99,7 @@ public static LoggerConfiguration MSSqlServer(
7599
var colOpts = columnOptions ?? new ColumnOptions();
76100

77101
var systemConfiguration = new ApplySystemConfiguration();
78-
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
102+
if (ConfigurationManager.GetSection(configSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
79103
colOpts = systemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
80104

81105
connectionString = systemConfiguration.GetConnectionString(connectionString);
@@ -120,6 +144,29 @@ public static LoggerConfiguration MSSqlServer(
120144
bool autoCreateSqlTable = false,
121145
ColumnOptions columnOptions = null,
122146
string schemaName = "dbo",
147+
ITextFormatter logEventFormatter = null) =>
148+
loggerAuditSinkConfiguration.MSSqlServer(
149+
configSectionName: AppConfigSectionName,
150+
connectionString,
151+
tableName,
152+
restrictedToMinimumLevel,
153+
formatProvider,
154+
autoCreateSqlTable,
155+
columnOptions,
156+
schemaName,
157+
logEventFormatter);
158+
159+
// Internal overload with parameter configSectionName used by tests to override the config section
160+
internal static LoggerConfiguration MSSqlServer(
161+
this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration,
162+
string configSectionName,
163+
string connectionString,
164+
string tableName,
165+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
166+
IFormatProvider formatProvider = null,
167+
bool autoCreateSqlTable = false,
168+
ColumnOptions columnOptions = null,
169+
string schemaName = "dbo",
123170
ITextFormatter logEventFormatter = null)
124171
{
125172
if (loggerAuditSinkConfiguration == null)
@@ -128,7 +175,7 @@ public static LoggerConfiguration MSSqlServer(
128175
var colOpts = columnOptions ?? new ColumnOptions();
129176

130177
var systemConfiguration = new ApplySystemConfiguration();
131-
if (ConfigurationManager.GetSection(AppConfigSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
178+
if (ConfigurationManager.GetSection(configSectionName) is MSSqlServerConfigurationSection serviceConfigSection)
132179
colOpts = systemConfiguration.ConfigureColumnOptions(serviceConfigSection, colOpts);
133180

134181
connectionString = systemConfiguration.GetConnectionString(connectionString);

src/Serilog.Sinks.MSSqlServer/Serilog.Sinks.MSSqlServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>A Serilog sink that writes events to Microsoft SQL Server</Description>
5-
<VersionPrefix>5.2.1</VersionPrefix>
5+
<VersionPrefix>5.3.0</VersionPrefix>
66
<Authors>Michiel van Oudheusden;Serilog Contributors</Authors>
77
<TargetFrameworks>netstandard2.0;net452;netcoreapp2.0;net461</TargetFrameworks>
88
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

test/Serilog.Sinks.MSSqlServer.Tests/Configuration/Extensions/System.Configuration/ConfigurationExtensionsTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public void CustomStandardColumnNames()
4444
{
4545
var standardNames = new List<string> { "CustomMessage", "CustomMessageTemplate", "CustomLevel", "CustomTimeStamp", "CustomException", "CustomProperties" };
4646

47-
LoggerConfigurationMSSqlServerExtensions.AppConfigSectionName = "CustomStandardColumnNames";
48-
4947
var loggerConfiguration = new LoggerConfiguration();
5048
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
49+
configSectionName: "CustomStandardColumnNames",
5150
connectionString: DatabaseFixture.LogEventsConnectionString,
5251
tableName: DatabaseFixture.LogTableName,
5352
autoCreateSqlTable: true)
@@ -71,10 +70,9 @@ public void CustomStandardColumnNames()
7170
[Fact]
7271
public void CustomizedColumnList()
7372
{
74-
LoggerConfigurationMSSqlServerExtensions.AppConfigSectionName = "CustomizedColumnList";
75-
7673
var loggerConfiguration = new LoggerConfiguration();
7774
Log.Logger = loggerConfiguration.WriteTo.MSSqlServer(
75+
configSectionName: "CustomizedColumnList",
7876
connectionString: DatabaseFixture.LogEventsConnectionString,
7977
tableName: DatabaseFixture.LogTableName,
8078
autoCreateSqlTable: true)

test/Serilog.Sinks.MSSqlServer.Tests/CustomStandardColumnNamesTests.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void CustomIdColumn()
2626
options.Id.ColumnName = customIdName;
2727

2828
// act
29-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null);
29+
using (var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null))
30+
{ }
3031

3132
// assert
3233
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -52,7 +53,8 @@ public void DefaultIdColumn()
5253
var options = new ColumnOptions();
5354

5455
// act
55-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null);
56+
using (var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null))
57+
{ }
5658

5759
// assert
5860
var idColumnName = "Id";
@@ -87,7 +89,8 @@ public void TableCreatedWithCustomNames()
8789
options.Properties.ColumnName = "CustomProperties";
8890

8991
// act
90-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null);
92+
using (var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null))
93+
{ }
9194

9295
// assert
9396
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -112,7 +115,8 @@ public void TableCreatedWithDefaultNames()
112115
var standardNames = new List<string> { "Message", "MessageTemplate", "Level", "TimeStamp", "Exception", "Properties" };
113116

114117
// act
115-
var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null);
118+
using (var sink = new MSSqlServerSink(DatabaseFixture.LogEventsConnectionString, DatabaseFixture.LogTableName, 1, TimeSpan.FromSeconds(1), null, true, options, "dbo", null))
119+
{ }
116120

117121
// assert
118122
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -149,13 +153,15 @@ public void WriteEventToCustomStandardColumns()
149153
columnOptions: options)
150154
.CreateLogger();
151155

152-
var file = File.CreateText("CustomColumnsEvent.Self.log");
153-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
154156

155157
// act
156158
const string loggingInformationMessage = "Logging Information message";
157-
Log.Information(loggingInformationMessage);
158-
Log.CloseAndFlush();
159+
using (var file = File.CreateText("CustomColumnsEvent.Self.log"))
160+
{
161+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
162+
Log.Information(loggingInformationMessage);
163+
Log.CloseAndFlush();
164+
}
159165

160166
// assert
161167
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -180,14 +186,15 @@ public void WriteEventToDefaultStandardColumns()
180186
columnOptions: new ColumnOptions())
181187
.CreateLogger();
182188

183-
var file = File.CreateText("StandardColumns.Self.log");
184-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
185189

186190
// act
187191
const string loggingInformationMessage = "Logging Information message";
188-
Log.Information(loggingInformationMessage);
189-
190-
Log.CloseAndFlush();
192+
using (var file = File.CreateText("StandardColumns.Self.log"))
193+
{
194+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
195+
Log.Information(loggingInformationMessage);
196+
Log.CloseAndFlush();
197+
}
191198

192199
// assert
193200
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -220,13 +227,15 @@ public void AuditEventToCustomStandardColumns()
220227
columnOptions: options)
221228
.CreateLogger();
222229

223-
var file = File.CreateText("CustomColumnsAuditEvent.Self.log");
224-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
225230

226231
// act
227232
const string loggingInformationMessage = "Logging Information message";
228-
Log.Information(loggingInformationMessage);
229-
Log.CloseAndFlush();
233+
using (var file = File.CreateText("CustomColumnsAuditEvent.Self.log"))
234+
{
235+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
236+
Log.Information(loggingInformationMessage);
237+
Log.CloseAndFlush();
238+
}
230239

231240
// assert
232241
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -249,14 +258,15 @@ public void AuditEventToDefaultStandardColumns()
249258
columnOptions: new ColumnOptions())
250259
.CreateLogger();
251260

252-
var file = File.CreateText("StandardColumns.Audit.Self.log");
253-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
254-
255261
// act
256262
const string loggingInformationMessage = "Logging Information message";
257-
Log.Information(loggingInformationMessage);
263+
using (var file = File.CreateText("StandardColumns.Audit.Self.log"))
264+
{
265+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
258266

259-
Log.CloseAndFlush();
267+
Log.Information(loggingInformationMessage);
268+
Log.CloseAndFlush();
269+
}
260270

261271
// assert
262272
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))

test/Serilog.Sinks.MSSqlServer.Tests/LevelAsEnumTests.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ public void CanStoreLevelAsEnum()
3030
columnOptions: new ColumnOptions { Level = { StoreAsEnum = true } })
3131
.CreateLogger();
3232

33-
var file = File.CreateText("LevelAsEnum.True.Enum.Self.log");
34-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
35-
3633
// act
3734
const string loggingInformationMessage = "Logging Information message";
38-
Log.Information(loggingInformationMessage);
39-
Log.CloseAndFlush();
35+
using (var file = File.CreateText("LevelAsEnum.True.Enum.Self.log"))
36+
{
37+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
38+
Log.Information(loggingInformationMessage);
39+
Log.CloseAndFlush();
40+
}
4041

4142
// assert
4243
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -61,13 +62,14 @@ public void CanStoreLevelAsString()
6162
columnOptions: new ColumnOptions { Level = { StoreAsEnum = false } })
6263
.CreateLogger();
6364

64-
var file = File.CreateText("LevelAsEnum.False.Self.log");
65-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
66-
6765
// act
6866
const string loggingInformationMessage = "Logging Information message";
69-
Log.Information(loggingInformationMessage);
70-
Log.CloseAndFlush();
67+
using (var file = File.CreateText("LevelAsEnum.False.Self.log"))
68+
{
69+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
70+
Log.Information(loggingInformationMessage);
71+
Log.CloseAndFlush();
72+
}
7173

7274
// assert
7375
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -90,13 +92,14 @@ public void AuditCanStoreLevelAsEnum()
9092
columnOptions: new ColumnOptions { Level = { StoreAsEnum = true } })
9193
.CreateLogger();
9294

93-
var file = File.CreateText("LevelAsEnum.Audit.True.Enum.Self.log");
94-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
95-
9695
// act
9796
const string loggingInformationMessage = "Logging Information message";
98-
Log.Information(loggingInformationMessage);
99-
Log.CloseAndFlush();
97+
using (var file = File.CreateText("LevelAsEnum.Audit.True.Enum.Self.log"))
98+
{
99+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
100+
Log.Information(loggingInformationMessage);
101+
Log.CloseAndFlush();
102+
}
100103

101104
// assert
102105
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))
@@ -119,13 +122,14 @@ public void AuditCanStoreLevelAsString()
119122
columnOptions: new ColumnOptions { Level = { StoreAsEnum = false } })
120123
.CreateLogger();
121124

122-
var file = File.CreateText("LevelAsEnum.Audit.False.Self.log");
123-
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
124-
125125
// act
126126
const string loggingInformationMessage = "Logging Information message";
127-
Log.Information(loggingInformationMessage);
128-
Log.CloseAndFlush();
127+
using (var file = File.CreateText("LevelAsEnum.Audit.False.Self.log"))
128+
{
129+
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
130+
Log.Information(loggingInformationMessage);
131+
Log.CloseAndFlush();
132+
}
129133

130134
// assert
131135
using (var conn = new SqlConnection(DatabaseFixture.LogEventsConnectionString))

0 commit comments

Comments
 (0)