@@ -399,6 +399,68 @@ await connection.Receive(
399
399
Assert . False ( loggedHigherThanDebug ) ;
400
400
}
401
401
402
+ [ Fact ]
403
+ public async Task IncompleteRequestBodyDoesNotLogAsApplicationError ( )
404
+ {
405
+ var appErrorLogged = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
406
+ var badRequestLogged = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
407
+ var connectionStoppedLogged = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
408
+
409
+ const int badRequestEventId = 17 ;
410
+ const int appErrorEventId = 13 ;
411
+ const int connectionStopEventId = 2 ;
412
+
413
+ // Listen for the expected log message
414
+ TestSink . MessageLogged += context =>
415
+ {
416
+ if ( context . LoggerName == "Microsoft.AspNetCore.Server.Kestrel.BadRequests"
417
+ && context . EventId == badRequestEventId
418
+ && context . LogLevel == LogLevel . Debug )
419
+ {
420
+ badRequestLogged . TrySetResult ( ) ;
421
+ }
422
+ else if ( context . LoggerName == "Microsoft.AspNetCore.Server.Kestrel"
423
+ && context . EventId . Id == appErrorEventId
424
+ && context . LogLevel > LogLevel . Debug )
425
+ {
426
+ appErrorLogged . TrySetResult ( ) ;
427
+ }
428
+ else if ( context . LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Connections"
429
+ && context . EventId == connectionStopEventId )
430
+ {
431
+ connectionStoppedLogged . TrySetResult ( ) ;
432
+ }
433
+ } ;
434
+
435
+ await using var server = new TestServer ( async context =>
436
+ {
437
+ var buffer = new byte [ 1024 ] ;
438
+
439
+ // Attempt to read more of the body than will show up.
440
+ await context . Request . Body . ReadAsync ( buffer , 0 , buffer . Length ) ;
441
+ } , new TestServiceContext ( LoggerFactory ) ) ;
442
+
443
+ using ( var connection = server . CreateConnection ( ) )
444
+ {
445
+ await connection . Send (
446
+ "POST / HTTP/1.1" ,
447
+ "Host:" ,
448
+ "Connection: keep-alive" ,
449
+ "Content-Type: application/json" ,
450
+ "Content-Length: 100" , // Declare a larger body than will be sent
451
+ "" ,
452
+ "" ) ;
453
+ }
454
+
455
+ await connectionStoppedLogged . Task . DefaultTimeout ( ) ;
456
+
457
+ // Bad request log message should have fired.
458
+ await badRequestLogged . Task . DefaultTimeout ( ) ;
459
+
460
+ // App error log message should not have fired.
461
+ Assert . False ( appErrorLogged . Task . IsCompleted ) ;
462
+ }
463
+
402
464
[ Fact ]
403
465
public async Task ConnectionResetBetweenRequestsIsLoggedAsDebug ( )
404
466
{
0 commit comments