Example #1
0
        public virtual ISubCommand Create(string[] args, ISubCommandController controller, SubCommandParseOption option)
        {
            var  parser  = CommandUtility.GetParser(option);
            var  options = Activator.CreateInstance <TOptions>();
            bool parsed  = parser.ParseArguments(args, options);

            if (!parsed && option == SubCommandParseOption.Strict)
            {
                throw new OptionParserException();
            }
            var helpOption = options as ICanPrintHelpMessage;

            if (helpOption != null && helpOption.PrintHelpMessage)
            {
                return(new HelpCommand(GetHelpText()));
            }

            var    buildOption = options as BuildCommandOptions;
            string root        = Path.GetDirectoryName(buildOption?.ConfigFile ?? Directory.GetCurrentDirectory());

            var logOption = options as LogOptions;

            if (logOption != null)
            {
                if (!string.IsNullOrWhiteSpace(logOption.LogFilePath) && Logger.FindAsyncListener(l => l is ReportLogListener) == null)
                {
                    Logger.RegisterAsyncListener(new ReportLogListener(logOption.LogFilePath, logOption.RepoRoot ?? string.Empty, root));
                }

                if (logOption.LogLevel.HasValue)
                {
                    Logger.LogLevelThreshold = logOption.LogLevel.Value;
                }

                Logger.WarningsAsErrors = logOption.WarningsAsErrors;

                if (!string.IsNullOrEmpty(logOption.CorrelationId))
                {
                    if (AmbientContext.CurrentContext == null)
                    {
                        AmbientContext.InitializeAmbientContext(logOption.CorrelationId);
                    }
                }
            }

            return(CreateCommand(options, controller));
        }
Example #2
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();
            }
        }