/// <summary> /// Constructor /// </summary> /// <param name="errorMessage">The error that has occured.</param> /// <param name="severity">What is the severity of this exception?</param> public TechnolutionException(string errorMessage, SeverityOption severity = SeverityOption.Undefined) : base(errorMessage) { try { Severity = severity; } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); ex.LogParameterValue(errorMessage, "errorMessage"); ex.LogParameterValue(severity, "severity"); throw ex.GetStackToThrow(); } }
/// <summary> /// Constuctor for an exception stack to be logged. /// </summary> /// <param name="exception">The exception that was caught.</param> /// <param name="severity">The severity of this exception.</param> public TechnolutionExceptionStack(Exception exception, SeverityOption severity = SeverityOption.Undefined) : base(exception.Message) { try { if (exception is TechnolutionExceptionStack) { InvertedStack = ((TechnolutionExceptionStack)exception); OriginalException = ((TechnolutionExceptionStack)exception).OriginalException; } else OriginalException = exception; Parameters = new Dictionary<string, string>(); Configure(exception, severity); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(exception, "exception"); throw te.GetStackToThrow(); } }
private void AddToStack(TechnolutionExceptionStack stackEntry) { try { if (InvertedStack == null) InvertedStack = stackEntry; else InvertedStack.AddToStack(stackEntry); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(stackEntry, "stackEntry"); throw te.GetStackToThrow(); } }
/// <summary> /// Configures the exception stack /// </summary> /// <param name="innerException">The inner exception that was caught.</param> /// <param name="severity">The severity of the exception.</param> private void Configure(Exception innerException, SeverityOption severity) { try { Severity = severity; StackFrame sf = null; MethodBase method = null; StackTrace st = new StackTrace(innerException, true); for (int i = 0; i < st.FrameCount; i++) { sf = st.GetFrame(i); method = sf.GetMethod(); if ((method.DeclaringType != typeof(TechnolutionExceptionStack)) && (method.DeclaringType != typeof(TechnolutionException))) { break; } } if (sf == null) { LineNumber = 0; CodeClassName = "Unknown, could not resolve stack trace"; MethodName = "Unknown, could not resolve stack trace"; AssemblyName = "Unknown, could not resolve stack trace"; return; } LineNumber = sf.GetFileLineNumber(); CodeClassName = method.DeclaringType.FullName; MethodName = method.Name; AssemblyName assemblyName = method.Module.Assembly.GetName(); AssemblyName = assemblyName.Name; AssemblyVersion = assemblyName.Version.ToString(); if (!(innerException is TechnolutionException)) { if (severity == SeverityOption.Undefined) { if (innerException is NotImplementedException) { Severity = SeverityOption.NotImplemented; } else if ((innerException is InvalidProgramException) || (innerException is InvalidOperationException) || (innerException is InvalidCastException) || (innerException is NullReferenceException) || (innerException is NotSupportedException)) { Severity = SeverityOption.Critical; } } } } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); ex.LogParameterValue(innerException, "innerException"); ex.LogParameterValue(severity, "severity"); throw ex.GetStackToThrow(); } }
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { try { base.GetObjectData(info, context); if (info != null) { info.AddValue("AssemblyName", AssemblyName); info.AddValue("AssemblyVersion", AssemblyVersion); info.AddValue("CodeClassName", CodeClassName); info.AddValue("MethodName", MethodName); info.AddValue("LineNumber", LineNumber); info.AddValue("Parameters", Parameters); info.AddValue("Severity", Severity); info.AddValue("OriginalException", OriginalException); info.AddValue("InvertedStack", InvertedStack); } } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(info, "info"); te.LogParameterValue(context, "context"); throw te.GetStackToThrow(); } }
/// <summary> /// Adds a parameter and its value to the exception details. /// </summary> /// <param name="parameterValue">The value of the parameter.</param> /// <param name="parameterName">The name of the parameter.</param> public void LogParameterValue(object parameterValue, string parameterName) { try { if (Parameters == null) Parameters = new Dictionary<string, string>(); string valueString = ReflectionUtil.GetExceptionLoggableText(parameterValue); if (Parameters.ContainsKey(parameterName)) { // This should never get hit as the name should be unique. Parameters[parameterName] = valueString; } else Parameters.Add(parameterName, valueString); } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); ex.LogParameterValue(parameterValue, "parameterValue"); ex.LogParameterValue(parameterName, "parameterName"); throw ex.GetStackToThrow(); } }
/// <summary> /// Finds the original exception (prior to being logged as a stack). /// </summary> /// <returns></returns> public TechnolutionException FindOriginalTechnolutionException() { try { if (OriginalException is TechnolutionExceptionStack) return ((TechnolutionExceptionStack)OriginalException).FindOriginalTechnolutionException(); return null; } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); throw ex.GetStackToThrow(); } }
/// <summary> /// Find the worst severity for this stack. /// </summary> /// <returns>The worst severity option.</returns> public SeverityOption FindWorstSeverity() { try { SeverityOption innerSeverity = Severity; if (OriginalException is TechnolutionExceptionStack) innerSeverity = ((TechnolutionExceptionStack)OriginalException).FindWorstSeverity(); if (innerSeverity < Severity) return innerSeverity; return Severity; } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); throw ex.GetStackToThrow(); } }
/// <summary> /// Logs an exception to the exception log file. Useful when failure to log to database. /// </summary> protected void LogExceptionToFile(TechnolutionExceptionStack exceptionStack) { try { if (exceptionStack == null) return; // Make sure we have correct stack. exceptionStack = exceptionStack.GetStackToThrow(); // Log to file. StringBuilder sb = new StringBuilder(); sb.Append(exceptionStack.ToString()); sb.AppendLine(); sb.AppendLine(); SaveToLogFile(sb.ToString()); } catch { // What do we do now? Ignore exception as we tried logging to the file system and failed. } }
/// <summary> /// Finds the original exception (prior to being logged as a stack). /// </summary> /// <returns></returns> public Exception FindOriginalException() { try { if (OriginalException == null) return this; if (OriginalException is TechnolutionExceptionStack) return ((TechnolutionExceptionStack)OriginalException).FindOriginalException(); if (OriginalException.InnerException != null) return OriginalException.InnerException; else return OriginalException; } catch (Exception e) { TechnolutionExceptionStack ex = new TechnolutionExceptionStack(e); throw ex.GetStackToThrow(); } }
/// <summary> /// Fires a warning event for notification of actions. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="addExtraSpace">Should there be extra space around this notification?</param> /// <param name="message">The message to notify users about.</param> /// <param name="args">The arguements to format into the message.</param> public static void FireWarningEvent(object sender, bool addExtraSpace, string message, params object[] args) { try { FireEvent(sender, new EventNotification(EventLevel.Warning, addExtraSpace, message, args)); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(message, "message"); te.LogParameterValue(addExtraSpace, "addExtraSpace"); te.LogParameterValue(args, "args"); throw te.GetStackToThrow(); } }
/// <summary> /// Fires a warning event for notification of actions. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="addExtraSpace">Should there be extra space around this notification?</param> /// <param name="messages">The messages to notify users about.</param> public static void FireWarningEvent(object sender, bool addExtraSpace, params EventMessage[] messages) { try { EventNotification notification = new EventNotification(EventLevel.Warning, addExtraSpace); notification.Messages.AddRange(messages); FireEvent(sender, notification); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(sender, "sender"); te.LogParameterValue(addExtraSpace, "addExtraSpace"); te.LogParameterValue(messages, "messages"); throw te.GetStackToThrow(); } }
/// <summary> /// Fires a trace event for notification of actions. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="addExtraSpace">Should there be extra space around this notification?</param> /// <param name="message">The message to notify users about.</param> /// <param name="args">The arguements to format into the message.</param> public static void FireTraceEvent(Type sender, bool addExtraSpace, string message, params object[] args) { try { if (sender == null) throw new InvalidCodeException("Trace messages require a sender to determine the namespace the trace message is sent from."); if (!CheckTraceNamespace(sender)) return; FireEvent(sender, new EventNotification(EventLevel.Trace, addExtraSpace, message, args)); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(message, "message"); te.LogParameterValue(addExtraSpace, "addExtraSpace"); te.LogParameterValue(args, "args"); throw te.GetStackToThrow(); } }
/// <summary> /// Fires a trace event for notification of actions. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="addExtraSpace">Should there be extra space around this notification?</param> /// <param name="messages">The messages to notify users about.</param> public static void FireTraceEvent(object sender, bool addExtraSpace, params EventMessage[] messages) { try { if (sender == null) return; if ((sender is Type) && (!CheckTraceNamespace((Type)sender))) return; if ((!(sender is Type)) && (!CheckTraceNamespace(sender.GetType()))) return; EventNotification notification = new EventNotification(EventLevel.Trace, addExtraSpace); notification.Messages.AddRange(messages); FireEvent(sender, notification); } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(sender, "sender"); te.LogParameterValue(addExtraSpace, "addExtraSpace"); te.LogParameterValue(messages, "messages"); throw te.GetStackToThrow(); } }
/// <summary> /// Fires an event for notification of actions. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="notification">The notification to send.</param> /// <param name="loggingOnly">Only send this to the logging listener.</param> public static void FireEvent(object sender, EventNotification notification, bool loggingOnly = false) { if (sender is string) throw new InvalidCodeException("Fire Notification Event called without Sender Object."); if (sender is EventMessage) throw new InvalidCodeException("Fire Notification Event called without Sender Object."); try { if (CheckLoggingLevel(notification.Level)) { if (LoggingEvent != null) { notification.SetParameterMessages(); LoggingEvent.Invoke(sender, notification); } } if ((CheckDisplayLevel(notification.Level)) && (!loggingOnly)) { if (DisplayEvent != null) { notification.SetParameterMessages(); DisplayEvent.Invoke(sender, notification); } } } catch (Exception e) { TechnolutionExceptionStack te = new TechnolutionExceptionStack(e); te.LogParameterValue(sender, "sender"); te.LogParameterValue(notification, "notification"); throw te.GetStackToThrow(); } }