Primary configuration object that contains multiple SubRouteConfigurations and ActionConfigurations.
Example #1
0
 static GrandOutputConfiguration CreateDefaultConfig( string subFolder )
 {
     GrandOutputConfiguration def = new GrandOutputConfiguration();
     Debug.Assert( def.SourceOverrideFilterApplicationMode == SourceFilterApplyMode.None );
     Debug.Assert( def.GlobalDefaultFilter == null );
     var route = new RouteConfiguration();
     route.ConfigData = new GrandOutputChannelConfigData();
     route.AddAction( new BinaryFileConfiguration( "All" ) { Path = subFolder } );
     def.ChannelsConfiguration = route;
     return def;
 }
        public void RoutesAndActions()
        {
            RouteConfiguration c = 
                new RouteConfiguration()
                    .AddAction(
                        new ActionSequenceConfiguration( "FirstGroup" )
                            .AddAction( new TestActionConfiguration( "Sink1" ) )
                            .AddAction( new TestActionConfiguration( "Sink2" ) )
                            .AddAction( new ActionParallelConfiguration( "Parallel n°1" )
                                .AddAction( new TestActionConfiguration( "Sink3" ) )
                                .AddAction( new TestActionConfiguration( "Sink4" ) ) )
                            .AddAction( new ActionParallelConfiguration( "Parallel n°2" )
                                .AddAction( new TestActionConfiguration( "Sink3" ) )
                                .AddAction( new TestActionConfiguration( "Sink4" ) ) ) )
                    .AddAction( new TestActionConfiguration( "SecondGlobal" ) )
                    .DeclareRoute(
                        new SubRouteConfiguration( "CKTask", name => name.StartsWith( "CKTask:" ) )
                            .AddAction( new TestActionConfiguration( "TaskSink" ) ) 
                            .RemoveAction( "SecondGlobal" ) )
                    .AddAction( new TestActionConfiguration( "ForAllExceptCKTask" ) )
                    .DeclareRoute(
                        new SubRouteConfiguration( "Request", name => name.Contains( "/request/" ) )
                            .RemoveAction( "FirstGroup" )
                            .AddAction( new TestActionConfiguration( "RequestSink" ) )
                            .AddAction( new TestActionConfiguration( "AnotherRequestSink" ) )
                            .DeclareRoute(
                                new SubRouteConfiguration( "NoBugInRequest", name => name.Contains( "/BugFree/" ) ) { ImportParentActions = false } ) );
            
            var resolved = c.Resolve( TestHelper.ConsoleMonitor );
            Assert.That( resolved, Is.Not.Null );
            Assert.That( resolved.AllSubRoutes.Count, Is.EqualTo( 3 ) );

            var root = resolved.Root;
            Assert.That( root, Is.Not.Null );
            Assert.That( root.ActionsResolved, Is.Not.Null.And.Count.EqualTo( 3 ) );

            var ckTask = resolved.FindSubRouteByName( "CKTask" );
            Assert.That( ckTask, Is.Not.Null );
            Assert.That( ckTask.ActionsResolved, Is.Not.Null.And.Count.EqualTo( 2 ) );
            
            var request = resolved.FindSubRouteByName( "Request" );
            Assert.That( request, Is.Not.Null );
            Assert.That( request.ActionsResolved, Is.Not.Null.And.Count.EqualTo( 4 ) );

            var noBug = resolved.FindSubRouteByName( "NoBugInRequest" );
            Assert.That( noBug, Is.Not.Null );
            Assert.That( noBug.ActionsResolved, Is.Not.Null.And.Count.EqualTo( 0 ) );

        }
        /// <summary>
        /// Loads this configuration from a <see cref="XElement"/>.
        /// </summary>
        /// <param name="e">The xml element: its name must be GrandOutputConfiguration.</param>
        /// <param name="monitor">Monitor that will be used.</param>
        /// <returns>True on success, false if the configuration can not be read.</returns>
        public bool Load( XElement e, IActivityMonitor monitor )
        {
            if( e == null ) throw new ArgumentNullException( "e" );
            if( monitor == null ) throw new ArgumentNullException( "monitor" );
            try
            {
                if( e.Name != "GrandOutputConfiguration" ) throw new XmlException( "Element name must be <GrandOutputConfiguration>." + e.GetLineColumnString() );
                // AppDomainDefaultFilter was the name before.
                LogFilter? globalDefaultFilter = e.GetAttributeLogFilter( "GlobalDefaultFilter", false ) 
                                                    ?? e.GetAttributeLogFilter( "AppDomainDefaultFilter", false );

                SourceFilterApplyMode applyMode;
                Dictionary<string, LogFilter> sourceFilter = ReadSourceOverrideFilter( e, out applyMode, monitor );
                if( sourceFilter == null ) return false;

                RouteConfiguration routeConfig;
                using( monitor.OpenGroup( LogLevel.Trace, "Reading root Channel.", null ) )
                {
                    XElement channelElement = e.Element( "Channel" );
                    if( channelElement == null )
                    {
                        monitor.SendLine( LogLevel.Error, "Missing <Channel /> element." + e.GetLineColumnString(), null );
                        return false;
                    }
                    routeConfig = FillRoute( monitor, channelElement, new RouteConfiguration() );
                }
                // No error: set the new values.
                _routeConfig = routeConfig;
                _sourceFilter = sourceFilter;
                _sourceFilterApplyMode = applyMode;
                _globalDefaultFilter = globalDefaultFilter;
                return true;
            }
            catch( Exception ex )
            {
                monitor.SendLine( LogLevel.Error, null, ex );
            }
            return false;
        }
 RouteConfiguration FillRoute( IActivityMonitor monitor, XElement xml, RouteConfiguration route )
 {
     route.ConfigData = new GrandOutputChannelConfigData( xml );
     foreach( var e in xml.Elements() )
     {
         switch( e.Name.LocalName )
         {
             case "Channel":
                 route.DeclareRoute( FillSubRoute( monitor, e, new SubRouteConfiguration( e.AttributeRequired( "Name" ).Value, null ) ) );
                 break;
             case "Parallel": 
             case "Sequence":
             case "Add": DoSequenceOrParallelOrAdd( monitor, a => route.AddAction( a ), e );
                 break;
             default: throw new XmlException( "Element name must be <Add>, <Parallel>, <Sequence> or <Channel>." + e.GetLineColumnString() );
         }
     }
     return route;
 }
 static GrandOutputConfiguration CreateDefaultConfig()
 {
     GrandOutputConfiguration def = new GrandOutputConfiguration();
     Debug.Assert( def.SourceOverrideFilterApplicationMode == SourceFilterApplyMode.None );
     Debug.Assert( def.GlobalDefaultFilter == null );
     var route = new RouteConfiguration();
     route.ConfigData = new GrandOutputChannelConfigData() { MinimalFilter = LogFilter.Debug };
     route.AddAction( new TextFileConfiguration( "All" ) { Path = "GrandOutputDefault" } );
     def.ChannelsConfiguration = route;
     return def;
 }
 public void ActionNamesConflict()
 {
     RouteConfiguration c;
     {
         c = new RouteConfiguration()
                 .AddAction( new TestActionConfiguration( "Name" ) )
                 .AddAction( new TestActionConfiguration( "Name" ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new ActionParallelConfiguration( "Parallel" )
                     .AddAction( new TestActionConfiguration( "Name" ) )
                     .AddAction( new TestActionConfiguration( "Name" ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new ActionSequenceConfiguration( "Sequence" )
                     .AddAction( new TestActionConfiguration( "Name" ) )
                     .AddAction( new TestActionConfiguration( "Name" ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new TestActionConfiguration( "Name" ) )
                 .AddAction( new ActionSequenceConfiguration( "FirstGroup" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Not.Null, "Sequence acts as a namespace." );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new ActionSequenceConfiguration( "G1" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) )
                 .AddAction( new ActionSequenceConfiguration( "G2" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Not.Null, "Sequence hide names below them." );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new ActionSequenceConfiguration( "G1" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) )
                 .AddAction( new ActionParallelConfiguration( "P1" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) )
                 .AddAction( new ActionParallelConfiguration( "P2" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) )
                 .AddAction( new ActionSequenceConfiguration( "G2" )
                     .AddAction( new TestActionConfiguration( "Name" ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Not.Null, "Parallels also hide names below them." );
     }
 }
 public void InvalidNames()
 {
     {
         Assert.Throws<ArgumentNullException>( () => new RouteConfiguration().DeclareRoute( new SubRouteConfiguration( null, x => true ) ) );
         Assert.Throws<ArgumentNullException>( () => new RouteConfiguration().AddAction( new TestActionConfiguration( null ) ) );
     }
     RouteConfiguration c;
     {
         c = new RouteConfiguration()
                 .DeclareRoute( new SubRouteConfiguration( "", x => true )
                     .DeclareRoute( new SubRouteConfiguration( "", x => true ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null, "A route name can be empty but not 2 can be empty at the same time. The name of the root RouteConfiguration is always the empty string." );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new TestActionConfiguration( "" ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new TestActionConfiguration( "/" ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .AddAction( new TestActionConfiguration( "A/B" ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
 }
 public void RouteNamesConflict()
 {
     RouteConfiguration c;
     {
         c = new RouteConfiguration()
                 .DeclareRoute( new SubRouteConfiguration( "Name", x => true ) )
                 .DeclareRoute( new SubRouteConfiguration( "Name", x => true ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
     {
         c = new RouteConfiguration()
                 .DeclareRoute( new SubRouteConfiguration( "Name", x => true )
                     .DeclareRoute( new SubRouteConfiguration( "Name", x => true ) ) );
         Assert.That( c.Resolve( TestHelper.ConsoleMonitor ), Is.Null );
     }
 }