Skip to content

Commit 0566a4a

Browse files
committed
Making use of LoggerMessage to fix the CA1848 rule violation.
1 parent 397c3bf commit 0566a4a

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed
Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
2+
using Griesoft.AspNetCore.ReCaptcha.Localization;
3+
using Microsoft.Extensions.Logging;
44

55
namespace Griesoft.AspNetCore.ReCaptcha.Extensions
66
{
77
internal static class LoggerExtensions
88
{
9+
private static readonly Action<ILogger, Exception?> _validationRequestFailed = LoggerMessage.Define(
10+
LogLevel.Warning,
11+
new EventId(1, nameof(ValidationRequestFailed)),
12+
Resources.RequestFailedErrorMessage);
913

14+
private static readonly Action<ILogger, Exception?> _validationRequestUnexpectedException = LoggerMessage.Define(
15+
LogLevel.Critical,
16+
new EventId(2, nameof(ValidationRequestUnexpectedException)),
17+
Resources.ValidationUnexpectedErrorMessage);
18+
19+
private static readonly Action<ILogger, Exception?> _recaptchaResponseTokenMissing = LoggerMessage.Define(
20+
LogLevel.Warning,
21+
new EventId(3, nameof(RecaptchaResponseTokenMissing)),
22+
Resources.RecaptchaResponseTokenMissing);
23+
24+
private static readonly Action<ILogger, Exception?> _invalidResponseToken = LoggerMessage.Define(
25+
LogLevel.Information,
26+
new EventId(4, nameof(InvalidResponseToken)),
27+
Resources.InvalidResponseTokenMessage);
28+
29+
public static void ValidationRequestFailed(this ILogger logger)
30+
{
31+
_validationRequestFailed(logger, null);
32+
}
33+
34+
public static void ValidationRequestUnexpectedException(this ILogger logger, Exception exception)
35+
{
36+
_validationRequestUnexpectedException(logger, exception);
37+
}
38+
39+
public static void RecaptchaResponseTokenMissing(this ILogger logger)
40+
{
41+
_recaptchaResponseTokenMissing(logger, null);
42+
}
43+
44+
public static void InvalidResponseToken(this ILogger logger)
45+
{
46+
_invalidResponseToken(logger, null);
47+
}
1048
}
1149
}

src/ReCaptcha/Filters/ValidateRecaptchaFilter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.CompilerServices;
55
using System.Threading.Tasks;
66
using Griesoft.AspNetCore.ReCaptcha.Configuration;
7+
using Griesoft.AspNetCore.ReCaptcha.Extensions;
78
using Griesoft.AspNetCore.ReCaptcha.Localization;
89
using Griesoft.AspNetCore.ReCaptcha.Services;
910
using Microsoft.AspNetCore.Http;
@@ -43,7 +44,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
4344

4445
if (!TryGetRecaptchaToken(context.HttpContext.Request, out string? token))
4546
{
46-
_logger.LogWarning(Resources.RecaptchaResponseTokenMissing);
47+
_logger.RecaptchaResponseTokenMissing();
4748

4849
validationResponse = new ValidationResponse()
4950
{
@@ -77,7 +78,7 @@ private bool ShouldShortCircuit(ActionExecutingContext context, ValidationRespon
7778
{
7879
if (!response.Success || Action != response.Action)
7980
{
80-
_logger.LogInformation(Resources.InvalidResponseTokenMessage);
81+
_logger.InvalidResponseToken();
8182

8283
if (OnValidationFailedAction == ValidationFailedAction.BlockRequest)
8384
{

src/ReCaptcha/Services/RecaptchaService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.CompilerServices;
55
using System.Threading.Tasks;
66
using Griesoft.AspNetCore.ReCaptcha.Configuration;
7+
using Griesoft.AspNetCore.ReCaptcha.Extensions;
78
using Griesoft.AspNetCore.ReCaptcha.Localization;
89
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Options;
@@ -34,7 +35,7 @@ public async Task<ValidationResponse> ValidateRecaptchaResponse(string token, st
3435

3536
try
3637
{
37-
var response = await _httpClient.PostAsync($"?secret={_settings.SecretKey}&response={token}{(remoteIp != null ? $"&remoteip={remoteIp}" : "")}", null)
38+
var response = await _httpClient.PostAsync($"?secret={_settings.SecretKey}&response={token}{(remoteIp != null ? $"&remoteip={remoteIp}" : "")}", null!)
3839
.ConfigureAwait(true);
3940

4041
response.EnsureSuccessStatusCode();
@@ -53,7 +54,7 @@ await response.Content.ReadAsStringAsync()
5354
}
5455
catch (HttpRequestException)
5556
{
56-
_logger.LogWarning(Resources.RequestFailedErrorMessage);
57+
_logger.ValidationRequestFailed();
5758
return new ValidationResponse()
5859
{
5960
Success = false,
@@ -65,7 +66,7 @@ await response.Content.ReadAsStringAsync()
6566
}
6667
catch (Exception ex)
6768
{
68-
_logger.LogCritical(ex, Resources.ValidationUnexpectedErrorMessage);
69+
_logger.ValidationRequestUnexpectedException(ex);
6970
throw;
7071
}
7172
}

0 commit comments

Comments
 (0)