Skip to content

Commit 9661c5e

Browse files
committed
Use the [GeneratedRegex] attribute when available
1 parent 6abd8f4 commit 9661c5e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Serilog.Formatting.Log4Net;
1414
/// <summary>
1515
/// A text formatter that serialize log events into <a href="https://logging.apache.org/log4net/">log4net</a> or <a href="https://logging.apache.org/log4j/">log4j</a> compatible XML format.
1616
/// </summary>
17-
public class Log4NetTextFormatter : ITextFormatter
17+
// ReSharper disable once PartialTypeWithSinglePart -- Used for [GeneratedRegex] on .NET 8 onwards
18+
public partial class Log4NetTextFormatter : ITextFormatter
1819
{
1920
/// <summary>
2021
/// The characters that must be escaped inside an XML element. Used when <see cref="CDataMode"/> is <see cref="CDataMode.IfNeeded"/>.
@@ -62,7 +63,15 @@ public class Log4NetTextFormatter : ITextFormatter
6263
/// <summary>
6364
/// The regular exception matching "class", "method", and optionally "file" and "line" for the caller property.
6465
/// </summary>
65-
private static readonly Regex CallerRegex = new(@"(?<class>.*)\.(?<method>.*\(.*\))(?: (?<file>.*):(?<line>\d+))?", RegexOptions.Compiled);
66+
private const string CallerRegularExpression = @"(?<class>.*)\.(?<method>.*\(.*\))(?: (?<file>.*):(?<line>\d+))?";
67+
68+
#if NET8_0_OR_GREATER
69+
[GeneratedRegex(CallerRegularExpression)]
70+
private static partial Regex CallerRegex();
71+
#else
72+
private static readonly Regex CallerRegexField = new(CallerRegularExpression, RegexOptions.Compiled);
73+
private static Regex CallerRegex() => CallerRegexField;
74+
#endif
6675

6776
private readonly Log4NetTextFormatterOptions _options;
6877
private readonly bool _usesLog4JCompatibility;
@@ -498,7 +507,7 @@ private void WriteLocationInfo(LogEvent logEvent, XmlWriter writer)
498507
{
499508
if (logEvent.Properties.TryGetValue(CallerPropertyName, out var callerProperty) && callerProperty is ScalarValue { Value: string caller })
500509
{
501-
var match = CallerRegex.Match(caller);
510+
var match = CallerRegex().Match(caller);
502511
if (match.Success)
503512
{
504513
WriteStartElement(writer, "locationInfo");

tests/Serilog.Formatting.Log4Net.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<CoverageReportDirectory>$([System.IO.Path]::Combine($(RootDirectory),'coverage',$(TargetFramework)))</CoverageReportDirectory>
3131
<VSTestResultsDirectory Condition="$(ContinuousIntegrationBuild) == 'true'">$(RootDirectory)</VSTestResultsDirectory>
3232
<VSTestResultsDirectory Condition="$(ContinuousIntegrationBuild) != 'true'">$([System.IO.Path]::Combine($(CoverageReportDirectory),'results'))</VSTestResultsDirectory>
33-
<VSTestCollect>XPlat Code Coverage</VSTestCollect>
33+
<VSTestCollect>XPlat Code Coverage%3BExcludeByAttribute=GeneratedCodeAttribute</VSTestCollect>
3434
<VSTestLogger>@(VSTestLogger)</VSTestLogger>
3535
</PropertyGroup>
3636

0 commit comments

Comments
 (0)