internal ShouldBePreloaded(IBasicTestHelper basic, bool isMissingFromPreloaded)
 {
     _basic = basic;
     if (isMissingFromPreloaded)
     {
         throw new Exception($"Assembly {GetType().Assembly.GetName().Name} should appear in 'TestHelper/PreLoadedAssemblies' configuration.");
     }
 }
Пример #2
0
        static void CleanupTimedFolders(IActivityMonitor m, IBasicTestHelper basic, string basePath, int maxCurrentLogFolderCount, int maxArchivedLogFolderCount)
        {
            Debug.Assert(basePath.EndsWith(FileUtil.DirectorySeparatorString));
            // Note: The comparer is a reverse comparer. The most RECENT timed folder is the FIRST.
            GetTimedFolders(basePath, out SortedDictionary <DateTime, string> timedFolders, out string archivePath, false);
            if (timedFolders.Count > maxCurrentLogFolderCount)
            {
                int retryCount = 5;
retry:
                try
                {
                    if (archivePath == null)
                    {
                        m.Trace("Creating Archive folder.");
                        Directory.CreateDirectory(archivePath = basePath + "Archive");
                    }
                    foreach (var old in timedFolders.Values.Skip(maxCurrentLogFolderCount))
                    {
                        var fName = Path.GetFileName(old);
                        m.Trace($"Moving '{fName}' folder into Archive folder.");
                        var target = Path.Combine(archivePath, fName);
                        if (Directory.Exists(target))
                        {
                            target += '-' + Guid.NewGuid().ToString();
                        }
                        Directory.Move(old, target);
                    }
                    GetTimedFolders(archivePath, out timedFolders, out _, true);
                    foreach (var tooOld in timedFolders.Values.Skip(maxArchivedLogFolderCount))
                    {
                        basic.CleanupFolder(tooOld, false);
                    }
                }
                catch (Exception ex)
                {
                    if (--retryCount < 0)
                    {
                        m.Error($"Aborting Log's cleanup of timed folders in '{basePath}' after 5 retries.", ex);
                        return;
                    }
                    m.Warn($"Log's cleanup of timed folders in '{basePath}' failed. Retrying.", ex);
                    Thread.Sleep(retryCount * 100);
                    goto retry;
                }
            }
        }
Пример #3
0
 internal A(IBasicTestHelper basic)
 {
     _basic = basic;
 }
Пример #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);
                }
            });
        }