Ejemplo n.º 1
0
        public void RollFilesTest([Range(0, 4)] int numFilesExisting, [Range(0, 3)] int numOldFilesToKeep)
        {
            // Create existing files
            if (numFilesExisting > 0)
            {
                CreateFile(_TestFolder, _TestFileName, 0);
            }

            for (var i = 1; i < numFilesExisting; i++)
            {
                CreateFile(_TestFolder, GetFileName(i), i);
            }


            // Roll the files
            CLogFileRoller.RollLogs(Path.Combine(_TestFolder, _TestFileName), numOldFilesToKeep);


            // Check main file
            Assert.IsFalse(File.Exists(Path.Combine(_TestFolder, _TestFileName)), "Main file was not deleted.");
            //Check other files
            for (int i = 1; i <= Math.Max(numOldFilesToKeep, numFilesExisting); i++)
            {
                var fileToCheck = Path.Combine(_TestFolder, GetFileName(i));
                if (i <= Math.Min(numFilesExisting, numOldFilesToKeep))
                {
                    Assert.IsTrue(File.Exists(fileToCheck), $"File {GetFileName(i)} is missing.");
                    Assert.AreEqual((i - 1).ToString(), File.ReadAllText(fileToCheck), "Rotation is wrong");
                }
                else
                {
                    Assert.IsFalse(File.Exists(fileToCheck));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize the logging framework.
        /// </summary>
        /// <param name="logFolder">The folder where to write the log files.</param>
        /// <param name="fileNameMainLog">The name of the main log.</param>
        /// <param name="fileNameSongInfoLog">The name of the log for song problems.</param>
        /// <param name="fileNameCrashMarker">The name of the file which is used as crash marker.</param>
        /// <param name="currentVersion">The current version tag as it is displayed in the main menu.</param>
        /// <param name="showReporterFunc">Delegate to the function which should be called if the reporter have to been shown.</param>
        /// <param name="logLevel">The log level for log messages.</param>
        public static void Init(string logFolder, string fileNameMainLog, string fileNameSongInfoLog, string fileNameCrashMarker, string currentVersion, ShowReporterDelegate showReporterFunc, ELogLevel logLevel)
        {
            _LogFolder        = logFolder;
            _ShowReporterFunc = showReporterFunc;
            _CurrentVersion   = currentVersion;

            _CrashMarkerFilePath = Path.Combine(_LogFolder, fileNameCrashMarker);

            // Creates the log directory if it does not exist
            if (!Directory.Exists(_LogFolder))
            {
                Directory.CreateDirectory(_LogFolder);
            }

            var mainLogFilePath = Path.Combine(_LogFolder, fileNameMainLog);
            var songLogFilePath = Path.Combine(_LogFolder, fileNameSongInfoLog);

            // Check if crash marker file
            if (File.Exists(_CrashMarkerFilePath))
            {
                // There was a crash in the last run -> check version tag of the crashed application instance
                string versionTag;
                using (StreamReader reader = new StreamReader(_CrashMarkerFilePath, Encoding.UTF8))
                {
                    versionTag = (reader.ReadLine() ?? "").Trim();
                }

                // Delete the old marker
                File.Delete(_CrashMarkerFilePath);

#if !DEBUG
                if (_CurrentVersion == versionTag && File.Exists(mainLogFilePath))
                {
                    string logContent = File.ReadAllText(mainLogFilePath, Encoding.UTF8);
                    _ShowReporterFunc(crash: true,
                                      showContinue: true,
                                      vocaluxeVersionTag: versionTag,
                                      log: logContent,
                                      lastError: "Vocaluxe crashed while the last execution.");
                }
#endif
            }

            // Write new marker
            File.WriteAllText(_CrashMarkerFilePath, _CurrentVersion, Encoding.UTF8);

            CLogFileRoller.RollLogs(mainLogFilePath, 2);
            CLogFileRoller.RollLogs(songLogFilePath, 2);

            _MainLog = new LoggerConfiguration()
                       .MinimumLevel.Is(logLevel.ToSerilogLogLevel())
                       .Enrich.WithThreadId()
                       .Enrich.FromLogContext()
                       .Enrich.WithTimeStampFromStart()
                       .WriteTo.TextWriter(_MainLogStringWriter,
                                           outputTemplate: _MainLogTemplate)
                       // Json can be activated by adding "new CompactJsonFormatter()" as first argument
                       .WriteTo.File(mainLogFilePath,
                                     flushToDiskInterval: TimeSpan.FromSeconds(30),
                                     outputTemplate: _MainLogTemplate)
#if DEBUG
                       .WriteTo.Console(outputTemplate: _MainLogTemplate)
#endif
                       .CreateLogger();

            _SongLog = new LoggerConfiguration()
                       .MinimumLevel.Is(logLevel.ToSerilogLogLevel())
                       .WriteTo.File(songLogFilePath,
                                     flushToDiskInterval: TimeSpan.FromSeconds(60),
                                     outputTemplate: _SongLogTemplate)
#if DEBUG
                       .WriteTo.Console(outputTemplate: "[SongInfo] " + _SongLogTemplate)
#endif
                       .CreateLogger();

            // Adding first line to log with information about this run
            Information("Starting to log",
                        Params(new { Version = _CurrentVersion },
                               new { StartDate = DateTime.Now },
                               new { Id = Guid.NewGuid() }));
        }