Skip to content

Commit 58b0a5a

Browse files
committed
Fixed all remaining code analysis warnings.
1 parent bc00ffa commit 58b0a5a

File tree

6 files changed

+144
-89
lines changed

6 files changed

+144
-89
lines changed

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))

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/JsonLogEventFormatterTests.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ public void FormatTimeStampColumnTypeDateTimeOffsetUtcRendersCorrectTimeStamp()
2929
const string expectedResult = "{\"TimeStamp\":\"2020-03-27T14:17:00.0000000+00:00\",\"Level\":\"Information\",\"Message\":\"\",\"MessageTemplate\":\"Test message template\"}";
3030
_testTraits.ColumnOptions.TimeStamp.DataType = SqlDbType.DateTimeOffset;
3131
var testLogEvent = CreateTestLogEvent(new DateTimeOffset(2020, 3, 27, 14, 17, 0, TimeSpan.Zero));
32-
var outputWriter = new StringWriter();
3332

3433
// act
35-
_sut.Format(testLogEvent, outputWriter);
34+
string renderResult;
35+
using (var outputWriter = new StringWriter())
36+
{
37+
_sut.Format(testLogEvent, outputWriter);
38+
renderResult = outputWriter.ToString();
39+
}
3640

3741
// assert
38-
var renderResult = outputWriter.ToString();
3942
Assert.Equal(expectedResult, renderResult);
4043
}
4144

@@ -47,13 +50,16 @@ public void FormatTimeStampColumnTypeDateTimeOffsetLocalRendersCorrectTimeStamp(
4750
const string expectedResult = "{\"TimeStamp\":\"2020-03-27T13:17:00.0000000+01:00\",\"Level\":\"Information\",\"Message\":\"\",\"MessageTemplate\":\"Test message template\"}";
4851
_testTraits.ColumnOptions.TimeStamp.DataType = SqlDbType.DateTimeOffset;
4952
var testLogEvent = CreateTestLogEvent(new DateTimeOffset(2020, 3, 27, 13, 17, 0, new TimeSpan(1, 0, 0)));
50-
var outputWriter = new StringWriter();
5153

5254
// act
53-
_sut.Format(testLogEvent, outputWriter);
55+
string renderResult;
56+
using (var outputWriter = new StringWriter())
57+
{
58+
_sut.Format(testLogEvent, outputWriter);
59+
renderResult = outputWriter.ToString();
60+
}
5461

5562
// assert
56-
var renderResult = outputWriter.ToString();
5763
Assert.Equal(expectedResult, renderResult);
5864
}
5965

@@ -64,13 +70,16 @@ public void FormatTimeStampColumnTypeDateTimeRendersCorrectTimeStamp()
6470
// arrange
6571
const string expectedResult = "{\"TimeStamp\":\"2020-03-27T14:17:00.0000000\",\"Level\":\"Information\",\"Message\":\"\",\"MessageTemplate\":\"Test message template\"}";
6672
var testLogEvent = CreateTestLogEvent(new DateTimeOffset(2020, 3, 27, 14, 17, 0, TimeSpan.Zero));
67-
var outputWriter = new StringWriter();
6873

6974
// act
70-
_sut.Format(testLogEvent, outputWriter);
75+
string renderResult;
76+
using (var outputWriter = new StringWriter())
77+
{
78+
_sut.Format(testLogEvent, outputWriter);
79+
renderResult = outputWriter.ToString();
80+
}
7181

7282
// assert
73-
var renderResult = outputWriter.ToString();
7483
Assert.Equal(expectedResult, renderResult);
7584
}
7685

@@ -85,13 +94,16 @@ public void FormatWithPropertiesRendersCorrectProperties()
8594
new LogEventProperty("TestProperty2", new ScalarValue(2))
8695
};
8796
var testLogEvent = CreateTestLogEvent(new DateTimeOffset(2020, 3, 27, 14, 17, 0, TimeSpan.Zero), properties);
88-
var outputWriter = new StringWriter();
8997

9098
// act
91-
_sut.Format(testLogEvent, outputWriter);
99+
string renderResult;
100+
using (var outputWriter = new StringWriter())
101+
{
102+
_sut.Format(testLogEvent, outputWriter);
103+
renderResult = outputWriter.ToString();
104+
}
92105

93106
// assert
94-
var renderResult = outputWriter.ToString();
95107
Assert.Equal(expectedResult, renderResult);
96108
}
97109

@@ -107,13 +119,16 @@ public void FormatWithExcludeStandardColumnsWithPropertiesRendersCorrectProperti
107119
new LogEventProperty("TestProperty2", new ScalarValue(2))
108120
};
109121
var testLogEvent = CreateTestLogEvent(new DateTimeOffset(2020, 3, 27, 14, 17, 0, TimeSpan.Zero), properties);
110-
var outputWriter = new StringWriter();
111122

112123
// act
113-
_sut.Format(testLogEvent, outputWriter);
124+
string renderResult;
125+
using (var outputWriter = new StringWriter())
126+
{
127+
_sut.Format(testLogEvent, outputWriter);
128+
renderResult = outputWriter.ToString();
129+
}
114130

115131
// assert
116-
var renderResult = outputWriter.ToString();
117132
Assert.Equal(expectedResult, renderResult);
118133
}
119134

0 commit comments

Comments
 (0)