forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlWriterEventSink.cs
More file actions
166 lines (134 loc) · 3.95 KB
/
XmlWriterEventSink.cs
File metadata and controls
166 lines (134 loc) · 3.95 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Waher.Events.Files
{
/// <summary>
/// Outputs sniffed data to an XML writer.
/// </summary>
public class XmlWriterEventSink : EventSink, IDisposable
{
/// <summary>
/// XML writer object.
/// </summary>
protected XmlWriter output;
private readonly object synchObject = new object();
private bool disposed = false;
/// <summary>
/// Outputs sniffed data to an XML writer.
/// </summary>
/// <param name="ObjectID">Object ID</param>
/// <param name="Output">Output</param>
public XmlWriterEventSink(string ObjectID, XmlWriter Output)
: base(ObjectID)
{
this.output = Output;
}
/// <summary>
/// Method is called before writing something to the text file.
/// </summary>
protected virtual void BeforeWrite()
{
// Do nothing by default.
}
/// <summary>
/// Method is called after writing something to the text file.
/// </summary>
protected virtual void AfterWrite()
{
// Do nothing by default.
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public override void Dispose()
{
this.disposed = true;
base.Dispose();
}
/// <summary>
/// Queues an event to be output.
/// </summary>
/// <param name="Event">Event to queue.</param>
public override Task Queue(Event Event)
{
if (this.disposed)
return Task.CompletedTask;
lock (this.synchObject)
{
this.BeforeWrite();
try
{
this.output.WriteStartElement(Event.Type.ToString());
this.output.WriteAttributeString("timestamp", Encode(Event.Timestamp));
this.output.WriteAttributeString("level", Event.Level.ToString());
if (!string.IsNullOrEmpty(Event.EventId))
this.output.WriteAttributeString("id", Event.EventId);
if (!string.IsNullOrEmpty(Event.Object))
this.output.WriteAttributeString("object", Event.Object);
if (!string.IsNullOrEmpty(Event.Actor))
this.output.WriteAttributeString("actor", Event.Actor);
if (!string.IsNullOrEmpty(Event.Module))
this.output.WriteAttributeString("module", Event.Module);
if (!string.IsNullOrEmpty(Event.Facility))
this.output.WriteAttributeString("facility", Event.Facility);
this.output.WriteStartElement("Message");
foreach (string Row in GetRows(Event.Message))
this.output.WriteElementString("Row", Row);
this.output.WriteEndElement();
if (Event.Tags != null && Event.Tags.Length > 0)
{
foreach (KeyValuePair<string, object> Tag in Event.Tags)
{
this.output.WriteStartElement("Tag");
this.output.WriteAttributeString("key", Tag.Key);
if (Tag.Value != null)
this.output.WriteAttributeString("value", Tag.Value.ToString());
this.output.WriteEndElement();
}
}
if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
{
this.output.WriteStartElement("StackTrace");
foreach (string Row in GetRows(Event.StackTrace))
this.output.WriteElementString("Row", Row);
this.output.WriteEndElement();
}
this.output.WriteEndElement();
this.output.Flush();
}
finally
{
this.AfterWrite();
}
}
return Task.CompletedTask;
}
private static string[] GetRows(string s)
{
return s.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
}
internal static string Encode(DateTime DT)
{
StringBuilder sb = new StringBuilder();
sb.Append(DT.Year.ToString("D4"));
sb.Append('-');
sb.Append(DT.Month.ToString("D2"));
sb.Append('-');
sb.Append(DT.Day.ToString("D2"));
sb.Append('T');
sb.Append(DT.Hour.ToString("D2"));
sb.Append(':');
sb.Append(DT.Minute.ToString("D2"));
sb.Append(':');
sb.Append(DT.Second.ToString("D2"));
sb.Append('.');
sb.Append(DT.Millisecond.ToString("D3"));
if (DT.Kind == DateTimeKind.Utc)
sb.Append("Z");
return sb.ToString();
}
}
}