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;
2using System.Collections.Generic;
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 = new XmlDocument();
141 Doc.LoadXml(Xml);
142 return TryParse(Xml, out Parsed);
143 }
144
151 public static bool TryParse(XmlDocument Xml, out Event[] Parsed)
152 {
153 Parsed = null;
154
155 if (Xml.DocumentElement is null || Xml.DocumentElement.NamespaceURI != LogNamespace)
156 return false;
157
158 if (Xml.DocumentElement.LocalName == "EventOutput")
159 {
160 List<Event> List = new List<Event>();
161
162 foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
163 {
164 if (!(N is XmlElement E))
165 continue;
166
167 if (!TryParse(E, out Event ParsedElement))
168 return false;
169
170 List.Add(ParsedElement);
171 }
172
173 Parsed = List.ToArray();
174 return true;
175 }
176 else if (TryParse(Xml.DocumentElement, out Event ParsedElement))
177 {
178 Parsed = new Event[] { ParsedElement };
179 return true;
180 }
181 else
182 return false;
183 }
184
185
192 public static bool TryParse(XmlElement Xml, out Event Parsed)
193 {
194 if (Xml is null || Xml.NamespaceURI != LogNamespace)
195 {
196 Parsed = null;
197 return false;
198 }
199
200 DateTime Timestamp = XML.Attribute(Xml, "timestamp", DateTime.Now);
201 EventLevel Level = XML.Attribute(Xml, "level", EventLevel.Minor);
202 string EventId = XML.Attribute(Xml, "id");
203 string Object = XML.Attribute(Xml, "object");
204 string Actor = XML.Attribute(Xml, "actor");
205 string Module = XML.Attribute(Xml, "module");
206 string Facility = XML.Attribute(Xml, "facility");
207 string StackTrace = null;
208 string Message = null;
209 List<KeyValuePair<string, object>> Tags = new List<KeyValuePair<string, object>>();
210
211 foreach (XmlNode N2 in Xml.ChildNodes)
212 {
213 if (!(N2 is XmlElement E2))
214 continue;
215
216 switch (E2.LocalName)
217 {
218 case "Message":
219 Message = ReadRows(E2);
220 break;
221
222 case "StackTrace":
223 StackTrace = ReadRows(E2);
224 break;
225
226 case "Tag":
227 string Key = XML.Attribute(E2, "key");
228 string Value = XML.Attribute(E2, "value");
229
230 if (bool.TryParse(Value, out bool b))
231 Tags.Add(new KeyValuePair<string, object>(Key, b));
232 else if (int.TryParse(Value, out int i))
233 Tags.Add(new KeyValuePair<string, object>(Key, i));
234 else if (long.TryParse(Value, out long l))
235 Tags.Add(new KeyValuePair<string, object>(Key, l));
236 else if (double.TryParse(Value, out double d))
237 Tags.Add(new KeyValuePair<string, object>(Key, d));
238 else
239 Tags.Add(new KeyValuePair<string, object>(Key, Value));
240 break;
241 }
242 }
243
244 if (!Enum.TryParse(Xml.LocalName, out EventType EventType))
245 {
246 Parsed = null;
247 return false;
248 }
249
250 Parsed = new Event(Timestamp, EventType, Message ?? string.Empty, Object, Actor,
251 EventId, Level, Facility, Module, StackTrace, Tags.ToArray());
252
253 return true;
254 }
255
256 private static string ReadRows(XmlElement E)
257 {
258 StringBuilder sb = new StringBuilder();
259 bool First = true;
260
261 foreach (XmlNode N in E.ChildNodes)
262 {
263 if (!(N is XmlElement E2))
264 continue;
265
266 if (E2.LocalName == "Row")
267 {
268 if (First)
269 First = false;
270 else
271 sb.AppendLine();
272
273 sb.Append(E2.InnerText);
274 }
275 }
276
277 return sb.ToString();
278 }
279 }
280}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
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
override string ToString()
Definition: Event.cs:169
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
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