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');
154 if (Xml.DocumentElement is
null || Xml.DocumentElement.NamespaceURI !=
LogNamespace)
157 if (Xml.DocumentElement.LocalName ==
"EventOutput")
159 List<Event> List =
new List<Event>();
161 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
163 if (!(N is XmlElement E))
169 List.Add(ParsedElement);
172 Parsed = List.ToArray();
175 else if (
TryParse(Xml.DocumentElement, out
Event ParsedElement))
177 Parsed =
new Event[] { ParsedElement };
199 DateTime Timestamp =
XML.
Attribute(Xml,
"timestamp", DateTime.UtcNow);
206 string StackTrace =
null;
207 string Message =
null;
208 List<KeyValuePair<string, object>> Tags =
new List<KeyValuePair<string, object>>();
210 foreach (XmlNode N2
in Xml.ChildNodes)
212 if (!(N2 is XmlElement E2))
215 switch (E2.LocalName)
218 Message = ReadRows(E2);
222 StackTrace = ReadRows(E2);
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));
238 Tags.Add(
new KeyValuePair<string, object>(Key, Value));
249 Parsed =
new Event(Timestamp,
EventType, Message ??
string.Empty, Object, Actor,
250 EventId, Level, Facility, Module, StackTrace, Tags.ToArray());
255 private static string ReadRows(XmlElement E)
257 StringBuilder sb =
new StringBuilder();
260 foreach (XmlNode N
in E.ChildNodes)
262 if (!(N is XmlElement E2))
265 if (E2.LocalName ==
"Row")
272 sb.Append(E2.InnerText);
276 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.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
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.