Skip to content

Commit e6655c2

Browse files
committed
Use pattern matching where appropriate
* Simpler code * Uncovers some untested code paths
1 parent 733d811 commit e6655c2

6 files changed

+61
-7
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void WriteEvent(LogEvent logEvent, XmlWriter writer)
106106
WriteEventAttribute(logEvent, writer, "thread", ThreadIdPropertyName);
107107
WriteDomainAndUserName(logEvent, writer);
108108
var properties = logEvent.Properties.Where(e => !SpecialProperties.Contains(e.Key)).ToList();
109-
var hasMachineNameProperty = logEvent.Properties.TryGetValue(MachineNamePropertyName, out var machineNameProperty) && machineNameProperty is ScalarValue scalarValue && scalarValue.Value != null;
109+
var hasMachineNameProperty = logEvent.Properties.TryGetValue(MachineNamePropertyName, out var machineNameProperty) && machineNameProperty is ScalarValue { Value: not null };
110110
if (properties.Any() || hasMachineNameProperty)
111111
{
112112
WriteProperties(logEvent, writer, properties, machineNameProperty);
@@ -123,7 +123,7 @@ private void WriteEvent(LogEvent logEvent, XmlWriter writer)
123123
/// <param name="writer">The XML writer.</param>
124124
private static void WriteDomainAndUserName(LogEvent logEvent, XmlWriter writer)
125125
{
126-
if (logEvent.Properties.TryGetValue(UserNamePropertyName, out var propertyValue) && propertyValue is ScalarValue scalarValue && scalarValue.Value is string userNameProperty)
126+
if (logEvent.Properties.TryGetValue(UserNamePropertyName, out var propertyValue) && propertyValue is ScalarValue { Value: string userNameProperty })
127127
{
128128
// See https://github.com/serilog/serilog-enrichers-environment/blob/3fc7cf78c5f34816633000ae74d846033498e44b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs#L53
129129
var parts = userNameProperty.Split('\\');
@@ -149,9 +149,9 @@ private static void WriteDomainAndUserName(LogEvent logEvent, XmlWriter writer)
149149
/// <remarks>Only properties with a non null <see cref="ScalarValue"/> are supported, other types are ignored.</remarks>
150150
private void WriteEventAttribute(LogEvent logEvent, XmlWriter writer, string attributeName, string propertyName)
151151
{
152-
if (logEvent.Properties.TryGetValue(propertyName, out var propertyValue) && propertyValue is ScalarValue scalarValue && scalarValue.Value != null)
152+
if (logEvent.Properties.TryGetValue(propertyName, out var propertyValue) && propertyValue is ScalarValue { Value: not null })
153153
{
154-
writer.WriteAttributeString(attributeName, RenderValue(scalarValue));
154+
writer.WriteAttributeString(attributeName, RenderValue(propertyValue));
155155
}
156156
}
157157

@@ -225,7 +225,7 @@ private string RenderValue(LogEventPropertyValue value)
225225
{
226226
using var valueWriter = new StringWriter();
227227
// The "l" format specifier switches off quoting of strings, see https://github.com/serilog/serilog/wiki/Formatting-Output#formatting-plain-text
228-
value.Render(valueWriter, value is ScalarValue scalarValue && scalarValue.Value is string ? "l" : null, _options.FormatProvider);
228+
value.Render(valueWriter, value is ScalarValue { Value: string } ? "l" : null, _options.FormatProvider);
229229
return valueWriter.ToString();
230230
}
231231

@@ -321,7 +321,7 @@ private void WritePropertyElement(XmlWriter writer, string name, LogEventPropert
321321
{
322322
WriteStartElement(writer, "data");
323323
writer.WriteAttributeString("name", name);
324-
var isNullValue = value is ScalarValue scalarValue && scalarValue.Value == null;
324+
var isNullValue = value is ScalarValue { Value: null };
325325
if (!isNullValue)
326326
{
327327
writer.WriteAttributeString("value", RenderValue(value));

src/Serilog.Formatting.Log4Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<PropertyGroup Label="Compiling">
8-
<LangVersion>8.0</LangVersion>
8+
<LangVersion>9.0</LangVersion>
99
<Nullable>enable</Nullable>
1010
<AnalysisLevel>latest</AnalysisLevel>
1111
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<log4net:event timestamp="2003-01-04T15:09:26.535+01:00" level="INFO" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2/">
2+
<log4net:message><![CDATA[Hello from Serilog]]></log4net:message>
3+
</log4net:event>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<log4net:event timestamp="2003-01-04T15:09:26.535+01:00" level="INFO" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2/">
2+
<log4net:message><![CDATA[Hello from Serilog]]></log4net:message>
3+
</log4net:event>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<log4net:event timestamp="2003-01-04T15:09:26.535+01:00" level="INFO" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2/">
2+
<log4net:message><![CDATA[Hello from Serilog]]></log4net:message>
3+
</log4net:event>

tests/Log4NetTextFormatterTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,21 @@ public void LoggerName()
258258
Approvals.VerifyWithExtension(output.ToString(), "xml");
259259
}
260260

261+
[Fact]
262+
public void LoggerNameStructureValue()
263+
{
264+
// Arrange
265+
using var output = new StringWriter();
266+
var logEvent = CreateLogEvent(properties: new LogEventProperty(Constants.SourceContextPropertyName, new StructureValue(new List<LogEventProperty>())));
267+
var formatter = new Log4NetTextFormatter();
268+
269+
// Act
270+
formatter.Format(logEvent, output);
271+
272+
// Assert
273+
Approvals.VerifyWithExtension(output.ToString(), "xml");
274+
}
275+
261276
[Fact]
262277
public void DefaultFormatProvider()
263278
{
@@ -503,6 +518,21 @@ public void DomainAndUserNameProperty(string? environmentUserName)
503518
Approvals.VerifyWithExtension(output.ToString(), "xml");
504519
}
505520

521+
[Fact]
522+
public void DomainAndUserNamePropertyStructureValue()
523+
{
524+
// Arrange
525+
using var output = new StringWriter();
526+
var logEvent = CreateLogEvent(properties: new LogEventProperty(EnvironmentUserNameEnricher.EnvironmentUserNamePropertyName, new StructureValue(new List<LogEventProperty>())));
527+
var formatter = new Log4NetTextFormatter();
528+
529+
// Act
530+
formatter.Format(logEvent, output);
531+
532+
// Assert
533+
Approvals.VerifyWithExtension(output.ToString(), "xml");
534+
}
535+
506536
[Theory]
507537
[InlineData(null)]
508538
[InlineData("TheMachineName")]
@@ -522,6 +552,21 @@ public void MachineNameProperty(string? machineName)
522552
Approvals.VerifyWithExtension(output.ToString(), "xml");
523553
}
524554

555+
[Fact]
556+
public void MachineNamePropertyStructureValue()
557+
{
558+
// Arrange
559+
using var output = new StringWriter();
560+
var logEvent = CreateLogEvent(properties: new LogEventProperty(MachineNameEnricher.MachineNamePropertyName, new StructureValue(new List<LogEventProperty>())));
561+
var formatter = new Log4NetTextFormatter();
562+
563+
// Act
564+
formatter.Format(logEvent, output);
565+
566+
// Assert
567+
Approvals.VerifyWithExtension(output.ToString(), "xml");
568+
}
569+
525570
[Fact]
526571
public void SequenceProperty()
527572
{

0 commit comments

Comments
 (0)