Skip to content

Commit

Permalink
Decode content in Windows Event Log properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster620 committed Oct 20, 2024
1 parent 9535acf commit 230238e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
5 changes: 2 additions & 3 deletions ULogViewer/Logs/DataSources/WindowsEventLogDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -64,10 +63,10 @@ public ReaderImpl(WindowsEventLogDataSource source, EventLog eventLog)
it.Add($"<Timestamp>{timestamp:yyyy/MM/dd HH:mm:ss}</Timestamp>");
it.Add($"<EventId>{eventId}</EventId>");
it.Add($"<Level>{level}</Level>");
it.Add($"<Source>{WebUtility.HtmlEncode(sourceName)}</Source>");
it.Add($"<Source>{sourceName}</Source>");
it.Add("<Message>");
foreach (var messageLine in message.Split('\n'))
it.Add($"{WebUtility.HtmlEncode(messageLine.TrimEnd())}");
it.Add($"{messageLine.TrimEnd()}");
});
return "</Message>";
}
Expand Down
26 changes: 13 additions & 13 deletions ULogViewer/Logs/DataSources/WindowsEventLogFileDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static TextReader ReadMessageFromEventData(XmlElement eventDataNode)
if (dataNode.NodeType == XmlNodeType.Element && dataNode.Name == "Data")
{
if (dataNode.Attributes?.Count == 0 && dataNode.FirstChild is XmlText dataText)
return new StringReader(dataText.Value ?? "");
return new StringReader(WebUtility.HtmlDecode(dataText.Value) ?? "");
var dataLines = new StringBuilder();
do
{
Expand All @@ -75,19 +75,19 @@ static TextReader ReadMessageFromEventData(XmlElement eventDataNode)
{
dataLines.Append(nameAttr.Value);
dataLines.Append(": ");
dataLines.Append(dataNode.InnerXml.Trim());
dataLines.Append(WebUtility.HtmlDecode(dataNode.InnerXml.Trim()));
}
else if (dataNode.FirstChild is not null)
{
dataLines.Append("Data: ");
dataLines.Append(dataNode.InnerXml.Trim());
dataLines.Append(WebUtility.HtmlDecode(dataNode.InnerXml.Trim()));
}
}
else if (dataNode.FirstChild is not null)
{
dataLines.Append(dataNode.Name);
dataLines.Append(": ");
dataLines.Append(dataNode.InnerXml.Trim());
dataLines.Append(WebUtility.HtmlDecode(dataNode.InnerXml.Trim()));
}
}
finally
Expand Down Expand Up @@ -133,10 +133,10 @@ static TextReader ReadMessageFromUserData(XmlElement userDataNode)
if (propertyValueNode?.NodeType == XmlNodeType.Element
&& propertyValueNode.ChildNodes.Count <= 1)
{
messageBuffer.Append(propertyValueNode.InnerXml.Trim());
messageBuffer.Append(WebUtility.HtmlDecode(propertyValueNode.InnerXml.Trim()));
}
else
messageBuffer.Append(propertyNode.InnerXml.Trim());
messageBuffer.Append(WebUtility.HtmlDecode(propertyNode.InnerXml.Trim()));
}
propertyNode = propertyNode.NextSibling;
}
Expand All @@ -145,8 +145,8 @@ static TextReader ReadMessageFromUserData(XmlElement userDataNode)
childNode = childNode.NextSibling;
}
if (messageBuffer.Length > 0)
return new StringReader(messageBuffer.ToString());
return new StringReader(userDataNode.InnerXml.Trim());
return new StringReader(WebUtility.HtmlDecode(messageBuffer.ToString()));
return new StringReader(WebUtility.HtmlDecode(userDataNode.InnerXml.Trim()));
}

/// <inheritdoc/>
Expand All @@ -173,20 +173,20 @@ static TextReader ReadMessageFromUserData(XmlElement userDataNode)

// generate lines for record
recordLines.Enqueue($"<Timestamp>{record.Timestamp.DateTime.ToLocalTime():yyyy/MM/dd HH:mm:ss}</Timestamp>");
recordLines.Enqueue($"<Computer>{WebUtility.HtmlEncode(record.Computer)}</Computer>");
recordLines.Enqueue($"<UserName>{WebUtility.HtmlEncode(record.UserName)}</UserName>");
recordLines.Enqueue($"<Category>{WebUtility.HtmlEncode(record.Channel)}</Category>");
recordLines.Enqueue($"<Computer>{record.Computer}</Computer>");
recordLines.Enqueue($"<UserName>{record.UserName}</UserName>");
recordLines.Enqueue($"<Category>{record.Channel}</Category>");
recordLines.Enqueue($"<ProcessId>{record.ProcessId}</ProcessId>");
recordLines.Enqueue($"<ThreadId>{record.ThreadId}</ThreadId>");
recordLines.Enqueue($"<EventId>{record.EventId}</EventId>");
recordLines.Enqueue($"<Level>{level}</Level>");
recordLines.Enqueue($"<SourceName>{WebUtility.HtmlEncode(record.Provider)}</SourceName>");
recordLines.Enqueue($"<SourceName>{record.Provider}</SourceName>");
recordLines.Enqueue("<Message>");
var messageReader = ReadMessage(record.Payload);
var messageLine = messageReader.ReadLine();
while (messageLine != null)
{
recordLines.Enqueue(WebUtility.HtmlEncode(messageLine).TrimEnd());
recordLines.Enqueue(messageLine.TrimEnd());
messageLine = messageReader.ReadLine();
}
recordLines.Enqueue("</Message>");
Expand Down

0 comments on commit 230238e

Please sign in to comment.