Skip to content

Instantly share code, notes, and snippets.

@t-ashula
Last active September 25, 2015 14:36
Show Gist options
  • Save t-ashula/9043b3423bc596793624 to your computer and use it in GitHub Desktop.
Save t-ashula/9043b3423bc596793624 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
internal class Program
{
public static void Main(string[] args)
{
using (var eventLog = new EventLog("Lenovo-Customer Feedback"))
{
foreach (EventLogEntry eventLogEntry in eventLog.Entries)
{
try
{
var text = Decompress(eventLogEntry.Data);
if (!string.IsNullOrEmpty(text))
{
var dictionary = ToDictionary(text);
Console.WriteLine("{0}:{2}:{1}", eventLogEntry.TimeGenerated, text, dictionary["serverUrl"]);
}
}
catch (Exception)
{
Console.WriteLine("Exception occcurred while processing event log record with timestamp {0}. Record will be skipped.", eventLogEntry.TimeGenerated);
}
}
}
}
private static string Decompress(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
string result;
using (var memoryStream = new MemoryStream(data, false))
{
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress, false))
{
using (var streamReader = new StreamReader(gzipStream, Encoding.UTF8))
{
result = streamReader.ReadToEnd();
streamReader.Close();
}
gzipStream.Close();
}
}
return result;
}
private static Dictionary<string, string> ToDictionary(string xml)
{
if (string.IsNullOrEmpty(xml))
{
throw new ArgumentNullException("xml", "Value cannot be null or an empty string.");
}
var dictionary = new Dictionary<string, string>();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
if (xmlDocument.DocumentElement != null && xmlDocument.DocumentElement.ChildNodes != null)
{
foreach (XmlNode xmlNode in xmlDocument.DocumentElement.ChildNodes)
{
if (!string.IsNullOrEmpty(xmlNode.Name) && !string.IsNullOrEmpty(xmlNode.InnerText))
{
dictionary.Add(xmlNode.Name, xmlNode.InnerText);
}
}
}
return dictionary;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment