Last active
January 29, 2018 13:54
-
-
Save JamesSkemp/c00a18877130438f301984218b8ac442 to your computer and use it in GitHub Desktop.
Parse ELMAH XML files with LINQPad (C# Program)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
// Update with the path to Elmah.Errors/where the ELMAH logs are being stored. | |
var directoryPath = @"\\path\to\Elmah.Errors\"; | |
var directory = new DirectoryInfo(directoryPath); | |
var files = directory.GetFiles("*.xml").OrderByDescending(f => f.CreationTime); | |
files.Count().Dump(); | |
var messages = new List<string>(); | |
var elmahXmlData = new List<ElmahXml>(); | |
foreach (var file in files) | |
{ | |
try | |
{ | |
var xml = XDocument.Load(file.FullName).Root; | |
var elmahXml = new ElmahXml(xml); | |
//xml.Dump(); | |
//return; | |
if (elmahXml.Message.StartsWith("Token value:")) | |
{ | |
File.Delete(file.FullName); | |
continue; | |
} | |
messages.Add(elmahXml.Message); | |
elmahXmlData.Add(elmahXml); | |
} | |
catch (Exception ex) | |
{ | |
file.FullName.Dump(); | |
ex.Message.Dump(); | |
} | |
} | |
messages.GroupBy(m => m).Select(m => new { Message = m.Key, Number = m.Count() }).OrderByDescending(m => m.Number).Dump(); | |
elmahXmlData.Dump(); | |
} | |
// Define other methods and classes here | |
class ElmahXml | |
{ | |
private XElement xml { get; set; } | |
public string Message { get; set; } | |
public string Details { get; set; } | |
public string PageUrl { get; set; } | |
public string QueryString { get; set; } | |
public string RemoteAddress { get; set; } | |
public DateTime Time { get; set; } | |
public ElmahXml(XElement xml) | |
{ | |
this.xml = xml; | |
Message = xml | |
.Attribute("message").Value; | |
Details = xml | |
.Attribute("detail").Value; | |
PageUrl = getServerVariable("PATH_INFO"); | |
QueryString = getServerVariable("QUERY_STRING"); | |
RemoteAddress = getServerVariable("REMOTE_ADDR"); | |
Time = DateTime.Parse(xml.Attribute("time").Value); | |
} | |
private string getServerVariable(string attributeName) { | |
string value = null; | |
if (!string.IsNullOrWhiteSpace(attributeName) | |
&& xml != null | |
&& xml.Element("serverVariables") != null | |
&& xml.Element("serverVariables").Elements("item").Where(i => i.Attribute("name").Value == attributeName).Count() == 1) { | |
value = xml.Element("serverVariables").Elements("item").First(i => i.Attribute("name").Value == attributeName).Element("value").Attribute("string").Value; | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment