Repository: dotnet/aspnetcore
File: src/Servers/Kestrel/test/FunctionalTests/RequestTests.cs (C#)

Code before:
        var appErrorLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
        var badRequestLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
        var connectionStoppedLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

        const int badRequestEventId = 17;
        const int appErrorEventId = 13;
        const int connectionStopEventId = 2;

        // Listen for the expected log message
        TestSink.MessageLogged += context =>
        {
            if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.BadRequests"
                && context.EventId == badRequestEventId
                && context.LogLevel == LogLevel.Debug)
            {
                badRequestLogged.SetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel"
                    && context.EventId.Id == appErrorEventId
                    && context.LogLevel > LogLevel.Debug)
            {
                appErrorLogged.SetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Connections"
                    && context.EventId == connectionStopEventId
                    && context.Message.Contains("stopped."))
            {
                connectionStoppedLogged.SetResult();
            }
        };

        await using var server = new TestServer(async context =>
        {
            var buffer = new byte[1024];

            // Attempt to read more of the body than will show up.
            await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
        }, new TestServiceContext(LoggerFactory));

        using (var connection = server.CreateConnection())
        {
            await connection.Send(
                "POST / HTTP/1.1",
                "Host:",
                "Connection: keep-alive",
                "Content-Type: application/json",
                "Content-Length: 100",  // Declare a larger body than will be sent
                "",
                "");
        }


Review comment (suggestion):
Is looking at the message contents nessessary? Is the eventid not specific enough? If it is required, consider a comment saying why you're testing the message here

Code after:
        var appErrorLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
        var badRequestLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
        var connectionStoppedLogged = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

        const int badRequestEventId = 17;
        const int appErrorEventId = 13;
        const int connectionStopEventId = 2;

        // Listen for the expected log message
        TestSink.MessageLogged += context =>
        {
            if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.BadRequests"
                && context.EventId == badRequestEventId
                && context.LogLevel == LogLevel.Debug)
            {
                badRequestLogged.TrySetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel"
                    && context.EventId.Id == appErrorEventId
                    && context.LogLevel > LogLevel.Debug)
            {
                appErrorLogged.TrySetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Connections"
                    && context.EventId == connectionStopEventId)
            {
                connectionStoppedLogged.TrySetResult();
            }
        };

        await using var server = new TestServer(async context =>
        {
            var buffer = new byte[1024];

            // Attempt to read more of the body than will show up.
            await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
        }, new TestServiceContext(LoggerFactory));

        using (var connection = server.CreateConnection())
        {
            await connection.Send(
                "POST / HTTP/1.1",
                "Host:",
                "Connection: keep-alive",
                "Content-Type: application/json",
                "Content-Length: 100",  // Declare a larger body than will be sent
                "",
                "");
        }

        await connectionStoppedLogged.Task.DefaultTimeout();