Skip to content

Commit

Permalink
Add support for .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Apr 24, 2024
1 parent 5381a2b commit aba43a1
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 64 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

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

## [Unreleased][Unreleased]

- Add support for .NET 8.

## [1.1.0][1.1.0] - 2023-05-02

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

- Implement log4j compatibility mode.

[Unreleased]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.1.0...HEAD
[1.1.0]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.2...1.1.0
[1.0.2]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/serilog-contrib/serilog-formatting-log4net/compare/1.0.0...1.0.1
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/global",
"sdk": {
"version": "6.0.400",
"version": "8.0.100",
"allowPrerelease": false,
"rollForward": "latestFeature"
}
Expand Down
20 changes: 14 additions & 6 deletions src/Log4NetTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public class Log4NetTextFormatter : ITextFormatter
/// <summary>
/// The characters that must be escaped inside an XML element. Used when <see cref="CDataMode"/> is <see cref="CDataMode.IfNeeded"/>.
/// </summary>
private static readonly char[] XmlElementCharactersToEscape = { '&', '<', '>' };
private static readonly char[] XmlElementCharactersToEscape = [ '&', '<', '>' ];

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

/// <summary>
/// The name of the thread id property, set by <a href="https://www.nuget.org/packages/Serilog.Enrichers.Thread/">Serilog.Enrichers.Thread</a>
Expand Down Expand Up @@ -93,6 +93,7 @@ public Log4NetTextFormatter(Action<Log4NetTextFormatterOptionsBuilder>? configur
/// <param name="logEvent">The event to format.</param>
/// <param name="output">The output.</param>
/// <exception cref="ArgumentNullException">If either <paramref name="logEvent"/> or <paramref name="output"/> is <c>null</c>.</exception>
[SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "Does not exist on .NET Standard 2.0")]
public void Format(LogEvent logEvent, TextWriter output)
{
if (logEvent == null)
Expand All @@ -113,7 +114,9 @@ public void Format(LogEvent logEvent, TextWriter output)
// The resulting XML is impossible to write with a standard compliant XML writer such as XmlWriter.
// That's why we write the event in a StringWriter then massage the output to remove the xmlns:log4j attribute to match log4j output.
// 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
const string log4JNamespaceAttribute = @" xmlns:log4j=""http://jakarta.apache.org/log4j/""";
const string log4JNamespaceAttribute = """
xmlns:log4j="http://jakarta.apache.org/log4j/"
""";
var xmlString = ((StringWriter)xmlWriterOutput).ToString();
var i = xmlString.IndexOf(log4JNamespaceAttribute, StringComparison.Ordinal);
#if NETSTANDARD2_0
Expand Down Expand Up @@ -145,7 +148,7 @@ private void WriteEvent(LogEvent logEvent, XmlWriter writer)
WriteDomainAndUserName(logEvent, writer);
var properties = logEvent.Properties.Where(e => !SpecialProperties.Contains(e.Key, StringComparer.Ordinal)).ToList();
var hasMachineNameProperty = logEvent.Properties.TryGetValue(MachineNamePropertyName, out var machineNameProperty) && machineNameProperty is ScalarValue { Value: not null };
if (properties.Any() || hasMachineNameProperty)
if (properties.Count > 0 || hasMachineNameProperty)
{
WriteProperties(logEvent, writer, properties, machineNameProperty);
}
Expand All @@ -165,7 +168,12 @@ private static void WriteDomainAndUserName(LogEvent logEvent, XmlWriter writer)
if (logEvent.Properties.TryGetValue(UserNamePropertyName, out var propertyValue) && propertyValue is ScalarValue { Value: string userNameProperty })
{
// See https://github.com/serilog/serilog-enrichers-environment/blob/3fc7cf78c5f34816633000ae74d846033498e44b/src/Serilog.Enrichers.Environment/Enrichers/EnvironmentUserNameEnricher.cs#L53
var separatorIndex = userNameProperty.IndexOf(@"\", StringComparison.OrdinalIgnoreCase);
#if NETSTANDARD
const string backslash = @"\";
#else
const char backslash = '\\';
#endif
var separatorIndex = userNameProperty.IndexOf(backslash, StringComparison.OrdinalIgnoreCase);
if (separatorIndex != -1)
{
writer.WriteAttributeString("domain", userNameProperty.Substring(0, separatorIndex));
Expand Down
2 changes: 1 addition & 1 deletion src/Log4NetTextFormatterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Serilog.Formatting.Log4Net;
/// <summary>
/// Options for configuring the XML format produced by <see cref="Log4NetTextFormatter"/>.
/// </summary>
internal class Log4NetTextFormatterOptions
internal sealed class Log4NetTextFormatterOptions
{
internal Log4NetTextFormatterOptions(IFormatProvider? formatProvider, CDataMode cDataMode, XmlQualifiedName? xmlNamespace, XmlWriterSettings xmlWriterSettings, PropertyFilter filterProperty, ExceptionFormatter formatException)
{
Expand Down
5 changes: 2 additions & 3 deletions src/Serilog.Formatting.Log4Net.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Label="Compiling">
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<AnalysisMode>All</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -57,7 +57,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="all" />
<PackageReference Include="Serilog" Version="2.0.0" />
</ItemGroup>
Expand Down
Loading

0 comments on commit aba43a1

Please sign in to comment.