Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlWriterEventSink.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
7
8namespace Waher.Events.Files
9{
13 public class XmlWriterEventSink : EventSink, IDisposable
14 {
18 protected XmlWriter output;
19
20 private readonly MultiReadSingleWriteObject semaphore;
21 private bool disposed = false;
22
28 public XmlWriterEventSink(string ObjectID, XmlWriter Output)
29 : base(ObjectID)
30 {
31 this.semaphore = new MultiReadSingleWriteObject(this);
32 this.output = Output;
33 }
34
38 protected virtual Task BeforeWrite()
39 {
40 return Task.CompletedTask; // Do nothing by default.
41 }
42
46 protected virtual Task AfterWrite()
47 {
48 return Task.CompletedTask; // Do nothing by default.
49 }
50
54 public override void Dispose()
55 {
56 this.disposed = true;
57
58 this.DisposeOutput();
59 base.Dispose();
60 }
61
66 public override async Task Queue(Event Event)
67 {
68 if (this.disposed)
69 return;
70
71 await this.semaphore.BeginWrite();
72 try
73 {
74 await this.BeforeWrite();
75 try
76 {
77 if (!(this.output is null))
78 {
79 this.output.WriteStartElement(Event.Type.ToString());
80 this.output.WriteAttributeString("timestamp", Encode(Event.Timestamp));
81 this.output.WriteAttributeString("level", Event.Level.ToString());
82
83
84 if (!string.IsNullOrEmpty(Event.EventId))
85 this.output.WriteAttributeString("id", Event.EventId);
86
87 if (!string.IsNullOrEmpty(Event.Object))
88 this.output.WriteAttributeString("object", Event.Object);
89
90 if (!string.IsNullOrEmpty(Event.Actor))
91 this.output.WriteAttributeString("actor", Event.Actor);
92
93 if (!string.IsNullOrEmpty(Event.Module))
94 this.output.WriteAttributeString("module", Event.Module);
95
96 if (!string.IsNullOrEmpty(Event.Facility))
97 this.output.WriteAttributeString("facility", Event.Facility);
98
99 this.output.WriteStartElement("Message");
100
101 foreach (string Row in EventExtensions.GetRows(Event.Message))
102 this.output.WriteElementString("Row", Row);
103
104 this.output.WriteEndElement();
105
106 if (!(Event.Tags is null) && Event.Tags.Length > 0)
107 {
108 foreach (KeyValuePair<string, object> Tag in Event.Tags)
109 {
110 this.output.WriteStartElement("Tag");
111 this.output.WriteAttributeString("key", Tag.Key);
112
113 if (!(Tag.Value is null))
114 this.output.WriteAttributeString("value", Tag.Value.ToString());
115
116 this.output.WriteEndElement();
117 }
118 }
119
120 if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
121 {
122 this.output.WriteStartElement("StackTrace");
123
124 foreach (string Row in EventExtensions.GetRows(Event.StackTrace))
125 this.output.WriteElementString("Row", Row);
126
127 this.output.WriteEndElement();
128 }
129
130 this.output.WriteEndElement();
131 this.output.Flush();
132 }
133 }
134 catch (Exception)
135 {
136 try
137 {
138 this.DisposeOutput();
139 }
140 catch (Exception)
141 {
142 // Ignore
143 }
144 }
145 finally
146 {
147 await this.AfterWrite();
148 }
149 }
150 finally
151 {
152 await this.semaphore.EndWrite();
153 }
154 }
155
156 internal static string Encode(DateTime DT)
157 {
158 StringBuilder sb = new StringBuilder();
159
160 sb.Append(DT.Year.ToString("D4"));
161 sb.Append('-');
162 sb.Append(DT.Month.ToString("D2"));
163 sb.Append('-');
164 sb.Append(DT.Day.ToString("D2"));
165 sb.Append('T');
166 sb.Append(DT.Hour.ToString("D2"));
167 sb.Append(':');
168 sb.Append(DT.Minute.ToString("D2"));
169 sb.Append(':');
170 sb.Append(DT.Second.ToString("D2"));
171 sb.Append('.');
172 sb.Append(DT.Millisecond.ToString("D3"));
173
174 if (DT.Kind == DateTimeKind.Utc)
175 sb.Append("Z");
176
177 return sb.ToString();
178 }
179
183 public virtual void DisposeOutput()
184 {
185 this.output?.Flush();
186 this.output?.Dispose();
187 this.output = null;
188 }
189
190 }
191}
Class representing an event.
Definition: Event.cs:10
string Message
Free-text event message.
Definition: Event.cs:131
EventType Type
Type of event.
Definition: Event.cs:121
string Object
Object related to the event.
Definition: Event.cs:136
EventLevel Level
Event Level.
Definition: Event.cs:126
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:141
string Module
Module where the event is reported.
Definition: Event.cs:156
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:116
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:166
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:146
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:151
string StackTrace
Stack Trace of event.
Definition: Event.cs:161
Base class for event sinks.
Definition: EventSink.cs:9
Outputs sniffed data to an XML writer.
virtual void DisposeOutput()
Disposes of the current output.
XmlWriter output
XML writer object.
override void Dispose()
IDisposable.Dispose
override async Task Queue(Event Event)
Queues an event to be output.
virtual Task AfterWrite()
Method is called after writing something to the text file.
virtual Task BeforeWrite()
Method is called before writing something to the text file.
XmlWriterEventSink(string ObjectID, XmlWriter Output)
Outputs sniffed data to an XML writer.
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:25
Represents an object that allows single concurrent writers but multiple concurrent readers....
EventType
Type of event.
Definition: EventType.cs:7