Skip to content

Commit 6fa2798

Browse files
Use [LoggerMessage] for logging
Change the template to use [LoggerMessage].
1 parent a36e21e commit 6fa2798

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

generators/app/templates/AuthenticationHandler.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace AspNet.Security.OAuth.<%= name %>;
1818
/// <summary>
1919
/// Defines a handler for authentication using <%= name %>.
2020
/// </summary>
21-
public class <%= name %>AuthenticationHandler : OAuthHandler<<%= name %>AuthenticationOptions>
21+
public partial class <%= name %>AuthenticationHandler : OAuthHandler<<%= name %>AuthenticationOptions>
2222
{
2323
/// <summary>
2424
/// Initializes a new instance of the <see cref="<%= name %>AuthenticationHandler"/> class.
@@ -56,12 +56,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
5656
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
5757
if (!response.IsSuccessStatusCode)
5858
{
59-
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
60-
"returned a {Status} response with the following payload: {Headers} {Body}.",
61-
/* Status: */ response.StatusCode,
62-
/* Headers: */ response.Headers.ToString(),
63-
/* Body: */ await response.Content.ReadAsStringAsync());
64-
59+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
6560
throw new HttpRequestException("An error occurred while retrieving the user profile from <%= name %>.");
6661
}
6762

@@ -74,4 +69,23 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
7469
await Events.CreatingTicket(context);
7570
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
7671
}
72+
73+
private static partial class Log
74+
{
75+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
76+
{
77+
UserProfileError(
78+
logger,
79+
response.StatusCode,
80+
response.Headers.ToString(),
81+
await response.Content.ReadAsStringAsync(cancellationToken));
82+
}
83+
84+
[LoggerMessage(1, LogLevel.Error, "An error occurred while retrieving the user profile: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
85+
private static partial void UserProfileError(
86+
ILogger logger,
87+
System.Net.HttpStatusCode status,
88+
string headers,
89+
string body);
90+
}
7791
}

0 commit comments

Comments
 (0)