/// <summary> /// Creates a new error report based on the given exception. /// </summary> /// <param name="exception">The exception to base the <see cref="ErrorReport"/> on.</param> /// <param name="componentName">The name of the component. Should be set to allow easier investigation.</param> /// <returns>The created <see cref="ErrorReport"/>. -or- null, if the exception parameter was null.</returns> public static ErrorReport CreateErrorReport(Exception exception, string componentName) { if (exception == null) { return null; } ErrorReport report = new ErrorReport(exception); report.SourceComponentName = componentName; return CreateErrorReportInternal(report); }
private static ErrorReport CreateBasicErrorReport() { InvalidOperationException rootException = CreateTestException(); ErrorReport report = new ErrorReport(rootException); report.Timestamp = DateTime.UtcNow; return report; }
/// <summary> /// Parses the given XML-representation and creates an <see cref="ErrorReport"/> off of it. /// </summary> /// <param name="xml">The XML-representation to convert.</param> /// <returns></returns> public static ErrorReport Deserialize(string xml) { XDocument doc = XDocument.Parse(xml); XElement root = doc.Root; ErrorReport report = new ErrorReport(); report.Timestamp = DateTime.Parse(root.Element("Timestamp").Value).ToUniversalTime(); report.SourceComponentName = root.Element("ComponentName").Value; report.IsTerminating = bool.Parse(root.Element("IsTerminating").Value); report.Exception = ExceptionDetail.Deserialize(root.Element("ExceptionDetail")); return report; }
internal void AddSingleErrorReport(ErrorReport report) { ErrorReportViewModel reportVM = new ErrorReportViewModel() { Report = report }; ErrorReports.Add(reportVM); }
private static void StoreErrorReport(ErrorReport report) { try { EnsureErrorReportsDirectoryExists(); string componentNamePart = string.IsNullOrWhiteSpace(report.SourceComponentName) ? "UnknownComponent" : report.SourceComponentName.Trim(); string fileName = string.Format(ErrorReportPathTemplate, componentNamePart, report.Timestamp.ToLocalTime().ToString("yyyyMMddHHmmssffff"), ErrorReportExtension); string path = Path.Combine(ErrorReportPath, fileName); File.WriteAllText(path, report.Serialize()); report.ReportFileName = path; } catch (Exception) { // This is an error by itself. // TODO: Handle exception carefully. We don't want endless cycles. } }
private static ErrorReport CreateErrorReportInternal(ErrorReport report) { Assertions.AssertNotNull(report, "report"); report.Timestamp = DateTime.UtcNow; StoreErrorReport(report); return report; }
/// <summary> /// Attaches an event handler to the <see cref="E:AppDomain.CurrentDomain.UnhandledException"/> event, /// which automatically creates error reports for each escalated exception. /// </summary> /// <param name="componentName">The default component name to use for each new error report. Must not be null or empty.</param> public static void RegisterAppDomainUnhandledExceptionListener(string componentName) { Assertions.AssertNotEmpty(componentName, "componentName"); AppDomain.CurrentDomain.UnhandledException += (o, e) => { Exception exception = (Exception)e.ExceptionObject; ErrorReport report = new ErrorReport(exception); report.SourceComponentName = componentName; report.IsTerminating = e.IsTerminating; CreateErrorReportInternal(report); }; }