/// <summary> /// Return the first item found with MinPriority or higher. The item is removed from the ErrorList. /// If no item matches the specified priority (or the list is empty), null is returned. /// </summary> /// <param name="minPriority"></param> /// <returns>entry or null</returns> public ErrorLogItem GetFirstItem(ErrorPriorities minPriority) { lock ( mResourceLock ) { // Don't use 'ErrorList' -- we can't remove the item... foreach (ErrorLogItem item in mList) { if ((int)item.Priority >= (int)minPriority) { // Remove the item from the ErrorList mList.Remove(item); return(item); } } // If not in the local list, check the children foreach (IErrorLog child in mChildLogs) { ErrorLogItem item = child.GetFirstItem(minPriority); if (item != null) { return(item); } } } return(null); }
/// <summary> /// Throws the first item found with MinPriority or higher. The item is removed from the ErrorList. /// InnerException will contain the original StackTrace. If no item matches the specified priority /// (or the list is empty), no exception is thrown. /// </summary> /// <param name="minPriority"></param> public void ThrowFirstItem(ErrorPriorities minPriority) { ErrorLogItem item = GetFirstItem(minPriority); if (item != null) { // Don't directly throw the original item.Exception because throwing will // overwrite its StackTrace. Instead, wrap it in a new exception // of the same type, with the original exception as InnerException. Exception ex = (Exception)Activator.CreateInstance( item.Exception.GetType(), new object[] { item.Message, item.Exception }); // bye bye... throw ex; } }
/// <summary> /// Logs an Exception. /// </summary> /// <param name="ex">Exception to be logged</param> /// <param name="priority">WARNING or ERROR</param> protected void AddException(Exception ex, ErrorPriorities priority) { ErrorLogItem item; lock ( mResourceLock ) { // this is a good place to print errors in debug builds Debug.Print(String.Format("{0} {1}: {2}", mSource, priority, ex.Message)); item = new ErrorLogItem { Priority = priority, Source = mSource, Exception = ex }; mList.Add(item); } // if anyone is listening, raise an event. if (ErrorLogItemAdded != null) { ErrorLogItemAdded(this, new ErrorLogItemAddedArgs(item)); } }
public ErrorLogItemAddedArgs(ErrorLogItem item) { Item = item; }