/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle(Constraint constraint, string message, params object[] args) { TextWriter.Write(ToString()); IEnsuranceResponsibilityChainLink handler = Successor; if (handler != null) { handler.Handle(constraint, message, args); } }
/// <summary> /// Processes the ensurance handlers. /// </summary> /// <param name="handlersList">The handlers list.</param> public static void ProcessEnsuranceHandlers(IList <IEnsuranceResponsibilityChainLink> handlersList) { That(handlersList, Is.Not.Null); That(handlersList.Count, Is.GreaterThan(0)); _handler = handlersList[0]; IEnsuranceResponsibilityChainLink current = _handler; for (int i = 1; i < handlersList.Count; i++) { current.Successor = handlersList[i]; current = current.Successor; } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <exception cref="EnsuranceException">Always</exception> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle(Constraint constraint, string message, params object[] args) { try { MessageWriter messageWriter = new TextMessageWriter(new StringWriter(CultureInfo.CurrentCulture)); constraint.WriteMessageTo(messageWriter); throw new EnsuranceException(messageWriter.ToString()); } finally { IEnsuranceResponsibilityChainLink handler = Successor; if (handler != null) { handler.Handle(constraint, message, args); } } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( successor != null) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public void Handle(Constraint constraint, string message, params object[] args) { try { if (Debugger.IsAttached) { Debugger.Break(); } } finally { IEnsuranceResponsibilityChainLink handler = _successor; if (handler != null) { handler.Handle(constraint, message, args); } } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// IEnsuranceResponsibilityChainLink handler = successor; /// if( handler != null ) { /// handler.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public void Handle(Constraint constraint, string message, params object[] args) { try { MessageWriter messagewriter = new TextMessageWriter(); messagewriter.WriteLine(); constraint.WriteMessageTo(messagewriter); messagewriter.Write(new StackTraceWriter().ToString()); string tmpMessage = String.Format(CultureInfo.CurrentCulture, "{0}{1}", message, messagewriter); switch (_logger.DefaultLogSeverity) { case LogSeverity.Debug: _logger.Debug(tmpMessage, args); break; case LogSeverity.Info: _logger.Info(tmpMessage, args); break; case LogSeverity.Warn: _logger.Warn(tmpMessage, args); break; case LogSeverity.Error: _logger.Error(tmpMessage, args); break; case LogSeverity.Fatal: _logger.Fatal(tmpMessage, args); break; } } finally { IEnsuranceResponsibilityChainLink handler = _successor; if (handler != null) { handler.Handle(constraint, message, args); } } }
/// <summary> /// Handles an Ensurance failure for the given constraint. Implementors /// should always call /// <code> /// if( successor != null ) { /// successor.Handle( constraint, message, args ); /// } /// </code> /// So that the downstream handler can have a chance to process the /// failure. /// </summary> /// <param name="constraint">The constraint.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> public override void Handle(Constraint constraint, string message, params object[] args) { try { if (!string.IsNullOrEmpty(message)) { WriteMessageLine(message, args); } constraint.WriteMessageTo(this); } finally { IEnsuranceResponsibilityChainLink handler = Successor; if (handler != null) { handler.Handle(constraint, message, args); } } }
// 5. Implement the interface explicitly. void IEnsuranceHandler.Handle(Constraint constraint, string message, params object[] args) { // 5. Do your logging. try { if (_log.IsErrorEnabled) { _log.Error(constraint.ToString()); _log.WarnFormat(message, args); } } finally { // 6. ALWAYS pass the call onto your successor. IEnsuranceResponsibilityChainLink handler = _successor; if (handler != null) { handler.Handle(constraint, message, args); } } }
// 4. We want to do some static initialization that cannot be done outside // of the static constructor. static EnsureWithLogAndThrow() { _handler = new LoggingEnsuranceHandler(); _handler.Successor = new ExceptionEnsuranceHandler(); }