2using System.Collections.Generic;
18 public const string LogNamespace =
"http://waher.se/Schema/EventOutput.xsd";
27 StringBuilder sb =
new StringBuilder();
39 string ElementName =
Event.
Type.ToString();
42 Xml.Append(ElementName);
43 Xml.Append(
" xmlns='");
45 Xml.Append(
"' timestamp='");
47 Xml.Append(
"' level='");
58 Xml.Append(
"' object='");
64 Xml.Append(
"' actor='");
70 Xml.Append(
"' module='");
76 Xml.Append(
"' facility='");
80 Xml.Append(
"'><Message>");
89 Xml.Append(
"</Message>");
93 foreach (KeyValuePair<string, object> Tag
in Event.
Tags)
95 Xml.Append(
"<Tag key='");
98 if (!(Tag.Value is
null))
100 Xml.Append(
"' value='");
101 Xml.Append(
XML.
Encode(Tag.Value.ToString()));
110 Xml.Append(
"<StackTrace>");
116 Xml.Append(
"</Row>");
119 Xml.Append(
"</StackTrace>");
123 Xml.Append(ElementName);
127 internal static string[] GetRows(
string s)
129 return s.Replace(
"\r\n",
"\n").Replace(
"\r",
"\n").Split(
'\n');
140 XmlDocument Doc =
new XmlDocument();
155 if (Xml.DocumentElement is
null || Xml.DocumentElement.NamespaceURI !=
LogNamespace)
158 if (Xml.DocumentElement.LocalName ==
"EventOutput")
160 List<Event> List =
new List<Event>();
162 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
164 if (!(N is XmlElement E))
170 List.Add(ParsedElement);
173 Parsed = List.ToArray();
176 else if (
TryParse(Xml.DocumentElement, out
Event ParsedElement))
178 Parsed =
new Event[] { ParsedElement };
200 DateTime Timestamp =
XML.
Attribute(Xml,
"timestamp", DateTime.Now);
207 string StackTrace =
null;
208 string Message =
null;
209 List<KeyValuePair<string, object>> Tags =
new List<KeyValuePair<string, object>>();
211 foreach (XmlNode N2
in Xml.ChildNodes)
213 if (!(N2 is XmlElement E2))
216 switch (E2.LocalName)
219 Message = ReadRows(E2);
223 StackTrace = ReadRows(E2);
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));
239 Tags.Add(
new KeyValuePair<string, object>(Key, Value));
250 Parsed =
new Event(Timestamp,
EventType, Message ??
string.Empty, Object, Actor,
251 EventId, Level, Facility, Module, StackTrace, Tags.ToArray());
256 private static string ReadRows(XmlElement E)
258 StringBuilder sb =
new StringBuilder();
261 foreach (XmlNode N
in E.ChildNodes)
263 if (!(N is XmlElement E2))
266 if (E2.LocalName ==
"Row")
273 sb.Append(E2.InnerText);
277 return sb.ToString();
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
static string Encode(string s)
Encodes a string for use in XML.
Class representing an event.
string Message
Free-text event message.
EventType Type
Type of event.
string Object
Object related to the event.
EventLevel Level
Event Level.
string Actor
Actor responsible for the action causing the event.
string Module
Module where the event is reported.
DateTime Timestamp
Timestamp of event.
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
override string ToString()
string EventId
Computer-readable Event ID identifying type of even.
string Facility
Facility can be either a facility in the network sense or in the system sense.
string StackTrace
Stack Trace of event.
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.