Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EventExtensions.cs
1using System;
3using System.Text;
4using System.Xml;
5using Waher.Content;
7
9{
13 public static class EventExtensions
14 {
18 public const string LogNamespace = "http://waher.se/Schema/EventOutput.xsd";
19
25 public static string ToXML(this Event Event)
26 {
27 StringBuilder sb = new StringBuilder();
28 Event.ToXML(sb);
29 return sb.ToString();
30 }
31
37 public static void ToXML(this Event Event, StringBuilder Xml)
38 {
39 string ElementName = Event.Type.ToString();
40
41 Xml.Append('<');
42 Xml.Append(ElementName);
43 Xml.Append(" xmlns='");
44 Xml.Append(LogNamespace);
45 Xml.Append("' timestamp='");
46 Xml.Append(XML.Encode(Event.Timestamp));
47 Xml.Append("' level='");
48 Xml.Append(Event.Level.ToString());
49
50 if (!string.IsNullOrEmpty(Event.EventId))
51 {
52 Xml.Append("' id='");
53 Xml.Append(Event.EventId);
54 }
55
56 if (!string.IsNullOrEmpty(Event.Object))
57 {
58 Xml.Append("' object='");
59 Xml.Append(Event.Object);
60 }
61
62 if (!string.IsNullOrEmpty(Event.Actor))
63 {
64 Xml.Append("' actor='");
65 Xml.Append(Event.Actor);
66 }
67
68 if (!string.IsNullOrEmpty(Event.Module))
69 {
70 Xml.Append("' module='");
71 Xml.Append(Event.Module);
72 }
73
74 if (!string.IsNullOrEmpty(Event.Facility))
75 {
76 Xml.Append("' facility='");
77 Xml.Append(Event.Facility);
78 }
79
80 Xml.Append("'><Message>");
81
82 foreach (string Row in GetRows(Event.Message))
83 {
84 Xml.Append("<Row>");
85 Xml.Append(XML.Encode(Row));
86 Xml.Append("</Row>");
87 }
88
89 Xml.Append("</Message>");
90
91 if (!(Event.Tags is null) && Event.Tags.Length > 0)
92 {
93 foreach (KeyValuePair<string, object> Tag in Event.Tags)
94 {
95 Xml.Append("<Tag key='");
96 Xml.Append(XML.Encode(Tag.Key));
97
98 if (!(Tag.Value is null))
99 {
100 Xml.Append("' value='");
101 Xml.Append(XML.Encode(Tag.Value.ToString()));
102 }
103
104 Xml.Append("'/>");
105 }
106 }
107
108 if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
109 {
110 Xml.Append("<StackTrace>");
111
112 foreach (string Row in GetRows(Event.StackTrace))
113 {
114 Xml.Append("<Row>");
115 Xml.Append(XML.Encode(Row));
116 Xml.Append("</Row>");
117 }
118
119 Xml.Append("</StackTrace>");
120 }
121
122 Xml.Append("</");
123 Xml.Append(ElementName);
124 Xml.Append('>');
125 }
126
127 internal static string[] GetRows(string s)
128 {
129 return s.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
130 }
131
138 public static bool TryParse(string Xml, out Event[] Parsed)
139 {
140 XmlDocument Doc = XML.ParseXml(Xml);
141 return TryParse(Doc, out Parsed);
142 }
143
150 public static bool TryParse(XmlDocument Xml, out Event[] Parsed)
151 {
152 Parsed = null;
153
154 if (Xml.DocumentElement is null || Xml.DocumentElement.NamespaceURI != LogNamespace)
155 return false;
156
157 if (Xml.DocumentElement.LocalName == "EventOutput")
158 {
159 List<Event> List = new List<Event>();
160
161 foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
162 {
163 if (!(N is XmlElement E))
164 continue;
165
166 if (!TryParse(E, out Event ParsedElement))
167 return false;
168
169 List.Add(ParsedElement);
170 }
171
172 Parsed = List.ToArray();
173 return true;
174 }
175 else if (TryParse(Xml.DocumentElement, out Event ParsedElement))
176 {
177 Parsed = new Event[] { ParsedElement };
178 return true;
179 }
180 else
181 return false;
182 }
183
184
191 public static bool TryParse(XmlElement Xml, out Event Parsed)
192 {
193 if (Xml is null || Xml.NamespaceURI != LogNamespace)
194 {
195 Parsed = null;
196 return false;
197 }
198
199 DateTime Timestamp = XML.Attribute(Xml, "timestamp", DateTime.UtcNow);
200 EventLevel Level = XML.Attribute(Xml, "level", EventLevel.Minor);
201 string EventId = XML.Attribute(Xml, "id");
202 string Object = XML.Attribute(Xml, "object");
203 string Actor = XML.Attribute(Xml, "actor");
204 string Module = XML.Attribute(Xml, "module");
205 string Facility = XML.Attribute(Xml, "facility");
206 string StackTrace = null;
207 string Message = null;
208 List<KeyValuePair<string, object>> Tags = new List<KeyValuePair<string, object>>();
209
210 foreach (XmlNode N2 in Xml.ChildNodes)
211 {
212 if (!(N2 is XmlElement E2))
213 continue;
214
215 switch (E2.LocalName)
216 {
217 case "Message":
218 Message = ReadRows(E2);
219 break;
220
221 case "StackTrace":
222 StackTrace = ReadRows(E2);
223 break;
224
225 case "Tag":
226 string Key = XML.Attribute(E2, "key");
227 string Value = XML.Attribute(E2, "value");
228
229 if (bool.TryParse(Value, out bool b))
230 Tags.Add(new KeyValuePair<string, object>(Key, b));
231 else if (int.TryParse(Value, out int i))
232 Tags.Add(new KeyValuePair<string, object>(Key, i));
233 else if (long.TryParse(Value, out long l))
234 Tags.Add(new KeyValuePair<string, object>(Key, l));
235 else if (double.TryParse(Value, out double d))
236 Tags.Add(new KeyValuePair<string, object>(Key, d));
237 else
238 Tags.Add(new KeyValuePair<string, object>(Key, Value));
239 break;
240 }
241 }
242
243 if (!Enum.TryParse(Xml.LocalName, out EventType EventType))
244 {
245 Parsed = null;
246 return false;
247 }
248
249 Parsed = new Event(Timestamp, EventType, Message ?? string.Empty, Object, Actor,
250 EventId, Level, Facility, Module, StackTrace, Tags.ToArray());
251
252 return true;
253 }
254
255 private static string ReadRows(XmlElement E)
256 {
257 StringBuilder sb = new StringBuilder();
258 bool First = true;
259
260 foreach (XmlNode N in E.ChildNodes)
261 {
262 if (!(N is XmlElement E2))
263 continue;
264
265 if (E2.LocalName == "Row")
266 {
267 if (First)
268 First = false;
269 else
270 sb.AppendLine();
271
272 sb.Append(E2.InnerText);
273 }
274 }
275
276 return sb.ToString();
277 }
278 }
279}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Class representing an event.
Definition: Event.cs:11
string Message
Free-text event message.
Definition: Event.cs:132
EventType Type
Type of event.
Definition: Event.cs:122
string Object
Object related to the event.
Definition: Event.cs:137
EventLevel Level
Event Level.
Definition: Event.cs:127
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:142
string Module
Module where the event is reported.
Definition: Event.cs:157
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:117
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:167
override string ToString()
Definition: Event.cs:170
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:147
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:152
string StackTrace
Stack Trace of event.
Definition: Event.cs:162
static void ToXML(this Event Event, StringBuilder Xml)
Exports the event object to XML.
static bool TryParse(XmlElement Xml, out Event Parsed)
Tries to parse an event object from an XML element.
static bool TryParse(XmlDocument Xml, out Event[] Parsed)
Tries to parse an event object (or collection of event objects) from an XML document.
const string LogNamespace
http://waher.se/Schema/EventOutput.xsd
static string ToXML(this Event Event)
Exports the event object to XML.
static bool TryParse(string Xml, out Event[] Parsed)
Tries to parse an event object (or collection of event objects) from an XML document.
EventLevel
Event level.
Definition: EventLevel.cs:7
EventType
Type of event.
Definition: EventType.cs:7