public void TestLogWritesToGlobalLogFile() { // Because this test writes to a file, it is a functional test and should be logged // but it's also testing the test logging facility. So this is pretty meta ;) var tempDir = Path.Combine(Path.GetTempPath(), $"TestLogging_{Guid.NewGuid().ToString("N")}"); try { using (StartLog(out var loggerFactory)) { var logger = loggerFactory.CreateLogger("Test"); using (var testAssemblyLog = AssemblyTestLog.Create("FakeTestAssembly", tempDir)) { logger.LogInformation("Created test log in {baseDirectory}", tempDir); using (testAssemblyLog.StartTestLog(output: null, className: "FakeTestAssembly.FakeTestClass", loggerFactory: out var testLoggerFactory, minLogLevel: LogLevel.Trace, testName: "FakeTestName")) { var testLogger = testLoggerFactory.CreateLogger("TestLogger"); testLogger.LogInformation("Information!"); testLogger.LogTrace("Trace!"); } } logger.LogInformation("Finished test log in {baseDirectory}", tempDir); } var globalLogPath = Path.Combine(tempDir, "FakeTestAssembly", RuntimeInformation.FrameworkDescription.TrimStart('.'), "global.log"); var testLog = Path.Combine(tempDir, "FakeTestAssembly", RuntimeInformation.FrameworkDescription.TrimStart('.'), "FakeTestClass", $"FakeTestName.log"); Assert.True(File.Exists(globalLogPath), $"Expected global log file {globalLogPath} to exist"); Assert.True(File.Exists(testLog), $"Expected test log file {testLog} to exist"); var globalLogContent = MakeConsistent(File.ReadAllText(globalLogPath)); var testLogContent = MakeConsistent(File.ReadAllText(testLog)); Assert.Equal(@"[GlobalTestLog] [Information] Global Test Logging initialized. Set the 'ASPNETCORE_TEST_LOG_DIR' Environment Variable in order to create log files on disk. [GlobalTestLog] [Information] Starting test ""FakeTestName"" [GlobalTestLog] [Information] Finished test ""FakeTestName"" in DURATION ", globalLogContent, ignoreLineEndingDifferences: true); Assert.Equal(@"[TestLifetime] [Information] Starting test ""FakeTestName"" [TestLogger] [Information] Information! [TestLogger] [Verbose] Trace! [TestLifetime] [Information] Finished test ""FakeTestName"" in DURATION ", testLogContent, ignoreLineEndingDifferences: true); } finally { if (Directory.Exists(tempDir)) { Directory.Delete(tempDir, recursive: true); } } }
public void TestLogWritesToITestOutputHelper() { var output = new TestTestOutputHelper(); var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null); using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory)) { var logger = loggerFactory.CreateLogger("TestLogger"); logger.LogInformation("Information!"); } Assert.Equal(@"| TestLifetime Information: Starting test TestLogWritesToITestOutputHelper | TestLogger Information: Information! | TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION ", MakeConsistent(output.Output)); }
public void TestLogWritesToITestOutputHelper() { var output = new TestTestOutputHelper(); var assemblyLog = AssemblyTestLog.Create("NonExistant.Test.Assembly", baseDirectory: null); using (assemblyLog.StartTestLog(output, "NonExistant.Test.Class", out var loggerFactory)) { var logger = loggerFactory.CreateLogger("TestLogger"); logger.LogInformation("Information!"); // Trace is disabled by default logger.LogTrace("Trace!"); } Assert.Equal(@"[TIMESTAMP] TestLifetime Information: Starting test TestLogWritesToITestOutputHelper [TIMESTAMP] TestLogger Information: Information! [TIMESTAMP] TestLifetime Information: Finished test TestLogWritesToITestOutputHelper in DURATION ", MakeConsistent(output.Output), ignoreLineEndingDifferences: true); }
public void TestLogWritesToGlobalLogFile() { // Because this test writes to a file, it is a functional test and should be logged // but it's also testing the test logging facility. So this is pretty meta ;) using (StartLog(out var loggerFactory)) { var logger = loggerFactory.CreateLogger("Test"); var tempDir = Path.Combine(Path.GetTempPath(), $"TestLogging_{Guid.NewGuid().ToString("N")}"); using (var testAssemblyLog = AssemblyTestLog.Create("FakeTestAssembly", tempDir)) { logger.LogInformation("Created test log in {baseDirectory}", tempDir); using (testAssemblyLog.StartTestLog(output: null, className: "FakeTestAssembly.FakeTestClass", loggerFactory: out var testLoggerFactory, testName: "FakeTestName")) { var testLogger = testLoggerFactory.CreateLogger("TestLogger"); testLogger.LogInformation("Information!"); } } logger.LogInformation("Finished test log in {baseDirectory}", tempDir); var globalLogPath = Path.Combine(tempDir, "FakeTestAssembly", "global.log"); var testLog = Path.Combine(tempDir, "FakeTestAssembly", "FakeTestClass", $"FakeTestName.log"); Assert.True(File.Exists(globalLogPath), $"Expected global log file {globalLogPath} to exist"); Assert.True(File.Exists(testLog), $"Expected test log file {testLog} to exist"); var globalLogContent = MakeConsistent(File.ReadAllText(globalLogPath)); logger.LogInformation($"Global Log Content:{Environment.NewLine}{{content}}", globalLogContent); var testLogContent = MakeConsistent(File.ReadAllText(testLog)); logger.LogInformation($"Test Log Content:{Environment.NewLine}{{content}}", testLogContent); Assert.Equal(@"[GlobalTestLog] [Information] Global Test Logging initialized. Set the 'ASPNETCORE_TEST_LOG_DIR' Environment Variable in order to create log files on disk. [GlobalTestLog] [Information] Starting test ""FakeTestName"" [GlobalTestLog] [Information] Finished test ""FakeTestName"" in DURATION ", globalLogContent); Assert.Equal(@"[TestLifetime] [Information] Starting test ""FakeTestName"" [TestLogger] [Information] Information! [TestLifetime] [Information] Finished test ""FakeTestName"" in DURATION ", testLogContent); } }
public async Task GrpcTemplate() { // Setup AssemblyTestLog var assemblyLog = AssemblyTestLog.Create(Assembly.GetExecutingAssembly(), baseDirectory: Project.ArtifactsLogDir); using var testLog = assemblyLog.StartTestLog(Output, nameof(GrpcTemplateTest), out var loggerFactory); var logger = loggerFactory.CreateLogger("TestLogger"); Project = await ProjectFactory.GetOrCreateProject("grpc", Output); var createResult = await Project.RunDotNetNewAsync("grpc"); Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", Project, createResult)); var publishResult = await Project.RunDotNetPublishAsync(); Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", Project, publishResult)); var buildResult = await Project.RunDotNetBuildAsync(); Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", Project, buildResult)); var isOsx = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); var isWindowsOld = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.OSVersion.Version < new Version(6, 2); var unsupported = isOsx || isWindowsOld; using (var serverProcess = Project.StartBuiltProjectAsync(hasListeningUri: !unsupported, logger: logger)) { // These templates are HTTPS + HTTP/2 only which is not supported on Mac due to missing ALPN support. // https://github.com/dotnet/aspnetcore/issues/11061 if (isOsx) { serverProcess.Process.WaitForExit(assertSuccess: false); Assert.True(serverProcess.Process.HasExited, "built"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on macOS due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } else if (isWindowsOld) { serverProcess.Process.WaitForExit(assertSuccess: false); Assert.True(serverProcess.Process.HasExited, "built"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } else { Assert.False( serverProcess.Process.HasExited, ErrorMessages.GetFailedProcessMessageOrEmpty("Run built service", Project, serverProcess.Process)); } } using (var aspNetProcess = Project.StartPublishedProjectAsync(hasListeningUri: !unsupported)) { // These templates are HTTPS + HTTP/2 only which is not supported on Mac due to missing ALPN support. // https://github.com/dotnet/aspnetcore/issues/11061 if (isOsx) { aspNetProcess.Process.WaitForExit(assertSuccess: false); Assert.True(aspNetProcess.Process.HasExited, "published"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on macOS due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } else if (isWindowsOld) { aspNetProcess.Process.WaitForExit(assertSuccess: false); Assert.True(aspNetProcess.Process.HasExited, "published"); Assert.Contains("System.NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN support.", ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } else { Assert.False( aspNetProcess.Process.HasExited, ErrorMessages.GetFailedProcessMessageOrEmpty("Run published service", Project, aspNetProcess.Process)); } } }