Skip to content

Commit f06a0bc

Browse files
committed
fix regression from 3.0.0 (log4j:data instead of log4j:throwable)
fixes #225
1 parent 1a6ff0f commit f06a0bc

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#region Apache License
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one or more
4+
// contributor license agreements. See the NOTICE file distributed with
5+
// this work for additional information regarding copyright ownership.
6+
// The ASF licenses this file to you under the Apache License, Version 2.0
7+
// (the "License"); you may not use this file except in compliance with
8+
// the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#endregion
19+
20+
using System;
21+
22+
using log4net.Config;
23+
using log4net.Layout;
24+
using log4net.Repository;
25+
using log4net.Tests.Appender;
26+
27+
using NUnit.Framework;
28+
29+
namespace log4net.Tests.Layout
30+
{
31+
/// <summary>
32+
/// Tests for <see cref="XmlLayoutSchemaLog4J"/>
33+
/// </summary>
34+
[TestFixture]
35+
public class XmlLayoutSchemaLog4JTest
36+
{
37+
/// <summary>
38+
/// Tests a regression from 3.0.0 (log4j:data instead of log4j:throwable)
39+
/// </summary>
40+
[Test]
41+
public void LogExceptionTest()
42+
{
43+
StringAppender stringAppender = new() { Layout = new XmlLayoutSchemaLog4J() };
44+
45+
ILoggerRepository repository = LogManager.CreateRepository(Guid.NewGuid().ToString());
46+
BasicConfigurator.Configure(repository, stringAppender);
47+
ILog log = LogManager.GetLogger(repository.Name, "TestLogger");
48+
49+
ThrowAndLog(42);
50+
51+
string logEventXml = stringAppender.GetString();
52+
Assert.That(logEventXml, Does.Contain("log4j:throwable"));
53+
54+
void ThrowAndLog(int foo)
55+
{
56+
try
57+
{
58+
throw new TimeoutException();
59+
}
60+
catch (TimeoutException ex)
61+
{
62+
log.Error($"Error {foo}", ex);
63+
}
64+
}
65+
}
66+
}
67+
}

src/log4net/Layout/XmlLayoutSchemaLog4j.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
221221
if (!string.IsNullOrEmpty(exceptionStr))
222222
{
223223
// Append the stack trace line
224-
writer.WriteStartElement("log4j:throwable", "log4j", "data", "log4net");
224+
writer.WriteStartElement("log4j:throwable", "log4j", "throwable", "log4net");
225225
Transform.WriteEscapedXmlString(writer, exceptionStr!, InvalidCharReplacement);
226226
writer.WriteEndElement();
227227
}

0 commit comments

Comments
 (0)