/// <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> /// Constructs the exception message. /// </summary> /// <param name="msg">The MSG.</param> /// <param name="ex">The ex.</param> /// <param name="args">The arguments.</param> /// <returns></returns> private string ConstructExceptionMessage(string msg, Exception ex, params object[] args) { var sb = new StringBuilder(); sb.AppendFormat(msg, args); sb.AppendLine(); sb.Append(ExceptionFormatter.ConstructMessage(ex)); return(sb.ToString()); }
/// <summary> /// Publishes the specified exception. /// </summary> /// <param name="exception">The exception.</param> /// <param name="additionalParameters">The additional parameters.</param> public void Publish(Exception exception, NameValueCollection additionalParameters) { try { lock (this) { if (_logTextFileWriter != null) { var entry = ExceptionFormatter.ConstructMessage(exception, additionalParameters); _logTextFileWriter.WriteLine($"{entry}{Environment.NewLine}{_separator}{Environment.NewLine}"); } } } catch (Exception) { } }
/// <summary> /// Publishes the specified exception. /// </summary> /// <param name="exception">The exception.</param> /// <param name="additionalParameters">The additional parameters.</param> public void Publish(Exception exception, NameValueCollection additionalParameters) { var entry = ExceptionFormatter.ConstructMessage(exception, additionalParameters); MiscHelper.WriteToEventLog(_eventSource, entry, EventLogEntryType.Error); }
/// <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. } } }