Created
October 4, 2015 00:42
-
-
Save JamesSkemp/d99903d398837a149261 to your computer and use it in GitHub Desktop.
Parse ELMAH logs via LINQPad.
This file contains 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
<Query Kind="Program" /> | |
void Main() | |
{ | |
// If false, will disable all debugging messages. | |
var debug = true; | |
// If true, files will not really be deleted. | |
var testingDeletions = false; | |
// If true, dump the file name of Elmah logs that are deleted. | |
var debugDumpDeletedFileName = true; | |
// If true, error messages will be dumped to the display, if the file isn't deleted. | |
var debugDumpErrorMessage = true; | |
var deleteFile = false; | |
var directoryPath = @"\path\to\App_Data\Elmah.Errors"; | |
var directory = new DirectoryInfo(directoryPath); | |
var directoryFiles = directory.GetFiles("*.xml"); | |
string.Format("Total errors: {0}", directoryFiles.Count()).Dump(); | |
var files = directoryFiles.OrderByDescending(f => f.CreationTime).Take(1000); // 19984 | |
string.Format("Errors parsing: {0}", files.Count()).Dump(); | |
foreach (var file in files) | |
{ | |
try { | |
var xmlFile = new ElmahXmlFile(file.FullName); | |
deleteFile = false; | |
/* Add custom logic to trigger deletions here. */ | |
if ( | |
(xmlFile.Details.Contains("submitApp_Click") && xmlFile.Message == "Thread was being aborted.") | |
|| (xmlFile.Message == "User came from beta") | |
) { | |
deleteFile = true; | |
} | |
/* End custom logic to trigger deletions */ | |
// Delete the file, if not testing. | |
if (deleteFile) { | |
if (debug && debugDumpDeletedFileName) { | |
string.Format("Deleting file: {0}", file.Name).Dump(); | |
} | |
if (!testingDeletions) { | |
file.Delete(); | |
} | |
continue; | |
} | |
if (debug) { | |
if (debugDumpErrorMessage) { | |
string.Format("Message: {0}", xmlFile.Message).Dump(); | |
} | |
} | |
} | |
catch (XmlException ex) { | |
ex.Message.Dump(); | |
file.Name.Dump(); | |
} | |
catch (Exception ex) { | |
ex.Message.Dump(); | |
} | |
} | |
} | |
// Define other methods and classes here | |
public class ElmahXmlFile { | |
public string Message; | |
public string Details; | |
public string PageUrl; | |
public string QueryString; | |
public ElmahXmlFile(string xmlUri) { | |
var xml = XDocument.Load(xmlUri).Root; | |
if (xml.Element("serverVariables") != null && xml.Element("serverVariables").Elements("item").Where(i => i.Attribute("name").Value == "PATH_INFO").Count() == 1) | |
{ | |
this.PageUrl = xml.Element("serverVariables").Elements("item").First(i => i.Attribute("name").Value == "PATH_INFO").Element("value").Attribute("string").Value; | |
} | |
if (xml.Element("serverVariables") != null && xml.Element("serverVariables").Elements("item").Where(i => i.Attribute("name").Value == "QUERY_STRING").Count() == 1) { | |
this.QueryString = xml.Element("serverVariables").Elements("item").First(i => i.Attribute("name").Value == "QUERY_STRING").Element("value").Attribute("string").Value; | |
} | |
this.Message = xml.Attribute("message").Value; | |
this.Details = xml.Attribute("detail").Value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment