private async Task ValidateLogEntriesAsync(LogLevel logLevel, Dictionary <int, string> truth) { using (var provider = new SimpleFileLoggerProvider(new SimpleFileLoggerOptions { RootFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Filter = level => level >= logLevel && level != LogLevel.None })) { var logger = provider.CreateLogger(CategoryName); logger.LogTrace("Trace"); logger.LogDebug("Debug"); logger.LogInformation("Information"); logger.LogWarning("Warning"); logger.LogError("Error"); logger.LogCritical("Critical"); logger.Log(LogLevel.None, "None"); var entries = await File.ReadAllLinesAsync(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), SimpleFileLoggerOptions.DefaultLogFilename)); Assert.AreEqual((truth.Count - 1) * 2, entries.Length); foreach (var kvp in truth) { Assert.IsTrue(entries[kvp.Key].Contains(kvp.Value)); } } // using log provider. }
public async Task ArchiveCheck() { using (var provider = new SimpleFileLoggerProvider(new SimpleFileLoggerOptions { RootFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), MaxFileSizeBytes = 1024 })) { var logger = provider.CreateLogger(CategoryName); for (var i = 1; i <= 10; i++) { for (var j = 1; j <= 15; j++) { logger.LogInformation($"{i}-{j}"); } // It appears as though we can write files too quickly so that sorting them later by LastWriteTime // doesn't work properly. Based on the docs there is some caching going on. This seems to fix it. // In real life these log files won't be archived this quickly anyway. // https://docs.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.lastwritetimeutc?view=netframework-4.7.2 await Task.Delay(500); } // file for loop } // using log provider. Assert.AreEqual(6, Directory.GetFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"*{Path.GetExtension(SimpleFileLoggerOptions.DefaultLogFilename)}").Length); }