Skip to content

Commit 8dc3b53

Browse files
committed
Properly dispose StringWriter in unit tests
1 parent 901699a commit 8dc3b53

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

tests/Log4NetTextFormatterTest.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void LogEventLevel(LogEventLevel level)
9191
NamerFactory.AdditionalInformation = level.ToString();
9292

9393
// Arrange
94-
var output = new StringWriter();
94+
using var output = new StringWriter();
9595
var logEvent = CreateLogEvent(level);
9696
var formatter = new Log4NetTextFormatter();
9797

@@ -106,7 +106,7 @@ public void LogEventLevel(LogEventLevel level)
106106
public void InvalidLogEventLevelThrowsArgumentOutOfRangeException()
107107
{
108108
// Arrange
109-
var output = new StringWriter();
109+
using var output = new StringWriter();
110110
var logEvent = CreateLogEvent((LogEventLevel)(-1));
111111
var formatter = new Log4NetTextFormatter();
112112

@@ -128,7 +128,7 @@ public void CDataMode(CDataMode mode, bool needsEscaping)
128128
NamerFactory.AdditionalInformation = mode + "." + (needsEscaping ? "NeedsEscaping" : "DoesntNeedEscaping");
129129

130130
// Arrange
131-
var output = new StringWriter();
131+
using var output = new StringWriter();
132132
var logEvent = CreateLogEvent(messageTemplate: needsEscaping ? ">> Hello from Serilog <<" : "Hello from Serilog");
133133
var formatter = new Log4NetTextFormatter(options => options.UseCDataMode(mode));
134134

@@ -149,7 +149,7 @@ public void LineEnding(LineEnding lineEnding)
149149
NamerFactory.AdditionalInformation = lineEnding.ToString();
150150

151151
// Arrange
152-
var output = new StringWriter();
152+
using var output = new StringWriter();
153153
var logEvent = CreateLogEvent();
154154
var formatter = new Log4NetTextFormatter(options => options.UseLineEnding(lineEnding));
155155

@@ -170,7 +170,7 @@ public void IndentationSettings(Indentation indentation, byte size)
170170
NamerFactory.AdditionalInformation = indentation + "." + size;
171171

172172
// Arrange
173-
var output = new StringWriter();
173+
using var output = new StringWriter();
174174
var logEvent = CreateLogEvent();
175175
var formatter = new Log4NetTextFormatter(options => options.UseIndentationSettings(new IndentationSettings(indentation, size)));
176176

@@ -185,7 +185,7 @@ public void IndentationSettings(Indentation indentation, byte size)
185185
public void NoIndentation()
186186
{
187187
// Arrange
188-
var output = new StringWriter();
188+
using var output = new StringWriter();
189189
var logEvent = CreateLogEvent();
190190
var formatter = new Log4NetTextFormatter(options => options.UseNoIndentation());
191191

@@ -200,7 +200,7 @@ public void NoIndentation()
200200
public void NoNamespace()
201201
{
202202
// Arrange
203-
var output = new StringWriter();
203+
using var output = new StringWriter();
204204
var logEvent = CreateLogEvent();
205205
var formatter = new Log4NetTextFormatter(options => options.UseLog4NetXmlNamespace(null));
206206

@@ -215,7 +215,7 @@ public void NoNamespace()
215215
public void NullProperty()
216216
{
217217
// Arrange
218-
var output = new StringWriter();
218+
using var output = new StringWriter();
219219
var logEvent = CreateLogEvent(properties: new LogEventProperty("n/a", new ScalarValue(null)));
220220
var formatter = new Log4NetTextFormatter();
221221

@@ -230,7 +230,7 @@ public void NullProperty()
230230
public void DefaultFormatProvider()
231231
{
232232
// Arrange
233-
var output = new StringWriter();
233+
using var output = new StringWriter();
234234
var logEvent = CreateLogEvent(messageTemplate: "π = {π}", properties: new LogEventProperty("π", new ScalarValue(3.14m)));
235235
var formatter = new Log4NetTextFormatter();
236236

@@ -245,7 +245,7 @@ public void DefaultFormatProvider()
245245
public void ExplicitFormatProvider()
246246
{
247247
// Arrange
248-
var output = new StringWriter();
248+
using var output = new StringWriter();
249249
var logEvent = CreateLogEvent(messageTemplate: "π = {π}", properties: new LogEventProperty("π", new ScalarValue(3.14m)));
250250
var formatProvider = new NumberFormatInfo { NumberDecimalSeparator = "," };
251251
var formatter = new Log4NetTextFormatter(options => options.UseFormatProvider(formatProvider));
@@ -261,7 +261,7 @@ public void ExplicitFormatProvider()
261261
public void TwoProperties()
262262
{
263263
// Arrange
264-
var output = new StringWriter();
264+
using var output = new StringWriter();
265265
var logEvent = CreateLogEvent(properties: new[]{
266266
new LogEventProperty("one", new ScalarValue(1)),
267267
new LogEventProperty("two", new ScalarValue(2)),
@@ -279,7 +279,7 @@ public void TwoProperties()
279279
public void TwoPropertiesOneNull()
280280
{
281281
// Arrange
282-
var output = new StringWriter();
282+
using var output = new StringWriter();
283283
var logEvent = CreateLogEvent(properties: new[]{
284284
new LogEventProperty("n/a", new ScalarValue(null)),
285285
new LogEventProperty("one", new ScalarValue(1)),
@@ -297,7 +297,7 @@ public void TwoPropertiesOneNull()
297297
public void FilterProperty()
298298
{
299299
// Arrange
300-
var output = new StringWriter();
300+
using var output = new StringWriter();
301301
var logEvent = CreateLogEvent(properties: new[]{
302302
new LogEventProperty("one", new ScalarValue(1)),
303303
new LogEventProperty("two", new ScalarValue(2)),
@@ -315,7 +315,7 @@ public void FilterProperty()
315315
public void FilterPropertyThrowing()
316316
{
317317
// Arrange
318-
var output = new StringWriter();
318+
using var output = new StringWriter();
319319
var logEvent = CreateLogEvent(properties: new[]{
320320
new LogEventProperty("one", new ScalarValue(1)),
321321
new LogEventProperty("two", new ScalarValue(2)),
@@ -338,7 +338,7 @@ public void FilterPropertyThrowing()
338338
public void TwoEvents()
339339
{
340340
// Arrange
341-
var output = new StringWriter();
341+
using var output = new StringWriter();
342342
var logEvent = CreateLogEvent();
343343
var formatter = new Log4NetTextFormatter();
344344

@@ -354,7 +354,7 @@ public void TwoEvents()
354354
public void Exception()
355355
{
356356
// Arrange
357-
var output = new StringWriter();
357+
using var output = new StringWriter();
358358
var logEvent = CreateLogEvent(exception: new Exception("An error occurred").SetStackTrace(@" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
359359
var formatter = new Log4NetTextFormatter();
360360

@@ -369,7 +369,7 @@ public void Exception()
369369
public void ExceptionFormatter()
370370
{
371371
// Arrange
372-
var output = new StringWriter();
372+
using var output = new StringWriter();
373373
var logEvent = CreateLogEvent(exception: new Exception("An error occurred"));
374374
var formatter = new Log4NetTextFormatter(options => options.UseExceptionFormatter(e => $"Type = {e.GetType().FullName}\nMessage = {e.Message}"));
375375

@@ -384,7 +384,7 @@ public void ExceptionFormatter()
384384
public void ExceptionFormatterReturningNull()
385385
{
386386
// Arrange
387-
var output = new StringWriter();
387+
using var output = new StringWriter();
388388
var logEvent = CreateLogEvent(exception: new Exception("An error occurred"));
389389
var formatter = new Log4NetTextFormatter(options => options.UseExceptionFormatter(e => null!));
390390

@@ -399,7 +399,7 @@ public void ExceptionFormatterReturningNull()
399399
public void ExceptionFormatterThrowing()
400400
{
401401
// Arrange
402-
var output = new StringWriter();
402+
using var output = new StringWriter();
403403
var logEvent = CreateLogEvent(exception: new Exception("An error occurred"));
404404
var formatter = new Log4NetTextFormatter(options => options.UseExceptionFormatter(e => throw new InvalidOperationException("💥 Boom 💥")));
405405

@@ -418,7 +418,7 @@ public void ThreadId(int? threadId)
418418
NamerFactory.AdditionalInformation = threadId?.ToString() ?? "_null";
419419

420420
// Arrange
421-
var output = new StringWriter();
421+
using var output = new StringWriter();
422422
var logEvent = CreateLogEvent(properties: new LogEventProperty(ThreadIdEnricher.ThreadIdPropertyName, new ScalarValue(threadId)));
423423
var formatter = new Log4NetTextFormatter();
424424

@@ -440,7 +440,7 @@ public void DomainAndUserName(string? environmentUserName)
440440
NamerFactory.AdditionalInformation = environmentUserName == null ? "_null" : environmentUserName.Length == 0 ? "_empty" : environmentUserName.Replace(@"\", "_");
441441

442442
// Arrange
443-
var output = new StringWriter();
443+
using var output = new StringWriter();
444444
var logEvent = CreateLogEvent(properties: new LogEventProperty(EnvironmentUserNameEnricher.EnvironmentUserNamePropertyName, new ScalarValue(environmentUserName)));
445445
var formatter = new Log4NetTextFormatter();
446446

@@ -459,7 +459,7 @@ public void MachineName(string? machineName)
459459
NamerFactory.AdditionalInformation = machineName ?? "_null";
460460

461461
// Arrange
462-
var output = new StringWriter();
462+
using var output = new StringWriter();
463463
var logEvent = CreateLogEvent(properties: new LogEventProperty(MachineNameEnricher.MachineNamePropertyName, new ScalarValue(machineName)));
464464
var formatter = new Log4NetTextFormatter();
465465

@@ -474,7 +474,7 @@ public void MachineName(string? machineName)
474474
public void SequenceProperty()
475475
{
476476
// Arrange
477-
var output = new StringWriter();
477+
using var output = new StringWriter();
478478
var values = new LogEventPropertyValue[] { new ScalarValue(1), new ScalarValue("two"), CreateDictionary() };
479479
var logEvent = CreateLogEvent(properties: new LogEventProperty("Sequence", new SequenceValue(values)));
480480
var formatter = new Log4NetTextFormatter();
@@ -490,7 +490,7 @@ public void SequenceProperty()
490490
public void DictionaryProperty()
491491
{
492492
// Arrange
493-
var output = new StringWriter();
493+
using var output = new StringWriter();
494494
var values = new[]
495495
{
496496
new KeyValuePair<ScalarValue, LogEventPropertyValue>(new ScalarValue(1), new ScalarValue("one")),
@@ -511,7 +511,7 @@ public void DictionaryProperty()
511511
public void StructureProperty()
512512
{
513513
// Arrange
514-
var output = new StringWriter();
514+
using var output = new StringWriter();
515515
var values = new[]
516516
{
517517
new LogEventProperty("1", new ScalarValue("one")),

0 commit comments

Comments
 (0)