From 826bc841832958cacb5481713928d24a3c7e096b Mon Sep 17 00:00:00 2001 From: Sourabh Shirhatti Date: Fri, 21 Aug 2020 20:16:54 -0700 Subject: [PATCH] Add wireshark logging. Replaces #23088 (#25139) * Add wireshark style logging in Kestrel * More Append! * Update third party notices Co-authored-by: Kahbazi --- THIRD-PARTY-NOTICES.txt | 27 +++++++++- .../src/Middleware/Internal/LoggingStream.cs | 52 ++++++++++++++----- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/THIRD-PARTY-NOTICES.txt b/THIRD-PARTY-NOTICES.txt index 45f0a37b30..2a600a4d6d 100644 --- a/THIRD-PARTY-NOTICES.txt +++ b/THIRD-PARTY-NOTICES.txt @@ -267,4 +267,29 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. + +License notice for BedrockFramework +=================================== + +MIT License + +Copyright (c) 2019 David Fowler + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/Servers/Kestrel/Core/src/Middleware/Internal/LoggingStream.cs b/src/Servers/Kestrel/Core/src/Middleware/Internal/LoggingStream.cs index 15758f64f4..c4fea7a9e0 100644 --- a/src/Servers/Kestrel/Core/src/Middleware/Internal/LoggingStream.cs +++ b/src/Servers/Kestrel/Core/src/Middleware/Internal/LoggingStream.cs @@ -145,32 +145,56 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal return; } - var builder = new StringBuilder($"{method}[{buffer.Length}] "); + var builder = new StringBuilder(); + builder.Append(method); + builder.Append("["); + builder.Append(buffer.Length); + builder.AppendLine("]"); + + var charBuilder = new StringBuilder(); // Write the hex for (int i = 0; i < buffer.Length; i++) { builder.Append(buffer[i].ToString("X2")); builder.Append(" "); - } - builder.AppendLine(); - builder.Append("{0}"); - var rawDataBuilder = new StringBuilder(); - // Write the bytes as if they were ASCII - for (int i = 0; i < buffer.Length; i++) - { var bufferChar = (char)buffer[i]; - if (Char.IsControl(bufferChar)) + if (char.IsControl(bufferChar)) { - rawDataBuilder.Append("\\x"); - rawDataBuilder.Append(buffer[i].ToString("X2")); - continue; + charBuilder.Append("."); } - rawDataBuilder.Append(bufferChar); + else + { + charBuilder.Append(bufferChar); + } + + if ((i + 1) % 16 == 0) + { + builder.Append(" "); + builder.Append(charBuilder.ToString()); + builder.AppendLine(); + charBuilder.Clear(); + } + else if ((i + 1) % 8 == 0) + { + builder.Append(" "); + charBuilder.Append(" "); + } + } + if (charBuilder.Length > 0) + { + // 2 (between hex and char blocks) + num bytes left (3 per byte) + builder.Append(string.Empty.PadRight(2 + (3 * (16 - charBuilder.Length)))); + // extra for space after 8th byte + if (charBuilder.Length < 8) + { + builder.Append(" "); + } + builder.Append(charBuilder.ToString()); } - _logger.LogDebug(builder.ToString(), rawDataBuilder.ToString()); + _logger.LogDebug(builder.ToString()); } // The below APM methods call the underlying Read/WriteAsync methods which will still be logged.