static void CheckParents(LoggerBuilderCollection builders,
                                 LoggerBuilder lb, Logger logger) {

            if (lb.ComputeParent(null).Length == 0) {
                return;
            }

            HashSet<string> ancestors = new HashSet<string>();
            LoggerBuilder current = lb;
            StringBuilder forwards = new StringBuilder(current.Name);

            while (current != null) {
                if (!ancestors.Add(current.ComputeName())) {
                    Traceables.DetectedCycleInLogParents(forwards.ToString());
                    logger.ForwardToParentBlocked = true;
                    break;
                }
                string parent = current.ComputeParent(null);
                if (parent.Length == 0)
                    break;

                // Detect missing parents
                current = builders[parent];
                if (current == null) {
                    Traceables.MissingParentLoggerReference(lb.Name);
                    break;
                }

                forwards.Append(" -> " + parent);
            }
        }
 protected override void Capture(LoggerEvent loggerEvent)
 {
     if (loggerEvent.Level == null)
     {
         throw new NotImplementedException();
     }
     RunInspectors(loggerEvent);
     try {
         target.Write(loggerEvent);
     } catch (Exception ex) {
         Traceables.FinalCaptureFailed(ex);
         target = Target.Null;
     }
 }
        static void Init() {
            if (init)
                return;

            init = true;
            int loggerCount = InstrumentationConfiguration.Current.Loggers.Count;
            Traceables.RootLoggerInitializing(loggerCount);

            AppDomain.CurrentDomain.ProcessExit += LeaveLogger;
            AppDomain.CurrentDomain.DomainUnload += LeaveLogger;

            // Break cycles in forwarding
            var builders = InstrumentationConfiguration.Current.Loggers;

            foreach (var lb in builders) {
                lb.ComputeParent(builders);

                Logger logger = BuildLoggerSafe(lb, lb.Name);
                bool hasParent = !string.IsNullOrWhiteSpace(lb.Parent);

                if (hasParent) {
                    if (lb.Name == "root") {
                        Traceables.RootCannotHaveParent();
                    }
                    if (!(lb.Target is NullTarget || lb.Target is ForwardingTarget)) {
                        Traceables.CannotSpecifyParentAndTarget(lb.Name);
                    }
                }

                CheckParents(builders, lb, logger);
            }

            _root = cache.GetValueOrDefault("root", Logger.Null);

            // Clone list to prevent concurrent modification in
            // ForwardingTarget
            foreach (var log in cache.Values.ToArray()) {
                log.Initialize();
            }

            Traceables.RootLoggerDoneInitializing();
        }
        internal void RunInstruments(LoggerEvent evt)
        {
            foreach (var i in this.Instruments)
            {
                if (i.ShouldSkipThis)
                {
                    continue;
                }

                try {
                    object result = i.Capture(evt);
                    if (result != null)
                    {
                        evt.Data[i.Name].Value = evt;
                    }
                } catch (Exception ex) {
                    i.SkipThis();
                    Traceables.InstrumentCaptureFailed(i.Name, ex);
                }
            }
        }
        static Logger BuildLoggerSafeCore(LoggerBuilder lb, string name) {
            if (lb != null) {
                try {
                    return lb.Build();

                } catch (Exception ex) {
                    Traceables.FailedToBuildLogger(name, ex);

                    if (Failure.IsCriticalException(ex)) {
                        throw;
                    }
                }
            }

            LoggerBuilder forward = LoggerBuilder.PickSourceForward(
                InstrumentationConfiguration.Current.Loggers, name);

            if (forward == null)
                return new NullLogger(name);

            var result = new DefaultLogger(name, new ForwardingTarget(forward.Name));
            result.Initialize();
            return result;
        }