Example #1
0
        public void ParseTests()
        {
            Assert.That(LogFilter.Parse("Undefined"), Is.EqualTo(LogFilter.Undefined));
            Assert.That(LogFilter.Parse("Debug"), Is.EqualTo(LogFilter.Debug));
            Assert.That(LogFilter.Parse("Verbose"), Is.EqualTo(LogFilter.Verbose));
            Assert.That(LogFilter.Parse("Monitor"), Is.EqualTo(LogFilter.Monitor));
            Assert.That(LogFilter.Parse("Terse"), Is.EqualTo(LogFilter.Terse));
            Assert.That(LogFilter.Parse("Release"), Is.EqualTo(LogFilter.Release));
            Assert.That(LogFilter.Parse("Off"), Is.EqualTo(LogFilter.Off));

            Assert.That(LogFilter.Parse("{None,None}"), Is.EqualTo(LogFilter.Undefined));
            Assert.That(LogFilter.Parse("{Warn,None}"), Is.EqualTo(new LogFilter(LogLevelFilter.Warn, LogLevelFilter.None)));
            Assert.That(LogFilter.Parse("{Error,Warn}"), Is.EqualTo(new LogFilter(LogLevelFilter.Error, LogLevelFilter.Warn)));
            Assert.That(LogFilter.Parse("{Off,None}"), Is.EqualTo(new LogFilter(LogLevelFilter.Off, LogLevelFilter.None)));
            Assert.That(LogFilter.Parse("{Error,Error}"), Is.EqualTo(LogFilter.Release));
            Assert.That(LogFilter.Parse("{Info,Error}"), Is.EqualTo(LogFilter.Terse));
            Assert.That(LogFilter.Parse("{Fatal,Invalid}"), Is.EqualTo(new LogFilter(LogLevelFilter.Fatal, LogLevelFilter.Invalid)));

            Assert.That(LogFilter.Parse("{ Error , Error }"), Is.EqualTo(LogFilter.Release));
            Assert.That(LogFilter.Parse("{   Trace    ,    Info   }"), Is.EqualTo(LogFilter.Verbose));

            Assert.Throws <CKException>(() => LogFilter.Parse(" {Error,Error}"));
            Assert.Throws <CKException>(() => LogFilter.Parse("{Error,Error} "));
            Assert.Throws <CKException>(() => LogFilter.Parse("Error,Error}"));
            Assert.Throws <CKException>(() => LogFilter.Parse("{Error,Error"));
            Assert.Throws <CKException>(() => LogFilter.Parse("{Error,,Error}"));
            Assert.Throws <CKException>(() => LogFilter.Parse("{Error,Warn,Trace}"));
            Assert.Throws <CKException>(() => LogFilter.Parse("{}"));
        }
        /// <summary>
        /// Reads a <see cref="LogFilter"/>.
        /// </summary>
        /// <param name="this">This <see cref="XElement"/>.</param>
        /// <param name="name">Name of the attribute.</param>
        /// <param name="fallbackToUndefined">True to return <see cref="LogFilter.Undefined"/> instead of null when not found.</param>
        /// <returns>A nullable LogFilter.</returns>
        static public LogFilter?GetAttributeLogFilter(this XElement @this, string name, bool fallbackToUndefined)
        {
            XAttribute a = @this.Attribute(name);

            return(a != null?LogFilter.Parse(a.Value) : (fallbackToUndefined ? LogFilter.Undefined : (LogFilter?)null));
        }
 /// <summary>
 /// Reads a <see cref="LogFilter"/> that must exist.
 /// </summary>
 /// <param name="this">This <see cref="XElement"/>.</param>
 /// <param name="name">Name of the attribute.</param>
 /// <returns>A LogFilter.</returns>
 static public LogFilter GetRequiredAttributeLogFilter(this XElement @this, string name)
 {
     return(LogFilter.Parse(@this.AttributeRequired(name).Value));
 }
Example #4
0
        internal MonitorTestHelper(ITestHelperConfiguration config, IBasicTestHelper basic)
        {
            _config = config;
            _basic  = basic;

            basic.OnlyOnce(() =>
            {
                _logToBinFile = _config.GetBoolean("Monitor/LogToBinFile")
                                ?? _config.GetBoolean("Monitor/LogToBinFiles")
                                ?? false;
                _logToTextFile = _config.GetBoolean("Monitor/LogToTextFile")
                                 ?? _config.GetBoolean("Monitor/LogToTextFiles")
                                 ?? false;

                string logLevel = _config.Get("Monitor/LogLevel");
                if (logLevel != null)
                {
                    var lf = LogFilter.Parse(logLevel);
                    ActivityMonitor.DefaultFilter = lf;
                }
                LogFile.RootLogPath = basic.LogFolder;
                var conf            = new GrandOutputConfiguration();
                if (_logToBinFile)
                {
                    var binConf = new BinaryFileConfiguration
                    {
                        UseGzipCompression = true,
                        Path = FileUtil.CreateUniqueTimedFolder(LogFile.RootLogPath + "CKMon/", null, DateTime.UtcNow)
                    };
                    conf.AddHandler(binConf);
                }
                if (_logToTextFile)
                {
                    var txtConf = new TextFileConfiguration
                    {
                        Path = FileUtil.CreateUniqueTimedFolder(LogFile.RootLogPath + "Text/", null, DateTime.UtcNow)
                    };
                    conf.AddHandler(txtConf);
                }
                GrandOutput.EnsureActiveDefault(conf, clearExistingTraceListeners: false);
                var monitorListener = Trace.Listeners.OfType <MonitorTraceListener>().FirstOrDefault(m => m.GrandOutput == GrandOutput.Default);
                // (Defensive programming) There is no real reason for this listener to not be in the listeners, but it can be.
                if (monitorListener != null)
                {
                    // If our standard MonitorTraceListener has been injected, then we remove the StaticBasicTestHelper.SafeTraceListener
                    // that throws Exceptions instead of callinf FailFast.
                    Trace.Listeners.Remove("CK.Testing.SafeTraceListener");
                }
            });
            _monitor               = new ActivityMonitor("MonitorTestHelper");
            _console               = new ActivityMonitorConsoleClient();
            LogToConsole           = _config.GetBoolean("Monitor/LogToConsole") ?? false;
            basic.OnCleanupFolder += OnCleanupFolder;
            basic.OnlyOnce(() =>
            {
                var basePath = LogFile.RootLogPath + "Text" + FileUtil.DirectorySeparatorString;
                if (Directory.Exists(basePath))
                {
                    CleanupTimedFolders(_monitor, _basic, basePath, MaxCurrentLogFolderCount, MaxArchivedLogFolderCount);
                }
                basePath = LogFile.RootLogPath + "CKMon" + FileUtil.DirectorySeparatorString;
                if (Directory.Exists(basePath))
                {
                    CleanupTimedFolders(_monitor, _basic, basePath, MaxCurrentLogFolderCount, MaxArchivedLogFolderCount);
                }
            });
        }