Skip to content

Commit 1be3800

Browse files
committed
Add new Log4JFormatter static property
1 parent bd766db commit 1be3800

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public class Log4NetTextFormatter : ITextFormatter
4949
private readonly Log4NetTextFormatterOptions _options;
5050
private readonly bool _usesLog4JCompatibility;
5151

52+
/// <summary>
53+
/// A <see cref="Log4NetTextFormatter"/> instance configured for the log4j XML layout.
54+
/// Accessible with <c>Serilog.Formatting.Log4Net.Log4NetTextFormatter::Log4JFormatter, Serilog.Formatting.Log4Net</c> when using the
55+
/// <a href="https://github.com/serilog/serilog-settings-configuration/">Serilog.Settings.Configuration</a> package.
56+
/// </summary>
57+
public static Log4NetTextFormatter Log4JFormatter { get; } = new(c => c.UseLog4JCompatibility());
58+
5259
/// <summary>
5360
/// Initialize a new instance of the <see cref="Log4NetTextFormatter"/> class.
5461
/// </summary>

tests/Log4NetTextFormatterTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,22 +306,24 @@ public Task DefaultFormatProvider()
306306
return Verify(output);
307307
}
308308

309-
[Fact]
310-
public Task Log4JCompatibility()
309+
[Theory]
310+
[InlineData(true)]
311+
[InlineData(false)]
312+
public Task Log4JCompatibility(bool useStaticInstance)
311313
{
312314
// Arrange
313315
using var output = new StringWriter();
314316
var logEvent = CreateLogEvent(
315317
exception: new Exception("An error occurred").SetStackTrace(@" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
316318
properties: new LogEventProperty("π", new ScalarValue(3.14m))
317319
);
318-
var formatter = new Log4NetTextFormatter(c => c.UseLog4JCompatibility());
320+
var formatter = useStaticInstance ? Log4NetTextFormatter.Log4JFormatter : new Log4NetTextFormatter(c => c.UseLog4JCompatibility());
319321

320322
// Act
321323
formatter.Format(logEvent, output);
322324

323325
// Assert
324-
return Verify(output);
326+
return Verify(output).DisableRequireUniquePrefix();
325327
}
326328

327329
[Fact]

tests/PublicApi.net6.0.verified.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Log4NetTextFormatter : Serilog.Formatting.ITextFormatter
3131
{
3232
public Log4NetTextFormatter() { }
3333
public Log4NetTextFormatter(System.Action<Serilog.Formatting.Log4Net.Log4NetTextFormatterOptionsBuilder>? configureOptions) { }
34+
public static Serilog.Formatting.Log4Net.Log4NetTextFormatter Log4JFormatter { get; }
3435
public void Format(Serilog.Events.LogEvent logEvent, System.IO.TextWriter output) { }
3536
}
3637
public class Log4NetTextFormatterOptionsBuilder

tests/PublicApi.netstandard2.0.verified.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Log4NetTextFormatter : Serilog.Formatting.ITextFormatter
3131
{
3232
public Log4NetTextFormatter() { }
3333
public Log4NetTextFormatter(System.Action<Serilog.Formatting.Log4Net.Log4NetTextFormatterOptionsBuilder>? configureOptions) { }
34+
public static Serilog.Formatting.Log4Net.Log4NetTextFormatter Log4JFormatter { get; }
3435
public void Format(Serilog.Events.LogEvent logEvent, System.IO.TextWriter output) { }
3536
}
3637
public class Log4NetTextFormatterOptionsBuilder

0 commit comments

Comments
 (0)