From cdb3cbd62037dcb492305c93501c4a6e76d8cb51 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Tue, 18 Aug 2020 09:34:43 -0700 Subject: [PATCH] Remove \r\n for \n when writing to ostream (#24980) --- .../CommonLib/RedirectionOutput.cpp | 13 ++++++++++++- .../IIS/test/Common.FunctionalTests/LogFileTests.cs | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/RedirectionOutput.cpp b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/RedirectionOutput.cpp index a3897954fa..36ef5a2c19 100644 --- a/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/RedirectionOutput.cpp +++ b/src/Servers/IIS/AspNetCoreModuleV2/CommonLib/RedirectionOutput.cpp @@ -77,7 +77,18 @@ void FileRedirectionOutput::Append(const std::wstring& text) { if (m_file.is_open()) { - const auto multiByte = to_multi_byte_string(text, CP_UTF8); + auto multiByte = to_multi_byte_string(text, CP_UTF8); + + // Writing \r\n to an ostream will cause two new lines to be written rather + // than one. Change all \r\n to \n. + std::string slashRslashN = "\r\n"; + std::string slashN = "\n"; + size_t start_pos = 0; + while ((start_pos = multiByte.find(slashRslashN, start_pos)) != std::string::npos) { + multiByte.replace(start_pos, slashRslashN.length(), slashN); + start_pos += slashN.length(); + } + m_file << multiByte; } } diff --git a/src/Servers/IIS/IIS/test/Common.FunctionalTests/LogFileTests.cs b/src/Servers/IIS/IIS/test/Common.FunctionalTests/LogFileTests.cs index 11ea362317..a18346f099 100644 --- a/src/Servers/IIS/IIS/test/Common.FunctionalTests/LogFileTests.cs +++ b/src/Servers/IIS/IIS/test/Common.FunctionalTests/LogFileTests.cs @@ -53,6 +53,8 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests var contents = Helpers.ReadAllTextFromFile(Helpers.GetExpectedLogName(deploymentResult, _logFolderPath), Logger); Assert.Contains("TEST MESSAGE", contents); + Assert.DoesNotContain("\r\n\r\n", contents); + Assert.Contains("\r\n", contents); } // Move to separate file