|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using log4net; |
| 5 | +using log4net.Config; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace log4net.Tests.Integration |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + public class Log4NetIntegrationTests |
| 12 | + { |
| 13 | + [SetUp] |
| 14 | + public void SetUp() |
| 15 | + { |
| 16 | + var filesFromPreviousRuns = |
| 17 | + Directory.EnumerateFiles(TestContext.CurrentContext.TestDirectory) |
| 18 | + .Where(filePath => Path.GetFileName(filePath).StartsWith("integrationTestLogFile", StringComparison.InvariantCultureIgnoreCase)) |
| 19 | + .ToList(); |
| 20 | + filesFromPreviousRuns.ForEach(File.Delete); |
| 21 | + var directoriesFromPreviousRuns = |
| 22 | + Directory.EnumerateDirectories(TestContext.CurrentContext.TestDirectory) |
| 23 | + .Where(dirPath => Path.GetDirectoryName(dirPath)?.StartsWith("integrationTestLogDir", StringComparison.InvariantCultureIgnoreCase) == true) |
| 24 | + .ToList(); |
| 25 | + directoriesFromPreviousRuns.ForEach(d => Directory.Delete(d, true)); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void Log4Net_WritesLogFile_AndContentIsCorrect() |
| 31 | + { |
| 32 | + // Arrange: configure log4net from config file |
| 33 | + var config = "log4net.integration.basic.config"; |
| 34 | + string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config); |
| 35 | + var repo = LogManager.CreateRepository(Guid.NewGuid().ToString()); |
| 36 | + XmlConfigurator.Configure(repo, new FileInfo(configPath)); |
| 37 | + |
| 38 | + // Act: log |
| 39 | + var log = LogManager.GetLogger(repo.Name, "IntegrationTestLogger"); |
| 40 | + log.Info("Hello integration test"); |
| 41 | + log.Error("This is an error"); |
| 42 | + repo.Shutdown(); |
| 43 | + |
| 44 | + // Assert: log file exists and contains expected content |
| 45 | + string[] logLines = File.ReadAllLines("integrationTestLogFile_integration.log"); |
| 46 | + Assert.That(logLines.Length, Is.EqualTo(2)); |
| 47 | + Assert.That(logLines[0], Does.Contain("Hello integration test")); |
| 48 | + Assert.That(logLines[1], Does.Contain("This is an error")); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void Log4Net_WritesLogFile_AndContentIsCorrectAfterRestart() |
| 53 | + { |
| 54 | + for (int i = 0; i < 10; i++) |
| 55 | + { |
| 56 | + // Arrange: configure log4net from config file |
| 57 | + var config = "log4net.integration.basic.config"; |
| 58 | + string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config); |
| 59 | + var repo = LogManager.CreateRepository(Guid.NewGuid().ToString()); |
| 60 | + XmlConfigurator.Configure(repo, new FileInfo(configPath)); |
| 61 | + |
| 62 | + // Act: log |
| 63 | + var log = LogManager.GetLogger(repo.Name, "IntegrationTestLogger"); |
| 64 | + log.Info("Hello integration test"); |
| 65 | + log.Error("This is an error"); |
| 66 | + repo.Shutdown(); |
| 67 | + } |
| 68 | + |
| 69 | + // Assert: log file exists and contains expected content |
| 70 | + string[] logLines = File.ReadAllLines("integrationTestLogFile_integration.log"); |
| 71 | + Assert.That(logLines.Length, Is.EqualTo(20)); |
| 72 | + for (int i = 0; i < 10; i++) |
| 73 | + { |
| 74 | + Assert.That(logLines[i * 2], Does.Contain("Hello integration test")); |
| 75 | + Assert.That(logLines[i * 2 + 1], Does.Contain("This is an error")); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void Log4Net_WritesLogFile_WithRollAndNoAppend_AndContentIsCorrectAfterRestart() |
| 81 | + { |
| 82 | + for (int i = 0; i < 10; i++) |
| 83 | + { |
| 84 | + // Arrange: configure log4net from config file |
| 85 | + var config = "log4net.roll.config.xml"; |
| 86 | + string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config); |
| 87 | + var repo = LogManager.CreateRepository(Guid.NewGuid().ToString()); |
| 88 | + XmlConfigurator.Configure(repo, new FileInfo(configPath)); |
| 89 | + |
| 90 | + // Act: log |
| 91 | + var log = LogManager.GetLogger(repo.Name, "log"); |
| 92 | + for (int j = 0; j < 10; ++j) |
| 93 | + { |
| 94 | + log.Info($"Hello, log4net! {i} {j}"); |
| 95 | + } |
| 96 | + repo.Shutdown(); |
| 97 | + } |
| 98 | + |
| 99 | + // Assert: log file exists and contains expected content |
| 100 | + string[] logFiles = Directory.GetFiles("integrationTestLogDir_1"); |
| 101 | + Assert.That(logFiles.Length, Is.EqualTo(10)); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments