1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics . CodeAnalysis ;
4
+ using System . Globalization ;
4
5
using System . IO ;
5
6
using System . Linq ;
6
7
using System . Xml ;
@@ -89,9 +90,11 @@ public void Format(LogEvent logEvent, TextWriter output)
89
90
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L218-L310</remarks>
90
91
private void WriteEvent ( LogEvent logEvent , XmlWriter writer )
91
92
{
93
+ var useLog4JCompatibility = _options . Log4NetXmlNamespace ? . Name == "log4j" ;
92
94
WriteStartElement ( writer , "event" ) ;
93
95
WriteEventAttribute ( logEvent , writer , "logger" , Constants . SourceContextPropertyName ) ;
94
- writer . WriteAttributeString ( "timestamp" , XmlConvert . ToString ( logEvent . Timestamp ) ) ;
96
+ var timestamp = useLog4JCompatibility ? logEvent . Timestamp . ToUnixTimeMilliseconds ( ) . ToString ( CultureInfo . InvariantCulture ) : XmlConvert . ToString ( logEvent . Timestamp ) ;
97
+ writer . WriteAttributeString ( "timestamp" , timestamp ) ;
95
98
writer . WriteAttributeString ( "level" , LogLevel ( logEvent . Level ) ) ;
96
99
WriteEventAttribute ( logEvent , writer , "thread" , ThreadIdPropertyName ) ;
97
100
WriteDomainAndUserName ( logEvent , writer ) ;
@@ -102,7 +105,7 @@ private void WriteEvent(LogEvent logEvent, XmlWriter writer)
102
105
WriteProperties ( logEvent , writer , properties , machineNameProperty ) ;
103
106
}
104
107
WriteMessage ( logEvent , writer ) ;
105
- WriteException ( logEvent , writer ) ;
108
+ WriteException ( logEvent , writer , useLog4JCompatibility ? "throwable" : "exception" ) ;
106
109
writer . WriteEndElement ( ) ;
107
110
}
108
111
@@ -146,16 +149,17 @@ private void WriteEventAttribute(LogEvent logEvent, XmlWriter writer, string att
146
149
}
147
150
148
151
/// <summary>
149
- /// Convert Serilog <see cref="LogEventLevel"/> into log4net equivalent level.
152
+ /// Convert Serilog <see cref="LogEventLevel"/> into log4net/log4j equivalent level.
150
153
/// </summary>
151
154
/// <param name="level">The serilog level.</param>
152
- /// <returns>The equivalent log4net level.</returns>
155
+ /// <returns>The equivalent log4net/log4j level.</returns>
153
156
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Core/Level.cs#L509-L603</remarks>
157
+ /// <remarks>https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/Level.java#L48-L92</remarks>
154
158
private static string LogLevel ( LogEventLevel level )
155
159
{
156
160
return level switch
157
161
{
158
- LogEventLevel . Verbose => "VERBOSE " ,
162
+ LogEventLevel . Verbose => "TRACE " ,
159
163
LogEventLevel . Debug => "DEBUG" ,
160
164
LogEventLevel . Information => "INFO" ,
161
165
LogEventLevel . Warning => "WARN" ,
@@ -331,9 +335,10 @@ private void WriteMessage(LogEvent logEvent, XmlWriter writer)
331
335
/// </summary>
332
336
/// <param name="logEvent">The log event.</param>
333
337
/// <param name="writer">The XML writer.</param>
338
+ /// <param name="elementName">The element name, should be either <c>exception</c> or <c>throwable</c>.</param>
334
339
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L288-L295</remarks>
335
340
[ SuppressMessage ( "Microsoft.Design" , "CA1031" , Justification = "Protecting from user-provided code which might throw anything" ) ]
336
- private void WriteException ( LogEvent logEvent , XmlWriter writer )
341
+ private void WriteException ( LogEvent logEvent , XmlWriter writer , string elementName )
337
342
{
338
343
var exception = logEvent . Exception ;
339
344
if ( exception == null )
@@ -352,7 +357,7 @@ private void WriteException(LogEvent logEvent, XmlWriter writer)
352
357
}
353
358
if ( formattedException != null )
354
359
{
355
- WriteContent ( writer , "exception" , formattedException ) ;
360
+ WriteContent ( writer , elementName , formattedException ) ;
356
361
}
357
362
}
358
363
0 commit comments