in src/log4net/Layout/XmlLayoutSchemaLog4j.cs [128:243]
protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
{
// Translate logging events for log4j
// Translate hostname property
if (loggingEvent.EnsureNotNull().LookupProperty(LoggingEvent.HostNameProperty) is not null
&& loggingEvent.LookupProperty("log4jmachinename") is null)
{
loggingEvent.GetProperties()["log4jmachinename"] = loggingEvent.LookupProperty(LoggingEvent.HostNameProperty);
}
// translate appdomain name
if (loggingEvent.LookupProperty("log4japp") is null
&& loggingEvent.Domain?.Length > 0)
{
loggingEvent.GetProperties()["log4japp"] = loggingEvent.Domain;
}
// translate identity name
if (loggingEvent.Identity?.Length > 0 &&
loggingEvent.LookupProperty(LoggingEvent.IdentityProperty) is null)
{
loggingEvent.GetProperties()[LoggingEvent.IdentityProperty] = loggingEvent.Identity;
}
// translate user name
if (loggingEvent.UserName.Length > 0 &&
loggingEvent.LookupProperty(LoggingEvent.UserNameProperty) is null)
{
loggingEvent.GetProperties()[LoggingEvent.UserNameProperty] = loggingEvent.UserName;
}
// Write the start element
writer.EnsureNotNull().WriteStartElement("log4j:event", "log4j", "event", "log4net");
writer.WriteAttributeString("logger", loggingEvent.LoggerName);
// Calculate the timestamp as the number of milliseconds since january 1970
//
// We must convert the TimeStamp to UTC before performing any mathematical
// operations. This allows use to take into account discontinuities
// caused by daylight savings time transitions.
TimeSpan timeSince1970 = loggingEvent.TimeStampUtc - _sDate1970;
writer.WriteAttributeString("timestamp", XmlConvert.ToString((long)timeSince1970.TotalMilliseconds));
if (loggingEvent.Level is not null)
{
writer.WriteAttributeString("level", loggingEvent.Level.DisplayName);
}
writer.WriteAttributeString("thread", loggingEvent.ThreadName);
// Append the message text
if (loggingEvent.RenderedMessage is not null)
{
writer.WriteStartElement("log4j:message", "log4j", "message", "log4net");
Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage, InvalidCharReplacement);
writer.WriteEndElement();
}
if (loggingEvent.LookupProperty("NDC") is object ndcObj)
{
string? valueStr = loggingEvent.Repository?.RendererMap.FindAndRender(ndcObj);
if (!string.IsNullOrEmpty(valueStr))
{
// Append the NDC text
writer.WriteStartElement("log4j:NDC", "log4j", "NDC", "log4net");
Transform.WriteEscapedXmlString(writer, valueStr!, InvalidCharReplacement);
writer.WriteEndElement();
}
}
// Append the properties text
PropertiesDictionary properties = loggingEvent.GetProperties();
if (properties.Count > 0)
{
writer.WriteStartElement("log4j:properties", "log4j", "properties", "log4net");
foreach (KeyValuePair<string, object?> entry in properties)
{
writer.WriteStartElement("log4j:data", "log4j", "data", "log4net");
writer.WriteAttributeString("name", entry.Key);
// Use an ObjectRenderer to convert the object to a string
string? valueStr = loggingEvent.Repository?.RendererMap.FindAndRender(entry.Value);
if (!string.IsNullOrEmpty(valueStr))
{
writer.WriteAttributeString("value", valueStr);
}
writer.WriteEndElement();
}
writer.WriteEndElement();
}
string? exceptionStr = loggingEvent.GetExceptionString();
if (!string.IsNullOrEmpty(exceptionStr))
{
// Append the stack trace line
writer.WriteStartElement("log4j:throwable", "log4j", "throwable", "log4net");
Transform.WriteEscapedXmlString(writer, exceptionStr!, InvalidCharReplacement);
writer.WriteEndElement();
}
if (LocationInfo)
{
if (loggingEvent.LocationInformation is LocationInfo locationInfo)
{
writer.WriteStartElement("log4j:locationInfo", "log4j", "locationInfo", "log4net");
writer.WriteAttributeString("class", locationInfo.ClassName);
writer.WriteAttributeString("method", locationInfo.MethodName);
writer.WriteAttributeString("file", locationInfo.FileName);
writer.WriteAttributeString("line", locationInfo.LineNumber);
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}