public void SingleExceptionSafeFormatter() { FormatException fe = new FormatException("Format Exception"); string formattedException = StringProcessor.SafeExceptionFormatter(fe); Assert.IsTrue(formattedException.Contains("Format Exception")); }
/// <summary> /// Create a logger /// </summary> /// <returns>A logger</returns> protected Logger CreateLogger() { try { this.LoggedExceptionList = new List <string>(); this.LoggingEnabledSetting = LoggingConfig.GetLoggingEnabledSetting(); // Setup the exception listener if (LoggingConfig.GetFirstChanceHandler()) { AppDomain.CurrentDomain.FirstChanceException += this.FirstChanceHandler; } if (this.LoggingEnabledSetting != LoggingEnabled.NO) { return(LoggingConfig.GetLogger( StringProcessor.SafeFormatter( "{0} - {1}", this.GetFullyQualifiedTestClassName(), DateTime.UtcNow.ToString("yyyy-MM-dd-hh-mm-ss-ffff", CultureInfo.InvariantCulture)))); } else { return(new ConsoleLogger()); } } catch (Exception e) { ConsoleLogger newLogger = new ConsoleLogger(); newLogger.LogMessage(MessageType.WARNING, StringProcessor.SafeExceptionFormatter(e)); return(newLogger); } }
public void NestedExceptionSafeFormatter() { FormatException fe = new FormatException("Format Exception"); InvalidOperationException ioe = new InvalidOperationException("Invalid Operation Exception"); AggregateException ae = new AggregateException(fe, ioe); string formattedException = StringProcessor.SafeExceptionFormatter(ae); Assert.IsTrue(formattedException.Contains("Format Exception")); Assert.IsTrue(formattedException.Contains("Invalid Operation Exception")); Assert.IsTrue(formattedException.Contains("One or more errors occurred")); }
/// <summary> /// Log a failed soft assert message /// </summary> /// <param name="assertName">The name of the assertion</param> /// <param name="ex">The related exception</param> /// <param name="failureMessage">Failure message</param> private void LogFailedMessage(string assertName, Exception ex, string failureMessage) { string formattedException = StringProcessor.SafeExceptionFormatter(ex); if (string.IsNullOrEmpty(failureMessage)) { this.Log.LogMessage(MessageType.WARNING, $"SoftAssert '{assertName}' failed {Environment.NewLine}Exception: {formattedException}"); } else { this.Log.LogMessage(MessageType.WARNING, $"SoftAssert '{assertName}' failed {failureMessage}{Environment.NewLine}Exception: {formattedException}"); } }
/// <summary> /// Listen for any thrown exceptions /// </summary> /// <param name="source">Source object</param> /// <param name="e">The first chance exception</param> private void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e) { // Config settings for logging are messed up so we cannot safely log to anything but the console if (e.Exception is MaqsLoggingConfigException) { Console.WriteLine(StringProcessor.SafeExceptionFormatter(e.Exception)); return; } try { // Only do this is we are logging if (LoggingConfig.GetLoggingEnabledSetting() == LoggingEnabled.NO) { return; } Exception ex = e.Exception; // Check for an inner exception or if it is from the NUnit core if (ex.InnerException == null || ex.Source.Equals("nunit.core")) { // This is not the test run exception we are looking for return; } // Get the inner exception and specific test name Exception inner = ex.InnerException; string innerStack = inner.StackTrace ?? string.Empty; var message = StringProcessor.SafeExceptionFormatter(inner); var messages = this.LoggedExceptionList; // Make sure this error is associated with the current test and that we have not logged it yet if (innerStack.ToLower().Contains("magenic.maqs") || innerStack.Contains("at " + this.GetFullyQualifiedTestClassName() + "(")) { // Check if this is a duplicate massage if (messages.Count > 0 && messages.Last().Equals(message)) { return; } this.TryToLog(MessageType.ERROR, message); messages.Add(message); } } catch (Exception ex) { this.TryToLog(MessageType.WARNING, "Failed to log exception because: " + ex.Message); } }
public void ThrowSingleExceptionSafeFormatter() { FormatException fe = new FormatException("Format Exception"); try { throw fe; } catch (Exception e) { string formattedException = StringProcessor.SafeExceptionFormatter(e); Assert.IsTrue(formattedException.Contains("Format Exception")); Assert.IsTrue(formattedException.Contains("at UtilitiesUnitTesting.StringProcessorUnitTests.ThrowSingleExceptionSafeFormatter()")); } }
/// <summary> /// Create a logger /// </summary> /// <returns>A logger</returns> protected virtual ILogger CreateLogger(string logName, string logType, MessageType loggingLevel, LoggingEnabled enabled) { try { if (enabled != LoggingEnabled.NO) { return(LoggerFactory.GetLogger(logName, logType, loggingLevel)); } else { return(LoggerFactory.GetConsoleLogger()); } } catch (Exception e) { ILogger newLogger = LoggerFactory.GetConsoleLogger(); newLogger.LogMessage(MessageType.WARNING, StringProcessor.SafeExceptionFormatter(e)); return(newLogger); } }
/// <summary> /// Create a logger /// </summary> /// <returns>A logger</returns> protected ILogger CreateAndSetupLogger(string logName, string logType, LoggingEnabled enabled, bool firstChanceHandler) { try { this.LoggedExceptionList = new List <string>(); this.LoggingEnabledSetting = enabled; // Setup the exception listener if (firstChanceHandler) { AppDomain.CurrentDomain.FirstChanceException += this.FirstChanceHandler; } return(CreateLogger(logName, logType, LoggingConfig.GetLoggingLevelSetting(), enabled)); } catch (Exception e) { ILogger newLogger = LoggerFactory.GetConsoleLogger(); newLogger.LogMessage(MessageType.WARNING, StringProcessor.SafeExceptionFormatter(e)); return(newLogger); } }
public void ThrowDoubleNestedExceptionSafeFormatter() { FormatException fe = new FormatException("Format Exception"); InvalidOperationException ioe = new InvalidOperationException("Invalid Operation Exception"); AggregateException ae = new AggregateException(fe, ioe); AggregateException ae1; try { throw ae; } catch (Exception e) { ae1 = new AggregateException(e); } string formattedException = StringProcessor.SafeExceptionFormatter(ae1); Assert.IsTrue(formattedException.Contains("at UtilitiesUnitTesting.StringProcessorUnitTests.ThrowDoubleNestedExceptionSafeFormatter()")); Assert.IsTrue(formattedException.Contains("Format Exception")); Assert.IsTrue(formattedException.Contains("Invalid Operation Exception")); Assert.IsTrue(formattedException.Contains("One or more errors occurred")); }