-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathXmlWriterEventSink.cs
More file actions
191 lines (160 loc) · 4.5 KB
/
XmlWriterEventSink.cs
File metadata and controls
191 lines (160 loc) · 4.5 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Waher.Runtime.Threading;
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 MultiReadSingleWriteObject semaphore;
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.semaphore = new MultiReadSingleWriteObject(this);
this.output = Output;
}
/// <summary>
/// Method is called before writing something to the text file.
/// </summary>
protected virtual Task BeforeWrite()
{
return Task.CompletedTask; // Do nothing by default.
}
/// <summary>
/// Method is called after writing something to the text file.
/// </summary>
protected virtual Task AfterWrite()
{
return Task.CompletedTask; // Do nothing by default.
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public override Task DisposeAsync()
{
this.disposed = true;
this.DisposeOutput();
return base.DisposeAsync();
}
/// <summary>
/// Queues an event to be output.
/// </summary>
/// <param name="Event">Event to queue.</param>
public override async Task Queue(Event Event)
{
if (this.disposed)
return;
await this.semaphore.BeginWrite();
try
{
await this.BeforeWrite();
try
{
if (!(this.output is null))
{
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 EventExtensions.GetRows(Event.Message))
this.output.WriteElementString("Row", Row);
this.output.WriteEndElement();
if (!(Event.Tags is 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 is 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 EventExtensions.GetRows(Event.StackTrace))
this.output.WriteElementString("Row", Row);
this.output.WriteEndElement();
}
this.output.WriteEndElement();
this.output.Flush();
}
}
catch (Exception)
{
try
{
this.DisposeOutput();
}
catch (Exception)
{
// Ignore
}
}
finally
{
await this.AfterWrite();
}
}
finally
{
await this.semaphore.EndWrite();
}
}
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();
}
/// <summary>
/// Disposes of the current output.
/// </summary>
public virtual void DisposeOutput()
{
this.output?.Flush();
this.output?.Dispose();
this.output = null;
}
}
}