Ejemplo n.º 1
0
 public RouteResolver(IActivityMonitor monitor, RouteConfiguration c)
 {
     try
     {
         using (monitor.OpenGroup(LogLevel.Info, c.Name.Length > 0 ? string.Format("Resolving root configuration (name is '{0}').", c.Name) : "Resolving root configuration.", null))
         {
             ProtoResolver protoResolver = new ProtoResolver(monitor, c);
             NamedSubRoutes = new Dictionary <string, SubRouteConfigurationResolved>();
             using (monitor.OpenGroup(LogLevel.Info, "Building final routes.", null))
             {
                 var preRoot = new PreRoute(monitor, protoResolver.Root);
                 Root = new RouteConfigurationResolved(protoResolver.Root.FullName, c.ConfigData, preRoot.FinalizeActions());
                 foreach (IProtoSubRoute sub in protoResolver.NamedSubRoutes.Values)
                 {
                     var preRoute = new PreRoute(monitor, sub);
                     NamedSubRoutes.Add(sub.FullName, new SubRouteConfigurationResolved(sub, preRoute.FinalizeActions()));
                 }
                 Root.SubRoutes = protoResolver.Root.SubRoutes.Select(p => NamedSubRoutes[p.FullName]).ToArray();
                 foreach (IProtoSubRoute sub in protoResolver.NamedSubRoutes.Values)
                 {
                     NamedSubRoutes[sub.FullName].SubRoutes = sub.SubRoutes.Select(p => NamedSubRoutes[p.FullName]).ToArray();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         monitor.SendLine(LogLevel.Fatal, null, ex);
     }
 }
Ejemplo n.º 2
0
        SubRouteConfiguration FillSubRoute(IActivityMonitor monitor, XElement xml, SubRouteConfiguration sub)
        {
            using (monitor.OpenGroup(LogLevel.Trace, string.Format("Reading subordinated channel '{0}'.", sub.Name), null))
            {
                var matchOptions = (string)xml.Attribute("MatchOptions");
                var filter       = (string)xml.Attribute("TopicFilter");
                var regex        = (string)xml.Attribute("TopicRegex");
                if ((filter == null) == (regex == null))
                {
                    throw new XmlException("Subordinated Channel must define one TopicFilter or TopicRegex attribute (and not both)." + xml.GetLineColumnString());
                }
                RegexOptions opt = RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.ExplicitCapture;
                if (!String.IsNullOrWhiteSpace(matchOptions))
                {
                    if (!Enum.TryParse(matchOptions, true, out opt))
                    {
                        var expected = String.Join(", ", Enum.GetNames(typeof(RegexOptions)).Where(n => n != "None"));
                        throw new XmlException("MatchOptions value must be a subset of: " + expected + xml.GetLineColumnString());
                    }
                    monitor.SendLine(LogLevel.Trace, string.Format("MatchOptions for Channel '{0}' is: {1}.", sub.Name, opt), null);
                }
                else
                {
                    monitor.SendLine(LogLevel.Trace, string.Format("MatchOptions for Channel '{0}' defaults to: IgnoreCase, CultureInvariant, Multiline, ExplicitCapture.", sub.Name), null);
                }

                sub.RoutePredicate = filter != null?CreatePredicateFromWildcards(filter, opt) : CreatePredicateRegex(regex, opt);

                FillRoute(monitor, xml, sub);
                return(sub);
            }
        }
Ejemplo n.º 3
0
 void DumpSources(IActivityMonitor monitor, LogLevel level)
 {
     if (Sources != null && Sources.Count > 0)
     {
         using (monitor.OpenGroup(level, $"Processed {Sources.Count} source tree(s):"))
         {
             for (int i = 0; i < Sources.Count; ++i)
             {
                 using (monitor.OpenGroup(level, $"Source n°{i}"))
                 {
                     monitor.Log(level, Sources[i].ToString());
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initialization of the handler: computes the path.
 /// </summary>
 /// <param name="m"></param>
 public override void Initialize( IActivityMonitor m )
 {
     using( m.OpenGroup( LogLevel.Trace, string.Format( "Initializing BinaryFile handler '{0}' (MaxCountPerFile = {1}).", Name, _file.MaxCountPerFile ), null ) )
     {
         _file.Initialize( m );
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initialization of the handler: computes the path.
 /// </summary>
 /// <param name="m"></param>
 public override void Initialize(IActivityMonitor m)
 {
     using (m.OpenGroup(LogLevel.Trace, string.Format("Initializing TextFile handler '{0}' (MaxCountPerFile = {1}).", Name, _file.MaxCountPerFile), null))
     {
         _file.Initialize(m);
     }
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
            bool IProtoRouteConfigurationContext.AddRoute(SubRouteConfiguration route)
            {
                if (route == null)
                {
                    throw new ArgumentNullException();
                }
                var newSub = new Route(_resolver, this, route);

                if (!_resolver.RegisterSubRoute(newSub))
                {
                    Monitor.SendLine(LogLevel.Error, string.Format("Route named '{0}' is already declared.", newSub._fullName), null);
                    return(false);
                }
                using (Monitor.OpenGroup(LogLevel.Info, string.Format("Preprocessing route '{0}'.", newSub._fullName), null))
                {
                    newSub.ExecuteMetaConfigurations();
                    _subRoutes.Add(newSub);
                }
                return(true);
            }
Ejemplo n.º 8
0
        static Dictionary <string, LogFilter> ReadSourceOverrideFilter(XElement e, out SourceFilterApplyMode apply, IActivityMonitor monitor)
        {
            apply = SourceFilterApplyMode.None;
            using (monitor.OpenGroup(LogLevel.Trace, "Reading SourceOverrideFilter elements.", null))
            {
                try
                {
                    var s = e.Element("SourceOverrideFilter");
                    if (s == null)
                    {
                        monitor.CloseGroup("No source filtering (ApplyMode is None).");
                        return(new Dictionary <string, LogFilter>());
                    }
                    apply = s.AttributeEnum("ApplyMode", SourceFilterApplyMode.Apply);

                    var stranger = e.Elements("SourceOverrideFilter").Elements().FirstOrDefault(f => f.Name != "Add" && f.Name != "Remove");
                    if (stranger != null)
                    {
                        throw new XmlException("SourceOverrideFilter element must contain only Add and Remove elements." + stranger.GetLineColumnString());
                    }
                    var result = e.Elements("SourceOverrideFilter")
                                 .Elements()
                                 .Select(f => new
                    {
                        File   = f.AttributeRequired("File"),
                        Filter = f.Name == "Add"
                                                                    ? f.GetRequiredAttributeLogFilter("Filter")
                                                                    : (LogFilter?)LogFilter.Undefined
                    })
                                 .Where(f => !String.IsNullOrWhiteSpace(f.File.Value))
                                 .ToDictionary(f => f.File.Value, f => f.Filter.Value);
                    monitor.CloseGroup(String.Format("{0} source files, ApplyMode is {1}.", result.Count, apply));
                    return(result);
                }
                catch (Exception ex)
                {
                    monitor.SendLine(LogLevel.Error, "Error while reading SourceOverrideFilter element.", ex);
                    return(null);
                }
            }
        }
            /// <summary>
            /// Replays this monitor's content into another monitor.
            /// </summary>
            /// <param name="replay">The target monitor. Can not be null.</param>
            /// <param name="m">Optional monitor (nothing is logged when null).</param>
            public void Replay(IActivityMonitor replay, IActivityMonitor m = null)
            {
                using (m != null ? m.OpenGroup(LogLevel.Info, string.Format("Replaying activity from '{0}'.", MonitorId), null) : null)
                {
                    int nbMissing = 0;
                    int nbTotal   = 0;
                    using (var page = ReadFirstPage(1024))
                    {
                        foreach (ParentedLogEntry e in page.Entries)
                        {
                            ++nbTotal;
                            LogLevel level = e.Entry.LogLevel;
                            if (e.IsMissing)
                            {
                                ++nbMissing;
                                level = LogLevel.Trace;
                            }
                            switch (e.Entry.LogType)
                            {
                            case LogEntryType.Line:
                                replay.UnfilteredLog(e.Entry.Tags, level, e.Entry.Text, e.Entry.LogTime, CKException.CreateFrom(e.Entry.Exception), e.Entry.FileName, e.Entry.LineNumber);
                                break;

                            case LogEntryType.OpenGroup:
                                replay.UnfilteredOpenGroup(e.Entry.Tags, level, null, e.Entry.Text, e.Entry.LogTime, CKException.CreateFrom(e.Entry.Exception), e.Entry.FileName, e.Entry.LineNumber);
                                break;

                            case LogEntryType.CloseGroup:
                                replay.CloseGroup(e.Entry.LogTime, e.Entry.Conclusions);
                                break;
                            }
                        }
                        page.ForwardPage();
                    }
                    if (m != null)
                    {
                        m.CloseGroup(String.Format("Replayed {0} entries ({1} missing).", nbTotal, nbMissing));
                    }
                }
            }
Ejemplo n.º 10
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;
        }
Ejemplo n.º 11
0
        SubRouteConfiguration FillSubRoute( IActivityMonitor monitor, XElement xml, SubRouteConfiguration sub )
        {
            using( monitor.OpenGroup( LogLevel.Trace, string.Format( "Reading subordinated channel '{0}'.", sub.Name ), null ) )
            {
                var matchOptions = (string)xml.Attribute( "MatchOptions" );
                var filter = (string)xml.Attribute( "TopicFilter" );
                var regex = (string)xml.Attribute( "TopicRegex" );
                if( (filter == null) == (regex == null) )
                {
                    throw new XmlException( "Subordinated Channel must define one TopicFilter or TopicRegex attribute (and not both)." + xml.GetLineColumnString() );
                }
                RegexOptions opt = RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.ExplicitCapture;
                if( !String.IsNullOrWhiteSpace( matchOptions ) )
                {
                    if( !Enum.TryParse( matchOptions, true, out opt ) )
                    {
                        var expected = String.Join( ", ", Enum.GetNames( typeof( RegexOptions ) ).Where( n => n != "None" ) );
                        throw new XmlException( "MatchOptions value must be a subset of: " + expected + xml.GetLineColumnString() );
                    }
                    monitor.SendLine( LogLevel.Trace, string.Format( "MatchOptions for Channel '{0}' is: {1}.", sub.Name, opt ), null );
                }
                else monitor.SendLine( LogLevel.Trace, string.Format( "MatchOptions for Channel '{0}' defaults to: IgnoreCase, CultureInvariant, Multiline, ExplicitCapture.", sub.Name ), null );

                sub.RoutePredicate = filter != null ? CreatePredicateFromWildcards( filter, opt ) : CreatePredicateRegex( regex, opt );

                FillRoute( monitor, xml, sub );
                return sub;
            }
        }
Ejemplo n.º 12
0
        static Dictionary<string, LogFilter> ReadSourceOverrideFilter( XElement e, out SourceFilterApplyMode apply, IActivityMonitor monitor )
        {
            apply = SourceFilterApplyMode.None;
            using( monitor.OpenGroup( LogLevel.Trace, "Reading SourceOverrideFilter elements.", null ) )
            {
                try
                {
                    var s = e.Element( "SourceOverrideFilter" );
                    if( s == null )
                    {
                        monitor.CloseGroup( "No source filtering (ApplyMode is None)." );
                        return new Dictionary<string, LogFilter>();
                    }
                    apply = s.AttributeEnum( "ApplyMode", SourceFilterApplyMode.Apply );

                    var stranger =  e.Elements( "SourceOverrideFilter" ).Elements().FirstOrDefault( f => f.Name != "Add" && f.Name != "Remove" );
                    if( stranger != null )
                    {
                        throw new XmlException( "SourceOverrideFilter element must contain only Add and Remove elements." + stranger.GetLineColumnString() );
                    }
                    var result = e.Elements( "SourceOverrideFilter" )
                                    .Elements()
                                    .Select( f => new
                                                    {
                                                        File = f.AttributeRequired( "File" ),
                                                        Filter = f.Name == "Add"
                                                                    ? f.GetRequiredAttributeLogFilter( "Filter" )
                                                                    : (LogFilter?)LogFilter.Undefined
                                                    } )
                                    .Where( f => !String.IsNullOrWhiteSpace( f.File.Value ) )
                                    .ToDictionary( f => f.File.Value, f => f.Filter.Value );
                    monitor.CloseGroup( String.Format( "{0} source files, ApplyMode is {1}.", result.Count, apply ) );
                    return result;
                }
                catch( Exception ex )
                {
                    monitor.SendLine( LogLevel.Error, "Error while reading SourceOverrideFilter element.", ex );
                    return null;
                }
            }
        }
Ejemplo n.º 13
0
 public RouteResolver( IActivityMonitor monitor, RouteConfiguration c )
 {
     try
     {
         using( monitor.OpenGroup( LogLevel.Info, c.Name.Length > 0 ? string.Format( "Resolving root configuration (name is '{0}').", c.Name ) : "Resolving root configuration.", null ) )
         {
             ProtoResolver protoResolver = new ProtoResolver( monitor, c );
             NamedSubRoutes = new Dictionary<string, SubRouteConfigurationResolved>();
             using( monitor.OpenGroup( LogLevel.Info, "Building final routes.", null ) )
             {
                 var preRoot = new PreRoute( monitor, protoResolver.Root );
                 Root = new RouteConfigurationResolved( protoResolver.Root.FullName, c.ConfigData, preRoot.FinalizeActions() );
                 foreach( IProtoSubRoute sub in protoResolver.NamedSubRoutes.Values )
                 {
                     var preRoute = new PreRoute( monitor, sub );
                     NamedSubRoutes.Add( sub.FullName, new SubRouteConfigurationResolved( sub, preRoute.FinalizeActions() ) );
                 }
                 Root.SubRoutes = protoResolver.Root.SubRoutes.Select( p => NamedSubRoutes[p.FullName] ).ToArray();
                 foreach( IProtoSubRoute sub in protoResolver.NamedSubRoutes.Values )
                 {
                     NamedSubRoutes[sub.FullName].SubRoutes = sub.SubRoutes.Select( p => NamedSubRoutes[p.FullName] ).ToArray();
                 }
             }
         }
     }
     catch( Exception ex )
     {
         monitor.SendLine( LogLevel.Fatal, null, ex );
     }
 }