/// <summary> /// Handles the specified ex. /// </summary> /// <param name="ex">The ex.</param> /// <param name="additionalInfo">The additional information.</param> /// <param name="ignoreTracking">if set to <c>true</c> [ignore tracking].</param> public void Handle(Exception ex, NameValueCollection additionalInfo = null, bool ignoreTracking = false) { if (!TimeEventTracker.CanEvent(ex.Message, _eventTracking, out int occurrences) || ignoreTracking) { return; } additionalInfo = additionalInfo ?? new NameValueCollection(); additionalInfo["TrackingId"] = SequentialGuid.NewGuid().ToString(); additionalInfo["Time (UTC)"] = DateTime.UtcNow.ToString(); if (occurrences != 0) { additionalInfo["Occurrences"] = occurrences.ToString("N0"); } foreach (var handler in _exceptionHandlers) { try { handler.Handle(ex, additionalInfo); } catch (Exception ex2) { // one of the handlers screwed up. var error = $"Exception Handler: '{handler.Name}' threw an unhandled exception."; var handlerException = new AggregateException(new Exception(error, ex2), ex); var errorMessage = ExceptionFormatter.ConstructMessage(handlerException, additionalInfo); _logger.LogError(errorMessage); } } }
/// <summary> /// Publishes the specified ex. /// </summary> /// <param name="ex">The ex.</param> /// <param name="additionalParameters">The additional parameters.</param> static public void Publish(Exception ex, NameValueCollection additionalParameters) { try { // add time stamps var now = DateTime.UtcNow; additionalParameters = additionalParameters ?? new NameValueCollection(); additionalParameters.Add("Local Time", now.ToString("yyyy/MM/dd, HH:mm:ss.fff")); additionalParameters.Add("UTC", now.ToUniversalTime().ToString("yyyy/MM/dd, HH:mm:ss.fff")); if (!TimeEventTracker.CanEvent(ex.Message, _eventTracking, out int occurrences)) { return; } additionalParameters = additionalParameters ?? new NameValueCollection(); if (occurrences != 0) { additionalParameters = additionalParameters ?? new NameValueCollection(); additionalParameters["Occurrences"] = occurrences.ToString("N0"); } var message = ExceptionFormatter.ConstructMessage(ex, additionalParameters); _logTextFileWriter.WriteLine(message); } catch (Exception) { // nothing we can do just swallow } }
/// <summary> /// Sends the exception. /// </summary> /// <param name="ex">The ex.</param> /// <param name="bIgnoreTracker">if set to <c>true</c> [b ignore tracker].</param> public override void SendException(Exception ex, bool bIgnoreTracker = false) { var eventTracker = new TimeSpan(0, WorkManagerConfig.EventTracker, 0); if (bIgnoreTracker || TimeEventTracker.CanEvent((int)StringHash.BKDRHash(ex.Message), eventTracker)) { // add additional information to the Exception Info. var additionalInfo = new NameValueCollection(); additionalInfo.Add("Timestamp", DateTimeOffset.Now.ToString()); additionalInfo.Add("Application Source", WorkManagerConfig.ApplicationName); ExceptionManagerContainer.Publish(ex, additionalInfo); } }
protected void DoOnException(Exception ex) { if (!UseLogExceptionTimeSpan || TimeEventTracker.CanEvent((Int32)ex.Message.BKDRHash(), LogExceptionTimeSpan)) { try { if (OnException != null) { OnException(ex); } else { LogError(ex.StackTrace); } } catch (Exception ex2) { LogError(ex2.StackTrace); } } }
/// <summary> /// Determines whether this instance can event the specified ex. /// </summary> /// <param name="ex">The ex.</param> /// <returns> /// <c>true</c> if this instance can event the specified ex; otherwise, <c>false</c>. /// </returns> static public bool CanEvent(Exception ex) { return(TimeEventTracker.CanEvent((int)ex.Message.BKDRHash(), _eventTracking)); }
/// <summary> /// Publishes the specified ex. /// </summary> /// <param name="ex">The ex.</param> /// <param name="additionalParameters">The additional parameters.</param> public void Publish(Exception ex, NameValueCollection additionalParameters) { int occurrences; if (!TimeEventTracker.CanEvent(ex.Message, _eventTracking, out occurrences)) { return; } additionalParameters = additionalParameters ?? new NameValueCollection(); if (occurrences != 0) { additionalParameters = additionalParameters ?? new NameValueCollection(); additionalParameters["Occurrences"] = occurrences.ToString("N0"); } lock (_publishers) { try { try { _unhandledExceptions.Clear(); _unhandledExceptions.Capacity = 0; foreach (PublisherInfo publisher in _publishers.ToArray()) { try { publisher.Publisher.Publish(ex, additionalParameters); } catch (Exception unhandledEx) { MiscHelper.DisposeObject(publisher); _publishers.Remove(publisher); var unEx = new UnhandledExceptionInfo() { PublisherAttributes = publisher.PublisherAttributes, Exception = unhandledEx }; _unhandledExceptions.Add(unEx); } } foreach (UnhandledExceptionInfo eInfo in _unhandledExceptions) { var eMsg = string.Format("The following Publisher: '{0}' caused an Unhandled exception", eInfo.PublisherAttributes.Attributes["name"]); var internalEx = new Exception(eMsg, eInfo.Exception); var exMessage = ExceptionFormatter.ConstructMessage(internalEx); MiscHelper.WriteToEventLog(_eventLogSource, exMessage, EventLogEntryType.Error); } } catch (Exception excep) { var exMessage = ExceptionFormatter.ConstructMessage(excep); MiscHelper.WriteToEventLog(_eventLogSource, exMessage, EventLogEntryType.Error); } } catch (Exception) { // not much we can do here. } } }