Skip to content

Commit 3027997

Browse files
authored
Update Kestrel logs to use LoggerMessage (#34910)
1 parent e497ba8 commit 3027997

File tree

7 files changed

+305
-466
lines changed

7 files changed

+305
-466
lines changed

src/Servers/Kestrel/Core/src/CoreStrings.resx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,6 @@
417417
<data name="HttpErrorConnectionSpecificHeaderField" xml:space="preserve">
418418
<value>Request headers contain connection-specific header field.</value>
419419
</data>
420-
<data name="AuthenticationFailed" xml:space="preserve">
421-
<value>Failed to authenticate HTTPS connection.</value>
422-
</data>
423-
<data name="AuthenticationTimedOut" xml:space="preserve">
424-
<value>Authentication of the HTTPS connection timed out.</value>
425-
</data>
426420
<data name="InvalidServerCertificateEku" xml:space="preserve">
427421
<value>Certificate {thumbprint} cannot be used as an SSL server certificate. It has an Extended Key Usage extension but the usages do not include Server Authentication (OID 1.3.6.1.5.5.7.3.1).</value>
428422
</data>
@@ -593,12 +587,6 @@ For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?l
593587
<data name="GreaterThanOrEqualToZeroRequired" xml:space="preserve">
594588
<value>A value greater than or equal to zero is required.</value>
595589
</data>
596-
<data name="HttpsConnectionEstablished" xml:space="preserve">
597-
<value>Connection "{connectionId}" established using the following protocol: {protocol}</value>
598-
</data>
599-
<data name="Http2DefaultCiphersInsufficient" xml:space="preserve">
600-
<value>HTTP/2 over TLS is not supported on Windows versions older than Windows 10 and Windows Server 2016 due to incompatible ciphers or missing ALPN support. Falling back to HTTP/1.1 instead.</value>
601-
</data>
602590
<data name="Http2NoTlsWin81" xml:space="preserve">
603591
<value>HTTP/2 over TLS is not supported on Windows versions earlier than Windows 10 and Windows Server 2016 due to incompatible ciphers or missing ALPN support.</value>
604592
</data>
@@ -626,18 +614,6 @@ For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?l
626614
<data name="EndpointHasUnusedHttpsConfig" xml:space="preserve">
627615
<value>The non-HTTPS endpoint {endpointName} includes HTTPS-only configuration for {keyName}.</value>
628616
</data>
629-
<data name="FoundCertWithPrivateKey" xml:space="preserve">
630-
<value>Found certificate with private key and thumbprint {Thumbprint} in certificate store {StoreName}.</value>
631-
</data>
632-
<data name="LocatingCertWithPrivateKey" xml:space="preserve">
633-
<value>Searching for certificate with private key and thumbprint {Thumbprint} in the certificate store.</value>
634-
</data>
635-
<data name="FailedToLocateCertificateFromStore" xml:space="preserve">
636-
<value>Failure to locate certificate from store.</value>
637-
</data>
638-
<data name="FailedToOpenCertStore" xml:space="preserve">
639-
<value>Failed to open certificate store {StoreName}.</value>
640-
</data>
641617
<data name="Http3ConnectionFaulted" xml:space="preserve">
642618
<value>The HTTP/3 connection faulted.</value>
643619
</data>

src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelTrace.cs

Lines changed: 173 additions & 199 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,41 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Security.Cryptography.X509Certificates;
65
using Microsoft.Extensions.Logging;
76

87
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
98
{
10-
internal static class LoggerExtensions
9+
internal static partial class LoggerExtensions
1110
{
12-
// Category: DefaultHttpsProvider
13-
private static readonly Action<ILogger, string, string, Exception?> _locatedDevelopmentCertificate =
14-
LoggerMessage.Define<string, string>(
15-
LogLevel.Debug,
16-
new EventId(0, "LocatedDevelopmentCertificate"),
17-
"Using development certificate: {certificateSubjectName} (Thumbprint: {certificateThumbprint})");
11+
private const string BadDeveloperCertificateStateMessage = "The ASP.NET Core developer certificate is in an invalid state. To fix this issue, run the following commands " +
12+
"'dotnet dev-certs https --clean' and 'dotnet dev-certs https' to remove all existing ASP.NET Core development certificates and create a new untrusted developer certificate. " +
13+
"On macOS or Windows, use 'dotnet dev-certs https --trust' to trust the new certificate.";
1814

19-
private static readonly Action<ILogger, Exception?> _unableToLocateDevelopmentCertificate =
20-
LoggerMessage.Define(
21-
LogLevel.Debug,
22-
new EventId(1, "UnableToLocateDevelopmentCertificate"),
23-
"Unable to locate an appropriate development https certificate.");
15+
[LoggerMessage(0, LogLevel.Debug, "Using development certificate: {certificateSubjectName} (Thumbprint: {certificateThumbprint})", EventName = "LocatedDevelopmentCertificate")]
16+
private static partial void LocatedDevelopmentCertificate(this ILogger<KestrelServer> logger, string certificateSubjectName, string certificateThumbprint);
2417

25-
private static readonly Action<ILogger, string, Exception?> _failedToLocateDevelopmentCertificateFile =
26-
LoggerMessage.Define<string>(
27-
LogLevel.Debug,
28-
new EventId(2, "FailedToLocateDevelopmentCertificateFile"),
29-
"Failed to locate the development https certificate at '{certificatePath}'.");
18+
public static void LocatedDevelopmentCertificate(this ILogger<KestrelServer> logger, X509Certificate2 certificate) => LocatedDevelopmentCertificate(logger, certificate.Subject, certificate.Thumbprint);
3019

31-
private static readonly Action<ILogger, string, Exception?> _failedToLoadDevelopmentCertificate =
32-
LoggerMessage.Define<string>(
33-
LogLevel.Debug,
34-
new EventId(3, "FailedToLoadDevelopmentCertificate"),
35-
"Failed to load the development https certificate at '{certificatePath}'.");
20+
[LoggerMessage(1, LogLevel.Debug, "Unable to locate an appropriate development https certificate.", EventName = "UnableToLocateDevelopmentCertificate")]
21+
public static partial void UnableToLocateDevelopmentCertificate(this ILogger<KestrelServer> logger);
3622

37-
private static readonly Action<ILogger, Exception?> _badDeveloperCertificateState =
38-
LoggerMessage.Define(
39-
LogLevel.Error,
40-
new EventId(4, "BadDeveloperCertificateState"),
41-
CoreStrings.BadDeveloperCertificateState);
23+
[LoggerMessage(2, LogLevel.Debug, "Failed to locate the development https certificate at '{certificatePath}'.", EventName = "FailedToLocateDevelopmentCertificateFile")]
24+
public static partial void FailedToLocateDevelopmentCertificateFile(this ILogger<KestrelServer> logger, string certificatePath);
4225

43-
private static readonly Action<ILogger, string, Exception?> _developerCertificateFirstRun =
44-
LoggerMessage.Define<string>(
45-
LogLevel.Warning,
46-
new EventId(5, "DeveloperCertificateFirstRun"),
47-
"{Message}");
26+
[LoggerMessage(3, LogLevel.Debug, "Failed to load the development https certificate at '{certificatePath}'.", EventName = "FailedToLoadDevelopmentCertificate")]
27+
public static partial void FailedToLoadDevelopmentCertificate(this ILogger<KestrelServer> logger, string certificatePath);
4828

49-
private static readonly Action<ILogger, string, Exception?> _failedToLoadCertificate =
50-
LoggerMessage.Define<string>(
51-
LogLevel.Error,
52-
new EventId(6, "MissingOrInvalidCertificateFile"),
53-
"The certificate file at '{CertificateFilePath}' can not be found, contains malformed data or does not contain a certificate.");
29+
[LoggerMessage(4, LogLevel.Error, BadDeveloperCertificateStateMessage, EventName = "BadDeveloperCertificateState")]
30+
public static partial void BadDeveloperCertificateState(this ILogger<KestrelServer> logger);
5431

55-
private static readonly Action<ILogger, string, Exception?> _failedToLoadCertificateKey =
56-
LoggerMessage.Define<string>(
57-
LogLevel.Error,
58-
new EventId(7, "MissingOrInvalidCertificateKeyFile"),
59-
"The certificate key file at '{CertificateKeyFilePath}' can not be found, contains malformed data or does not contain a PEM encoded key in PKCS8 format.");
32+
[LoggerMessage(5, LogLevel.Warning, "{Message}", EventName = "DeveloperCertificateFirstRun")]
33+
public static partial void DeveloperCertificateFirstRun(this ILogger<KestrelServer> logger, string message);
6034

61-
public static void LocatedDevelopmentCertificate(this ILogger<KestrelServer> logger, X509Certificate2 certificate) => _locatedDevelopmentCertificate(logger, certificate.Subject, certificate.Thumbprint, null);
35+
[LoggerMessage(6, LogLevel.Error, "The certificate file at '{CertificateFilePath}' can not be found, contains malformed data or does not contain a certificate.", EventName = "MissingOrInvalidCertificateFile")]
36+
public static partial void FailedToLoadCertificate(this ILogger<KestrelServer> logger, string certificateFilePath);
6237

63-
public static void UnableToLocateDevelopmentCertificate(this ILogger<KestrelServer> logger) => _unableToLocateDevelopmentCertificate(logger, null);
64-
65-
public static void FailedToLocateDevelopmentCertificateFile(this ILogger<KestrelServer> logger, string certificatePath) => _failedToLocateDevelopmentCertificateFile(logger, certificatePath, null);
66-
67-
public static void FailedToLoadDevelopmentCertificate(this ILogger<KestrelServer> logger, string certificatePath) => _failedToLoadDevelopmentCertificate(logger, certificatePath, null);
68-
69-
public static void BadDeveloperCertificateState(this ILogger<KestrelServer> logger) => _badDeveloperCertificateState(logger, null);
70-
71-
public static void DeveloperCertificateFirstRun(this ILogger<KestrelServer> logger, string message) => _developerCertificateFirstRun(logger, message, null);
72-
73-
public static void FailedToLoadCertificate(this ILogger<KestrelServer> logger, string certificatePath) => _failedToLoadCertificate(logger, certificatePath, null);
74-
75-
public static void FailedToLoadCertificateKey(this ILogger<KestrelServer> logger, string certificateKeyPath) => _failedToLoadCertificateKey(logger, certificateKeyPath, null);
38+
[LoggerMessage(7, LogLevel.Error, "The certificate key file at '{CertificateKeyFilePath}' can not be found, contains malformed data or does not contain a PEM encoded key in PKCS8 format.", EventName = "MissingOrInvalidCertificateKeyFile")]
39+
public static partial void FailedToLoadCertificateKey(this ILogger<KestrelServer> logger, string certificateKeyFilePath);
7640
}
7741
}

src/Servers/Kestrel/Core/src/Middleware/HttpsConnectionMiddleware.cs

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -509,81 +509,45 @@ private static bool IsWindowsVersionIncompatibleWithHttp2()
509509
}
510510
}
511511

512-
internal static class HttpsConnectionMiddlewareLoggerExtensions
512+
internal static partial class HttpsConnectionMiddlewareLoggerExtensions
513513
{
514-
private static readonly Action<ILogger, Exception> _authenticationFailed =
515-
LoggerMessage.Define(
516-
logLevel: LogLevel.Debug,
517-
eventId: new EventId(1, "AuthenticationFailed"),
518-
formatString: CoreStrings.AuthenticationFailed);
514+
[LoggerMessage(1, LogLevel.Debug, "Failed to authenticate HTTPS connection.", EventName = "AuthenticationFailed")]
515+
public static partial void AuthenticationFailed(this ILogger<HttpsConnectionMiddleware> logger, Exception exception);
519516

520-
private static readonly Action<ILogger, Exception?> _authenticationTimedOut =
521-
LoggerMessage.Define(
522-
logLevel: LogLevel.Debug,
523-
eventId: new EventId(2, "AuthenticationTimedOut"),
524-
formatString: CoreStrings.AuthenticationTimedOut);
517+
[LoggerMessage(2, LogLevel.Debug, "Authentication of the HTTPS connection timed out.", EventName = "AuthenticationTimedOut")]
518+
public static partial void AuthenticationTimedOut(this ILogger<HttpsConnectionMiddleware> logger);
525519

526-
private static readonly Action<ILogger, string, SslProtocols, Exception?> _httpsConnectionEstablished =
527-
LoggerMessage.Define<string, SslProtocols>(
528-
logLevel: LogLevel.Debug,
529-
eventId: new EventId(3, "HttpsConnectionEstablished"),
530-
formatString: CoreStrings.HttpsConnectionEstablished);
520+
[LoggerMessage(3, LogLevel.Debug, "Connection {connectionId} established using the following protocol: {protocol}", EventName = "HttpsConnectionEstablished")]
521+
public static partial void HttpsConnectionEstablished(this ILogger<HttpsConnectionMiddleware> logger, string connectionId, SslProtocols protocol);
531522

532-
private static readonly Action<ILogger, Exception?> _http2DefaultCiphersInsufficient =
533-
LoggerMessage.Define(
534-
logLevel: LogLevel.Information,
535-
eventId: new EventId(4, "Http2DefaultCiphersInsufficient"),
536-
formatString: CoreStrings.Http2DefaultCiphersInsufficient);
523+
[LoggerMessage(4, LogLevel.Information, "HTTP/2 over TLS is not supported on Windows versions older than Windows 10 and Windows Server 2016 due to incompatible ciphers or missing ALPN support. Falling back to HTTP/1.1 instead.",
524+
EventName = "Http2DefaultCiphersInsufficient")]
525+
public static partial void Http2DefaultCiphersInsufficient(this ILogger<HttpsConnectionMiddleware> logger);
537526

538-
private static readonly Action<ILogger, string, Exception?> _locatingCertWithPrivateKey =
539-
LoggerMessage.Define<string>(
540-
logLevel: LogLevel.Debug,
541-
eventId: new EventId(5, "LocateCertWithPrivateKey"),
542-
formatString: CoreStrings.LocatingCertWithPrivateKey);
527+
[LoggerMessage(5, LogLevel.Debug, "Searching for certificate with private key and thumbprint {Thumbprint} in the certificate store.", EventName = "LocateCertWithPrivateKey")]
528+
private static partial void LocatingCertWithPrivateKey(this ILogger<HttpsConnectionMiddleware> logger, string thumbPrint);
543529

544-
private static readonly Action<ILogger, string, string, Exception?> _foundCertWithPrivateKey =
545-
LoggerMessage.Define<string, string>(
546-
logLevel: LogLevel.Debug,
547-
eventId: new EventId(6, "FoundCertWithPrivateKey"),
548-
formatString: CoreStrings.FoundCertWithPrivateKey);
530+
public static void LocatingCertWithPrivateKey(this ILogger<HttpsConnectionMiddleware> logger, X509Certificate2 certificate) => LocatingCertWithPrivateKey(logger, certificate.Thumbprint);
549531

550-
private static readonly Action<ILogger, Exception> _failedToFindCertificateInStore =
551-
LoggerMessage.Define(
552-
logLevel: LogLevel.Debug,
553-
eventId: new EventId(7, "FailToLocateCertificate"),
554-
formatString: CoreStrings.FailedToLocateCertificateFromStore);
555-
556-
557-
private static readonly Action<ILogger, string, Exception> _failedToOpenCertificateStore =
558-
LoggerMessage.Define<string>(
559-
logLevel: LogLevel.Debug,
560-
eventId: new EventId(8, "FailToOpenStore"),
561-
formatString: CoreStrings.FailedToOpenCertStore);
562-
563-
public static void AuthenticationFailed(this ILogger<HttpsConnectionMiddleware> logger, Exception exception) => _authenticationFailed(logger, exception);
564-
565-
public static void AuthenticationTimedOut(this ILogger<HttpsConnectionMiddleware> logger) => _authenticationTimedOut(logger, null);
566-
567-
public static void HttpsConnectionEstablished(this ILogger<HttpsConnectionMiddleware> logger, string connectionId, SslProtocols sslProtocol) => _httpsConnectionEstablished(logger, connectionId, sslProtocol, null);
568-
569-
public static void Http2DefaultCiphersInsufficient(this ILogger<HttpsConnectionMiddleware> logger) => _http2DefaultCiphersInsufficient(logger, null);
570-
571-
public static void LocatingCertWithPrivateKey(this ILogger<HttpsConnectionMiddleware> logger, X509Certificate2 certificate) => _locatingCertWithPrivateKey(logger, certificate.Thumbprint, null);
532+
[LoggerMessage(6, LogLevel.Debug, "Found certificate with private key and thumbprint {Thumbprint} in certificate store {StoreName}.", EventName = "FoundCertWithPrivateKey")]
533+
public static partial void FoundCertWithPrivateKey(this ILogger<HttpsConnectionMiddleware> logger, string thumbprint, string? storeName);
572534

573535
public static void FoundCertWithPrivateKey(this ILogger<HttpsConnectionMiddleware> logger, X509Certificate2 certificate, StoreLocation storeLocation)
574536
{
575537
var storeLocationString = storeLocation == StoreLocation.LocalMachine ? nameof(StoreLocation.LocalMachine) : nameof(StoreLocation.CurrentUser);
576-
577-
_foundCertWithPrivateKey(logger, certificate.Thumbprint, storeLocationString, null);
538+
FoundCertWithPrivateKey(logger, certificate.Thumbprint, storeLocationString);
578539
}
579540

580-
public static void FailedToFindCertificateInStore(this ILogger<HttpsConnectionMiddleware> logger, Exception exception) => _failedToFindCertificateInStore(logger, exception);
541+
[LoggerMessage(7, LogLevel.Debug, "Failure to locate certificate from store.", EventName = "FailToLocateCertificate")]
542+
public static partial void FailedToFindCertificateInStore(this ILogger<HttpsConnectionMiddleware> logger, Exception exception);
543+
544+
[LoggerMessage(8, LogLevel.Debug, "Failed to open certificate store {StoreName}.", EventName = "FailToOpenStore")]
545+
public static partial void FailedToOpenStore(this ILogger<HttpsConnectionMiddleware> logger, string? storeName, Exception exception);
581546

582547
public static void FailedToOpenStore(this ILogger<HttpsConnectionMiddleware> logger, StoreLocation storeLocation, Exception exception)
583548
{
584549
var storeLocationString = storeLocation == StoreLocation.LocalMachine ? nameof(StoreLocation.LocalMachine) : nameof(StoreLocation.CurrentUser);
585-
586-
_failedToOpenCertificateStore(logger, storeLocationString, exception);
550+
FailedToOpenStore(logger, storeLocationString, exception);
587551
}
588552
}
589553
}

0 commit comments

Comments
 (0)