Skip to content

Commit d6d7123

Browse files
committed
Merge branch 'net8.0' into main
2 parents 5381a2b + cf7be90 commit d6d7123

12 files changed

+453
-88
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"dotnet-stryker": {
12-
"version": "3.11.1",
12+
"version": "4.0.4",
1313
"commands": [
1414
"dotnet-stryker"
1515
]

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased][Unreleased]
8+
9+
- Add support for .NET 8.
10+
711
## [1.1.0][1.1.0] - 2023-05-02
812

913
- Add support for caller information (class, method, file, line) through the [Serilog.Enrichers.WithCaller](https://www.nuget.org/packages/Serilog.Enrichers.WithCaller/) package.
@@ -84,6 +88,7 @@ Still trying to figure out how to make everything fit together with [MinVer](htt
8488

8589
- Implement log4j compatibility mode.
8690

91+
[Unreleased]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.1.0...HEAD
8792
[1.1.0]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.2...1.1.0
8893
[1.0.2]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.1...1.0.2
8994
[1.0.1]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.0...1.0.1

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://json.schemastore.org/global",
33
"sdk": {
4-
"version": "6.0.400",
4+
"version": "8.0.100",
55
"allowPrerelease": false,
66
"rollForward": "latestFeature"
77
}

src/Log4NetTextFormatter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class Log4NetTextFormatter : ITextFormatter
1919
/// <summary>
2020
/// The characters that must be escaped inside an XML element. Used when <see cref="CDataMode"/> is <see cref="CDataMode.IfNeeded"/>.
2121
/// </summary>
22-
private static readonly char[] XmlElementCharactersToEscape = { '&', '<', '>' };
22+
private static readonly char[] XmlElementCharactersToEscape = [ '&', '<', '>' ];
2323

2424
/// <summary>
2525
/// The Serilog properties which have a special mapping in a log4net event and must not be part of the log4net:properties element.
2626
/// </summary>
27-
private static readonly string[] SpecialProperties = {
27+
private static readonly string[] SpecialProperties = [
2828
Constants.SourceContextPropertyName, OutputProperties.MessagePropertyName,
2929
ThreadIdPropertyName, UserNamePropertyName, MachineNamePropertyName, CallerPropertyName
30-
};
30+
];
3131

3232
/// <summary>
3333
/// The name of the thread id property, set by <a href="https://www.nuget.org/packages/Serilog.Enrichers.Thread/">Serilog.Enrichers.Thread</a>
@@ -93,6 +93,7 @@ public Log4NetTextFormatter(Action<Log4NetTextFormatterOptionsBuilder>? configur
9393
/// <param name="logEvent">The event to format.</param>
9494
/// <param name="output">The output.</param>
9595
/// <exception cref="ArgumentNullException">If either <paramref name="logEvent"/> or <paramref name="output"/> is <c>null</c>.</exception>
96+
[SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Does not exist on .NET Standard 2.0")]
9697
public void Format(LogEvent logEvent, TextWriter output)
9798
{
9899
if (logEvent == null)
@@ -113,7 +114,9 @@ public void Format(LogEvent logEvent, TextWriter output)
113114
// The resulting XML is impossible to write with a standard compliant XML writer such as XmlWriter.
114115
// That's why we write the event in a StringWriter then massage the output to remove the xmlns:log4j attribute to match log4j output.
115116
// The XML fragment becomes valid when surrounded by an external entity, see https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L31-L49
116-
const string log4JNamespaceAttribute = @" xmlns:log4j=""http://jakarta.apache.org/log4j/""";
117+
const string log4JNamespaceAttribute = """
118+
xmlns:log4j="http://jakarta.apache.org/log4j/"
119+
""";
117120
var xmlString = ((StringWriter)xmlWriterOutput).ToString();
118121
var i = xmlString.IndexOf(log4JNamespaceAttribute, StringComparison.Ordinal);
119122
#if NETSTANDARD2_0
@@ -145,7 +148,7 @@ private void WriteEvent(LogEvent logEvent, XmlWriter writer)
145148
WriteDomainAndUserName(logEvent, writer);
146149
var properties = logEvent.Properties.Where(e => !SpecialProperties.Contains(e.Key, StringComparer.Ordinal)).ToList();
147150
var hasMachineNameProperty = logEvent.Properties.TryGetValue(MachineNamePropertyName, out var machineNameProperty) && machineNameProperty is ScalarValue { Value: not null };
148-
if (properties.Any() || hasMachineNameProperty)
151+
if (properties.Count > 0 || hasMachineNameProperty)
149152
{
150153
WriteProperties(logEvent, writer, properties, machineNameProperty);
151154
}
@@ -165,7 +168,12 @@ private static void WriteDomainAndUserName(LogEvent logEvent, XmlWriter writer)
165168
if (logEvent.Properties.TryGetValue(UserNamePropertyName, out var propertyValue) && propertyValue is ScalarValue { Value: string userNameProperty })
166169
{
167170
// See https://github.com/serilog/serilog-enrichers-environment/blob/3fc7cf78c5f34816633000ae74d846033498e44b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs#L53
168-
var separatorIndex = userNameProperty.IndexOf(@"\", StringComparison.OrdinalIgnoreCase);
171+
#if NETSTANDARD
172+
const string backslash = @"\";
173+
#else
174+
const char backslash = '\\';
175+
#endif
176+
var separatorIndex = userNameProperty.IndexOf(backslash, StringComparison.OrdinalIgnoreCase);
169177
if (separatorIndex != -1)
170178
{
171179
writer.WriteAttributeString("domain", userNameProperty.Substring(0, separatorIndex));

src/Log4NetTextFormatterOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Serilog.Formatting.Log4Net;
66
/// <summary>
77
/// Options for configuring the XML format produced by <see cref="Log4NetTextFormatter"/>.
88
/// </summary>
9-
internal class Log4NetTextFormatterOptions
9+
internal sealed class Log4NetTextFormatterOptions
1010
{
1111
internal Log4NetTextFormatterOptions(IFormatProvider? formatProvider, CDataMode cDataMode, XmlQualifiedName? xmlNamespace, XmlWriterSettings xmlWriterSettings, PropertyFilter filterProperty, ExceptionFormatter formatException)
1212
{

src/Serilog.Formatting.Log4Net.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup Label="Compiling">
8-
<LangVersion>10.0</LangVersion>
8+
<LangVersion>12</LangVersion>
99
<Nullable>enable</Nullable>
1010
<AnalysisMode>All</AnalysisMode>
1111
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -57,15 +57,14 @@
5757
</ItemGroup>
5858

5959
<ItemGroup>
60-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
61-
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="all" />
60+
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="all" />
6261
<PackageReference Include="Serilog" Version="2.0.0" />
6362
</ItemGroup>
6463

6564
<Target Name="ValidateNuGetPackage" AfterTargets="Pack">
6665
<!-- For reference: the file `.config/dotnet-tools.json` was created by running `dotnet new tool-manifest && dotnet tool install dotnet-validate` -->
6766
<Exec Command="dotnet tool restore" />
68-
<Exec Command="dotnet validate package local $([MSBuild]::EnsureTrailingSlash($(PackageOutputPath)))$(PackageId).$(PackageVersion).nupkg" />
67+
<Exec Command="dotnet validate package local $([MSBuild]::EnsureTrailingSlash($(PackageOutputPath)))$(PackageId).$(PackageVersion).nupkg" EnvironmentVariables="DOTNET_ROLL_FORWARD=LatestMajor" />
6968
</Target>
7069

7170
</Project>

0 commit comments

Comments
 (0)