Skip to content

Commit 826bc84

Browse files
shirhattiKahbazi
andauthored
Add wireshark logging. Replaces #23088 (#25139)
* Add wireshark style logging in Kestrel * More Append! * Update third party notices Co-authored-by: Kahbazi <A.Kahbazi@gmail.com>
1 parent 134850b commit 826bc84

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

THIRD-PARTY-NOTICES.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,29 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
267267
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
268268
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
269269
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
270-
SOFTWARE.
270+
SOFTWARE.
271+
272+
License notice for BedrockFramework
273+
===================================
274+
275+
MIT License
276+
277+
Copyright (c) 2019 David Fowler
278+
279+
Permission is hereby granted, free of charge, to any person obtaining a copy
280+
of this software and associated documentation files (the "Software"), to deal
281+
in the Software without restriction, including without limitation the rights
282+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
283+
copies of the Software, and to permit persons to whom the Software is
284+
furnished to do so, subject to the following conditions:
285+
286+
The above copyright notice and this permission notice shall be included in all
287+
copies or substantial portions of the Software.
288+
289+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
290+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
291+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
292+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
293+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
294+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
295+
SOFTWARE.

src/Servers/Kestrel/Core/src/Middleware/Internal/LoggingStream.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,56 @@ private void Log(string method, ReadOnlySpan<byte> buffer)
145145
return;
146146
}
147147

148-
var builder = new StringBuilder($"{method}[{buffer.Length}] ");
148+
var builder = new StringBuilder();
149+
builder.Append(method);
150+
builder.Append("[");
151+
builder.Append(buffer.Length);
152+
builder.AppendLine("]");
153+
154+
var charBuilder = new StringBuilder();
149155

150156
// Write the hex
151157
for (int i = 0; i < buffer.Length; i++)
152158
{
153159
builder.Append(buffer[i].ToString("X2"));
154160
builder.Append(" ");
155-
}
156-
builder.AppendLine();
157-
builder.Append("{0}");
158161

159-
var rawDataBuilder = new StringBuilder();
160-
// Write the bytes as if they were ASCII
161-
for (int i = 0; i < buffer.Length; i++)
162-
{
163162
var bufferChar = (char)buffer[i];
164-
if (Char.IsControl(bufferChar))
163+
if (char.IsControl(bufferChar))
164+
{
165+
charBuilder.Append(".");
166+
}
167+
else
168+
{
169+
charBuilder.Append(bufferChar);
170+
}
171+
172+
if ((i + 1) % 16 == 0)
173+
{
174+
builder.Append(" ");
175+
builder.Append(charBuilder.ToString());
176+
builder.AppendLine();
177+
charBuilder.Clear();
178+
}
179+
else if ((i + 1) % 8 == 0)
180+
{
181+
builder.Append(" ");
182+
charBuilder.Append(" ");
183+
}
184+
}
185+
if (charBuilder.Length > 0)
186+
{
187+
// 2 (between hex and char blocks) + num bytes left (3 per byte)
188+
builder.Append(string.Empty.PadRight(2 + (3 * (16 - charBuilder.Length))));
189+
// extra for space after 8th byte
190+
if (charBuilder.Length < 8)
165191
{
166-
rawDataBuilder.Append("\\x");
167-
rawDataBuilder.Append(buffer[i].ToString("X2"));
168-
continue;
192+
builder.Append(" ");
169193
}
170-
rawDataBuilder.Append(bufferChar);
194+
builder.Append(charBuilder.ToString());
171195
}
172196

173-
_logger.LogDebug(builder.ToString(), rawDataBuilder.ToString());
197+
_logger.LogDebug(builder.ToString());
174198
}
175199

176200
// The below APM methods call the underlying Read/WriteAsync methods which will still be logged.

0 commit comments

Comments
 (0)