Skip to content

Commit 901699a

Browse files
committed
Enable code quality analysis and fix reported issues
Note: the documentation on https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#analysislevel says > The latest code analyzers that have been released are used. This is the default. This is true only if you are targeting .NET 5.0 or later. For .NET Standard 2.0, the `AnalysisLevel` must be explicitly set to `latest`.
1 parent 1d1097e commit 901699a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.IO;
45
using System.Linq;
56
using System.Xml;
@@ -72,6 +73,8 @@ public Log4NetTextFormatter(Action<Log4NetTextFormatterOptionsBuilder>? configur
7273
/// <param name="output">The output.</param>
7374
public void Format(LogEvent logEvent, TextWriter output)
7475
{
76+
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
77+
if (output == null) throw new ArgumentNullException(nameof(output));
7578
using var writer = XmlWriter.Create(output, _options.XmlWriterSettings);
7679
WriteEvent(logEvent, writer);
7780
writer.Flush();
@@ -170,6 +173,7 @@ private static string LogLevel(LogEventLevel level)
170173
/// <param name="properties">The collection of properties to write.</param>
171174
/// <param name="machineNameProperty">The machine name property to write or <see langref="null"/> if doesn't exist.</param>
172175
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L262-L286</remarks>
176+
[SuppressMessage("Microsoft.Design", "CA1031", Justification = "Protecting from user-provided code which might throw anything")]
173177
private void WriteProperties(LogEvent logEvent, XmlWriter writer, IEnumerable<KeyValuePair<string, LogEventPropertyValue>> properties, LogEventPropertyValue? machineNameProperty)
174178
{
175179
WriteStartElement(writer, "properties");
@@ -205,7 +209,7 @@ private void WriteProperties(LogEvent logEvent, XmlWriter writer, IEnumerable<Ke
205209
/// <returns>A string representation of the <paramref name="value"/>.</returns>
206210
private string RenderValue(LogEventPropertyValue value)
207211
{
208-
var valueWriter = new StringWriter();
212+
using var valueWriter = new StringWriter();
209213
// The "l" format specifier switches off quoting of strings, see https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text
210214
value.Render(valueWriter, value is ScalarValue scalarValue && scalarValue.Value is string ? "l" : null, _options.FormatProvider);
211215
return valueWriter.ToString();
@@ -328,6 +332,7 @@ private void WriteMessage(LogEvent logEvent, XmlWriter writer)
328332
/// <param name="logEvent">The log event.</param>
329333
/// <param name="writer">The XML writer.</param>
330334
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L288-L295</remarks>
335+
[SuppressMessage("Microsoft.Design", "CA1031", Justification = "Protecting from user-provided code which might throw anything")]
331336
private void WriteException(LogEvent logEvent, XmlWriter writer)
332337
{
333338
var exception = logEvent.Exception;

src/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System;
2+
3+
// Can't use AssemblyAttribute MSBuild item yet, see https://github.com/dotnet/msbuild/issues/2281
4+
[assembly: CLSCompliant(true)]

src/Serilog.Formatting.Log4Net.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<PropertyGroup Label="Compiling">
88
<LangVersion>8.0</LangVersion>
99
<Nullable>enable</Nullable>
10+
<AnalysisLevel>latest</AnalysisLevel>
11+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
1012
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1113
</PropertyGroup>
1214

tests/Log4NetTextFormatterTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ private static LogEvent CreateLogEvent(LogEventLevel level = Events.LogEventLeve
5252
);
5353
}
5454

55+
[Fact]
56+
public void NullLogEventThrowsArgumentNullException()
57+
{
58+
// Arrange
59+
var formatter = new Log4NetTextFormatter();
60+
61+
// Act
62+
Action action = () => formatter.Format(null!, TextWriter.Null);
63+
64+
// Assert
65+
action.Should().ThrowExactly<ArgumentNullException>().Which.ParamName.Should().Be("logEvent");
66+
}
67+
68+
[Fact]
69+
public void NullOutputThrowsArgumentNullException()
70+
{
71+
// Arrange
72+
var logEvent = CreateLogEvent();
73+
var formatter = new Log4NetTextFormatter();
74+
75+
// Act
76+
Action action = () => formatter.Format(logEvent, null!);
77+
78+
// Assert
79+
action.Should().ThrowExactly<ArgumentNullException>().Which.ParamName.Should().Be("output");
80+
}
81+
5582
[Theory]
5683
[InlineData(Events.LogEventLevel.Verbose)]
5784
[InlineData(Events.LogEventLevel.Debug)]

0 commit comments

Comments
 (0)