示例#1
0
        private List <HostService> GetInnerContexts(
            DocumentBuildParameters parameters,
            IEnumerable <IDocumentProcessor> processors,
            TemplateProcessor templateProcessor,
            IHostServiceCreator creator)
        {
            var files = (from file in parameters.Files.EnumerateFiles().AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                         from p in (from processor in processors
                                    let priority = processor.GetProcessingPriority(file)
                                                   where priority != ProcessingPriority.NotSupported
                                                   group processor by priority into ps
                                                   orderby ps.Key descending
                                                   select ps.ToList()).FirstOrDefault() ?? new List <IDocumentProcessor> {
                null
            }
                         group file by p).ToList();

            var toHandleItems    = files.Where(s => s.Key != null);
            var notToHandleItems = files
                                   .Where(s => s.Key == null)
                                   .SelectMany(s => s)
                                   .Where(s => s.Type != DocumentType.Overwrite &&
                                          !s.File.EndsWith(".yaml.md", StringComparison.OrdinalIgnoreCase) &&
                                          !s.File.EndsWith(".yml.md", StringComparison.OrdinalIgnoreCase))
                                   .ToList();

            if (notToHandleItems.Count > 0)
            {
                Logger.LogWarning(
                    $"Unable to handle following files: {notToHandleItems.Select(s => s.File).ToDelimitedString()}. Do they miss `YamlMime` as the first line of file, e.g.: `### YamlMime:ManagedReference`?",
                    code: WarningCodes.Build.UnknownContentType);
            }

            try
            {
                return((from processor in processors.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                        join item in toHandleItems.AsParallel() on processor equals item.Key into g
                        from item in g.DefaultIfEmpty()
                        where item != null && item.Any(s => s.Type != DocumentType.Overwrite) // when normal file exists then processing is needed
                        select LoggerPhaseScope.WithScope(
                            processor.Name,
                            LogLevel.Verbose,
                            () => creator.CreateHostService(
                                parameters,
                                templateProcessor,
                                MarkdownService,
                                MetadataValidators,
                                processor,
                                item)
                            )).ToList());
            }
            catch (AggregateException ex)
            {
                throw ex.GetBaseException();
            }
        }
示例#2
0
        private List <HostService> GetInnerContexts(
            DocumentBuildParameters parameters,
            IEnumerable <IDocumentProcessor> processors,
            TemplateProcessor templateProcessor,
            IHostServiceCreator creator)
        {
            var files = (from file in parameters.Files.EnumerateFiles().AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                         from p in (from processor in processors
                                    let priority = processor.GetProcessingPriority(file)
                                                   where priority != ProcessingPriority.NotSupported
                                                   group processor by priority into ps
                                                   orderby ps.Key descending
                                                   select ps.ToList()).FirstOrDefault() ?? new List <IDocumentProcessor> {
                null
            }
                         group file by p).ToList();

            var toHandleItems    = files.Where(s => s.Key != null);
            var notToHandleItems = files.Where(s => s.Key == null);

            foreach (var item in notToHandleItems)
            {
                var sb = new StringBuilder();
                sb.AppendLine("Cannot handle following file:");
                foreach (var f in item)
                {
                    sb.Append("\t");
                    sb.AppendLine(f.File);
                }
                Logger.LogWarning(sb.ToString());
            }

            try
            {
                return((from processor in processors.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                        join item in toHandleItems.AsParallel() on processor equals item.Key into g
                        from item in g.DefaultIfEmpty()
                        select LoggerPhaseScope.WithScope(
                            processor.Name,
                            LogLevel.Verbose,
                            () => creator.CreateHostService(
                                parameters,
                                templateProcessor,
                                MarkdownService,
                                MetadataValidators,
                                processor,
                                item)
                            )).ToList());
            }
            catch (AggregateException ex)
            {
                throw new DocfxException(ex.InnerException?.Message, ex);
            }
        }
示例#3
0
文件: ScopeTest.cs 项目: runt18/docfx
        public void TestPhaseScope()
        {
            var listener = new TestLoggerListener {
                LogLevelThreshold = LogLevel.Diagnostic
            };
            var      logLevel = Logger.LogLevelThreshold;
            ILogItem item;

            try
            {
                Logger.LogLevelThreshold = LogLevel.Diagnostic;
                Logger.RegisterListener(listener);
                Action <bool> callback;

                Logger.LogInfo("test no phase scope");
                Assert.Null(listener.TakeAndRemove().Phase);

                using (new LoggerPhaseScope("A"))
                {
                    Logger.LogInfo("test in phase scope A");
                    Assert.Equal("A", listener.TakeAndRemove().Phase);

                    using (new LoggerPhaseScope("B"))
                    {
                        Logger.LogInfo("test in phase scope B");
                        Assert.Equal("A.B", listener.TakeAndRemove().Phase);

                        var captured = LoggerPhaseScope.Capture();
                        Assert.NotNull(captured);
                        callback = shouldLogPerformance =>
                        {
                            using (LoggerPhaseScope.Restore(captured, shouldLogPerformance))
                            {
                                Logger.LogInfo("test in captured phase scope B");
                            }
                        };
                    } // exit scope B.

                    using (new LoggerPhaseScope("C", true))
                    {
                        Logger.LogInfo("test in phase scope C");
                        Assert.Equal("A.C", listener.TakeAndRemove().Phase);

                        // run callback in scope C.
                        callback(false);
                        Assert.Equal("A.B", listener.TakeAndRemove().Phase);
                    } // exit scope C.

                    item = listener.TakeAndRemove();
                    Assert.Equal("A.C", item.Phase);
                    Assert.Equal(LogLevel.Diagnostic, item.LogLevel);
                } // exit scope A.

                Logger.LogInfo("test no phase scope");
                Assert.Null(listener.TakeAndRemove().Phase);

                // run callback in no scope.
                callback(true);
                Assert.Equal("A.B", listener.TakeAndRemove().Phase);
                item = listener.TakeAndRemove();
                Assert.Equal("A.B", item.Phase);
                Assert.Equal(LogLevel.Diagnostic, item.LogLevel);

                Logger.LogInfo("test no phase scope again");
                Assert.Null(listener.TakeAndRemove().Phase);
            }
            finally
            {
                Logger.UnregisterListener(listener);
                Logger.LogLevelThreshold = logLevel;
            }
        }
示例#4
0
        public void TestPhaseScope()
        {
            var      listener = TestLoggerListener.CreateLoggerListenerWithPhaseEqualFilter(null, LogLevel.Diagnostic);
            var      logLevel = Logger.LogLevelThreshold;
            ILogItem item;

            AmbientContext.InitializeAmbientContext("id");
            try
            {
                Logger.LogLevelThreshold = LogLevel.Diagnostic;
                Logger.RegisterListener(listener);
                Action <bool, int> callback;

                Logger.LogInfo("test no phase scope");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);
                Assert.Equal("id", AmbientContext.CurrentContext.Id);
                Assert.Equal("id.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                using (new LoggerPhaseScope("A"))
                {
                    Logger.LogInfo("test in phase scope A");
                    Assert.Equal("A", TakeFirstLogItemAndRemove(listener.Items).Phase);
                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);
                    Assert.Equal("id.3.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                    using (new LoggerPhaseScope("B"))
                    {
                        Logger.LogInfo("test in phase scope B");
                        Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);

                        Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                        Assert.Equal("id.3.3.2", AmbientContext.CurrentContext.GenerateNextCorrelationId());

                        var captured = LoggerPhaseScope.Capture();
                        Assert.NotNull(captured);
                        callback = (shouldLogPerformance, round) =>
                        {
                            using (shouldLogPerformance ?
                                   LoggerPhaseScope.Restore(captured, LogLevel.Diagnostic) :
                                   LoggerPhaseScope.Restore(captured))
                            {
                                Logger.LogInfo("test in captured phase scope B");
                                if (round == 1)
                                {
                                    Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                                    Assert.Equal("id.3.3.4", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                                }

                                if (round == 2)
                                {
                                    Assert.Equal("id.3.3", AmbientContext.CurrentContext.Id);
                                    Assert.Equal("id.3.3.6", AmbientContext.CurrentContext.GenerateNextCorrelationId());
                                }
                            }
                        };
                    } // exit scope B.

                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);

                    using (new LoggerPhaseScope("C", LogLevel.Diagnostic))
                    {
                        Logger.LogInfo("test in phase scope C");

                        Assert.Equal("A.C", TakeFirstLogItemAndRemove(listener.Items).Phase);

                        Assert.Equal("id.3.4", AmbientContext.CurrentContext.Id);

                        // run callback in scope C.
                        callback(false, 1);

                        Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);
                        Assert.Equal("id.3.4", AmbientContext.CurrentContext.Id);
                    } // exit scope C.

                    Assert.Equal("id.3", AmbientContext.CurrentContext.Id);

                    item = TakeFirstLogItemAndRemove(listener.Items);
                    Assert.Equal("A.C", item.Phase);
                    Assert.Equal(LogLevel.Diagnostic, item.LogLevel);
                } // exit scope A.

                Assert.Equal("id", AmbientContext.CurrentContext.Id);

                Logger.LogInfo("test no phase scope");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);

                // run callback in no scope.
                callback(true, 2);
                Assert.Equal("A.B", TakeFirstLogItemAndRemove(listener.Items).Phase);
                item = TakeFirstLogItemAndRemove(listener.Items);
                Assert.Equal("A.B", item.Phase);
                Assert.Equal(LogLevel.Diagnostic, item.LogLevel);

                Logger.LogInfo("test no phase scope again");
                Assert.Null(TakeFirstLogItemAndRemove(listener.Items).Phase);
            }
            finally
            {
                Logger.UnregisterListener(listener);
                Logger.LogLevelThreshold = logLevel;
                AmbientContext.CurrentContext.Dispose();
            }
        }
示例#5
0
 public TestListenerScope(string phaseName)
 {
     _listener = TestLoggerListener.CreateLoggerListenerWithPhaseStartFilter(phaseName);
     Logger.RegisterListener(_listener);
     _scope = new LoggerPhaseScope(phaseName);
 }