public void registering_multiple_times_the_same_client_is_an_error() { ActivityMonitor.AutoConfiguration = null; IActivityMonitor monitor = new ActivityMonitor(); Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 0 ) ); var counter = new ActivityMonitorErrorCounter(); monitor.Output.RegisterClient( counter ); Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 1 ) ); Assert.Throws<InvalidOperationException>( () => TestHelper.ConsoleMonitor.Output.RegisterClient( counter ), "Counter can be registered in one source at a time." ); var pathCatcher = new ActivityMonitorPathCatcher(); monitor.Output.RegisterClient( pathCatcher ); Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 2 ) ); Assert.Throws<InvalidOperationException>( () => TestHelper.ConsoleMonitor.Output.RegisterClient( pathCatcher ), "PathCatcher can be registered in one source at a time." ); IActivityMonitor other = new ActivityMonitor( applyAutoConfigurations: false ); ActivityMonitorBridge bridgeToConsole; using( monitor.Output.CreateBridgeTo( TestHelper.ConsoleMonitor.Output.BridgeTarget ) ) { bridgeToConsole = monitor.Output.FindBridgeTo( TestHelper.ConsoleMonitor.Output.BridgeTarget ); Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 3 ) ); Assert.That( bridgeToConsole.TargetMonitor, Is.SameAs( TestHelper.ConsoleMonitor ) ); Assert.Throws<InvalidOperationException>( () => other.Output.RegisterClient( bridgeToConsole ), "Bridge can be associated to only one source monitor." ); } Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 2 ) ); Assert.DoesNotThrow( () => other.Output.RegisterClient( bridgeToConsole ), "Now we can." ); Assert.DoesNotThrow( () => monitor.Output.UnregisterClient( bridgeToConsole ), "Already removed." ); monitor.Output.UnregisterClient( counter ); monitor.Output.UnregisterClient( pathCatcher ); Assert.That( monitor.Output.Clients.Count, Is.EqualTo( 0 ) ); }
public Status(IActivityMonitor m) { _m = m; _pathCatcher = new ActivityMonitorPathCatcher() { IsLocked = true }; _m.Output.RegisterClient(_pathCatcher); Success = true; }
public void ErrorCounterTests() { var monitor = new ActivityMonitor( applyAutoConfigurations: false ); using( monitor.Output.CreateBridgeTo( TestHelper.ConsoleMonitor.Output.BridgeTarget ) ) { // Registers the ErrorCounter first: it will be the last one to be called, but // this does not prevent the PathCatcher to work: the path elements reference the group // so that any conclusion arriving after PathCatcher.OnClosing are available. ActivityMonitorErrorCounter c = new ActivityMonitorErrorCounter(); monitor.Output.RegisterClient( c ); // Registers the PathCatcher now: it will be called BEFORE the ErrorCounter. ActivityMonitorPathCatcher p = new ActivityMonitorPathCatcher(); monitor.Output.RegisterClient( p ); Assert.That( c.GenerateConclusion, Is.False, "False by default." ); c.GenerateConclusion = true; Assert.That( c.Root.MaxLogLevel == LogLevel.None ); monitor.Trace().Send( "T1" ); Assert.That( !c.Root.HasWarnOrError && !c.Root.HasError ); Assert.That( c.Root.MaxLogLevel == LogLevel.Trace ); Assert.That( c.Root.ToString(), Is.Null ); monitor.Warn().Send( "W1" ); Assert.That( c.Root.HasWarnOrError && !c.Root.HasError ); Assert.That( c.Root.MaxLogLevel == LogLevel.Warn ); Assert.That( c.Root.ToString(), Is.Not.Null.And.Not.Empty ); monitor.Error().Send( "E2" ); Assert.That( c.Root.HasWarnOrError && c.Root.HasError ); Assert.That( c.Root.ErrorCount == 1 ); Assert.That( c.Root.MaxLogLevel == LogLevel.Error ); Assert.That( c.Root.ToString(), Is.Not.Null.And.Not.Empty ); c.Root.ClearError(); Assert.That( c.Root.HasWarnOrError && !c.Root.HasError ); Assert.That( c.Root.ErrorCount == 0 ); Assert.That( c.Root.MaxLogLevel == LogLevel.Warn ); Assert.That( c.Root.ToString(), Is.Not.Null ); c.Root.ClearWarn(); Assert.That( !c.Root.HasWarnOrError && !c.Root.HasError ); Assert.That( c.Root.MaxLogLevel == LogLevel.Info ); Assert.That( c.Root.ToString(), Is.Null ); using( monitor.OpenTrace().Send( "G1" ) ) { string errorMessage; using( monitor.OpenInfo().Send( "G2" ) ) { monitor.Error().Send( "E1" ); monitor.Fatal().Send( "F1" ); Assert.That( c.Root.HasWarnOrError && c.Root.HasError ); Assert.That( c.Root.ErrorCount == 1 && c.Root.FatalCount == 1 ); Assert.That( c.Root.WarnCount == 0 ); using( monitor.OpenInfo().Send( "G3" ) ) { Assert.That( !c.Current.HasWarnOrError && !c.Current.HasError ); Assert.That( c.Current.ErrorCount == 0 && c.Current.FatalCount == 0 && c.Current.WarnCount == 0 ); monitor.Error().Send( "An error..." ); Assert.That( c.Current.HasWarnOrError && c.Current.HasError ); Assert.That( c.Current.ErrorCount == 1 && c.Current.FatalCount == 0 && c.Current.WarnCount == 0 ); errorMessage = String.Join( "|", p.LastErrorPath.Select( e => e.Text + '-' + e.GroupConclusion.ToStringGroupConclusion() ) ); Assert.That( errorMessage, Is.EqualTo( "G1-|G2-|G3-|An error...-" ), "Groups are not closed: no conclusion exist yet." ); } errorMessage = String.Join( "|", p.LastErrorPath.Select( e => e.Text + '-' + e.GroupConclusion.ToStringGroupConclusion() ) ); Assert.That( errorMessage, Is.EqualTo( "G1-|G2-|G3-1 Error|An error...-" ), "G3 is closed: its conclusion is available." ); } errorMessage = String.Join( "|", p.LastErrorPath.Select( e => e.Text + '-' + e.GroupConclusion.ToStringGroupConclusion() ) ); Assert.That( errorMessage, Is.EqualTo( "G1-|G2-1 Fatal error, 2 Errors|G3-1 Error|An error...-" ) ); monitor.Error().Send( "E3" ); monitor.Fatal().Send( "F2" ); monitor.Warn().Send( "W2" ); Assert.That( c.Root.HasWarnOrError && c.Root.HasError ); Assert.That( c.Root.FatalCount == 2 ); Assert.That( c.Root.ErrorCount == 3 ); Assert.That( c.Root.MaxLogLevel == LogLevel.Fatal ); } Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.Text + '-' + e.GroupConclusion.ToStringGroupConclusion() ) ), Is.EqualTo( "G1-2 Fatal errors, 3 Errors, 1 Warning>F2-" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.Text + '-' + e.GroupConclusion.ToStringGroupConclusion() ) ), Is.EqualTo( "G1-2 Fatal errors, 3 Errors, 1 Warning>W2-" ) ); } }
public void PathCatcherTests() { var monitor = new ActivityMonitor( applyAutoConfigurations: false ); using( monitor.Output.CreateBridgeTo( TestHelper.ConsoleMonitor.Output.BridgeTarget ) ) { ActivityMonitorPathCatcher p = new ActivityMonitorPathCatcher(); monitor.Output.RegisterClient( p ); monitor.Trace().Send( "Trace n°1" ); Assert.That( p.DynamicPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Trace|Trace n°1" ) ); Assert.That( p.LastErrorPath, Is.Null ); Assert.That( p.LastWarnOrErrorPath, Is.Null ); monitor.Trace().Send( "Trace n°2" ); Assert.That( p.DynamicPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Trace|Trace n°2" ) ); Assert.That( p.LastErrorPath, Is.Null ); Assert.That( p.LastWarnOrErrorPath, Is.Null ); monitor.Warn().Send( "W1" ); Assert.That( p.DynamicPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Warn|W1" ) ); Assert.That( p.LastErrorPath, Is.Null ); Assert.That( p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Warn|W1" ) ); monitor.Error().Send( "E2" ); monitor.Warn().Send( "W1bis" ); Assert.That( p.DynamicPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Warn|W1bis" ) ); Assert.That( p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Error|E2" ) ); Assert.That( p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ).Single(), Is.EqualTo( "Warn|W1bis" ) ); p.ClearLastWarnPath(); Assert.That( p.LastErrorPath, Is.Not.Null ); Assert.That( p.LastWarnOrErrorPath, Is.Null ); p.ClearLastErrorPath(); Assert.That( p.LastErrorPath, Is.Null ); using( monitor.OpenTrace().Send( "G1" ) ) { using( monitor.OpenInfo().Send( "G2" ) ) { Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2" ) ); Assert.That( p.LastErrorPath, Is.Null ); using( monitor.OpenTrace().Send( "G3" ) ) { using( monitor.OpenInfo().Send( "G4" ) ) { monitor.Warn().Send( "W1" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2>G3>G4>W1" ) ); monitor.Info().Send( new Exception( "An exception logged as an Info.", new Exception( "With an inner exception. Since these exceptions have not been thrown, there is no stack trace." ) ), "Test With an exception: a Group is created. Since the text of the log is given, the Exception.Message must be displayed explicitely." ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2>G3>G4>Test With an exception: a Group is created. Since the text of the log is given, the Exception.Message must be displayed explicitely." ) ); try { try { try { try { throw new Exception( "Deepest exception." ); } catch( Exception ex ) { throw new Exception( "Yet another inner with inner Exception.", ex ); } } catch( Exception ex ) { throw new Exception( "Exception with inner Exception.", ex ); } } catch( Exception ex ) { throw new Exception( "Log without log text: the text of the entry is the Exception.Message.", ex ); } } catch( Exception ex ) { monitor.Trace().Send( ex ); Assert.That( p.DynamicPath.ToStringPath().Length > 0 ); } Assert.That( p.LastErrorPath, Is.Null ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Info|G4>Warn|W1" ) ); } Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.ToString() ) ), Is.EqualTo( "G1>G2>G3>G4" ) ); Assert.That( p.LastErrorPath, Is.Null ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Info|G4>Warn|W1" ) ); monitor.Error().Send( "E1" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2>G3>E1" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); } Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2>G3" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); } Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2" ) ); using( monitor.OpenTrace().Send( "G2Bis" ) ) { Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2Bis" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); monitor.Warn().Send( "W2" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>G2Bis>W2" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Trace|G2Bis>Warn|W2" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Info|G2>Trace|G3>Error|E1" ) ); } monitor.Fatal().Send( "F1" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "G1>F1" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Fatal|F1" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Fatal|F1" ) ); } // Extraneous closing are ignored. monitor.CloseGroup( null ); monitor.Warn().Send( "W3" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "W3" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Warn|W3" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Fatal|F1" ) ); // Extraneous closing are ignored. monitor.CloseGroup( null ); monitor.Warn().Send( "W4" ); Assert.That( String.Join( ">", p.DynamicPath.Select( e => e.Text ) ), Is.EqualTo( "W4" ) ); Assert.That( String.Join( ">", p.LastWarnOrErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Warn|W4" ) ); Assert.That( String.Join( ">", p.LastErrorPath.Select( e => e.MaskedLevel.ToString() + '|' + e.Text ) ), Is.EqualTo( "Trace|G1>Fatal|F1" ) ); p.ClearLastWarnPath( true ); Assert.That( p.LastErrorPath, Is.Null ); Assert.That( p.LastWarnOrErrorPath, Is.Null ); } }