-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Log4jTextFormatter.cs
151 lines (125 loc) · 4.44 KB
/
Log4jTextFormatter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2015-2024 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Serilog.Events;
using Serilog.Formatting;
using System.IO;
using System.Xml;
using Serilog.Sinks.Udp.Private;
namespace Serilog.Sinks.Udp.TextFormatters;
/// <summary>
/// Text formatter serializing log events into log4j compliant XML.
/// </summary>
public class Log4jTextFormatter : ITextFormatter
{
private static readonly string SourceContextPropertyName = "SourceContext";
private static readonly string ThreadIdPropertyName = "ThreadId";
private readonly XmlSerializer xmlSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="Log4jTextFormatter"/> class.
/// </summary>
public Log4jTextFormatter()
{
xmlSerializer = new XmlSerializer();
}
/// <summary>
/// Format the log event into the output.
/// </summary>
/// <param name="logEvent">The event to format.</param>
/// <param name="output">The output.</param>
public void Format(LogEvent logEvent, TextWriter output)
{
output.Write("<log4j:event xmlns:log4j=\"http://jakarta.apache.org/log4j/\"");
WriteLogger(logEvent, output);
WriteTimestamp(logEvent, output);
WriteLevel(logEvent, output);
WriteThread(logEvent, output);
output.Write(">");
WriteMessage(logEvent, output);
WriteException(logEvent, output);
output.Write("</log4j:event>");
}
private void WriteLogger(LogEvent logEvent, TextWriter output)
{
var value = GetScalarPropertyValue(logEvent, SourceContextPropertyName);
if (value != null)
{
output.Write($" logger=\"{xmlSerializer.SerializeXmlValue(value, true)}\"");
}
}
private static void WriteTimestamp(LogEvent logEvent, TextWriter output)
{
// Milliseconds since 1970-01-01
var milliseconds = logEvent.Timestamp.UtcDateTime.Ticks / 10000L - 62135596800000L;
output.Write($" timestamp=\"{XmlConvert.ToString(milliseconds)}\"");
}
private static void WriteLevel(LogEvent logEvent, TextWriter output)
{
string level;
switch (logEvent.Level)
{
case LogEventLevel.Fatal:
level = "FATAL";
break;
case LogEventLevel.Error:
level = "ERROR";
break;
case LogEventLevel.Warning:
level = "WARN";
break;
case LogEventLevel.Information:
level = "INFO";
break;
case LogEventLevel.Debug:
level = "DEBUG";
break;
default:
level = "TRACE";
break;
}
output.Write($" level=\"{level}\"");
}
private void WriteThread(LogEvent logEvent, TextWriter output)
{
var value = GetScalarPropertyValue(logEvent, ThreadIdPropertyName);
if (value != null)
{
output.Write($" thread=\"{xmlSerializer.SerializeXmlValue(value, true)}\"");
}
}
private void WriteMessage(LogEvent logEvent, TextWriter output)
{
output.Write("<log4j:message>");
xmlSerializer.SerializeXmlValue(output, logEvent.RenderMessage(), false);
output.Write("</log4j:message>");
}
private void WriteException(LogEvent logEvent, TextWriter output)
{
if (logEvent.Exception == null)
{
return;
}
output.Write("<log4j:throwable>");
xmlSerializer.SerializeXmlValue(output, logEvent.Exception.ToString(), false);
output.Write("</log4j:throwable>");
}
private static string? GetScalarPropertyValue(LogEvent logEvent, string propertyName)
{
if (!logEvent.Properties.TryGetValue(propertyName, out var property))
{
return null;
}
var value = ((ScalarValue)property).Value;
return value?.ToString();
}
}