Skip to content

Commit d2ac13f

Browse files
committed
Some logging improvements
1 parent 71f4a83 commit d2ac13f

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ When you install the package, it should be added to your `.csproj`. Alternativel
4040

4141
```xml
4242
<ItemGroup>
43-
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="6.0.0" />
43+
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="6.0.1" />
4444
</ItemGroup>
4545
```
4646

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>6.0.0</Version>
3+
<Version>6.0.1</Version>
44
<PackageReleaseNotes>Breaking Change: Drop support for netstandard and .net framework. Code refactoring. Dependency update. Nullable enable. Events are initialized once in handler only or configuration.</PackageReleaseNotes>
55
</PropertyGroup>
66

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationHandler.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace ZNetCS.AspNetCore.Authentication.Basic;
1111

12-
#region Usings
12+
#region Usings
1313

1414
using System;
1515
using System.Diagnostics.CodeAnalysis;
@@ -53,6 +53,15 @@ public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticat
5353

5454
#endregion
5555

56+
#region Fields
57+
58+
/// <summary>
59+
/// The logger.
60+
/// </summary>
61+
private readonly ILogger logger;
62+
63+
#endregion
64+
5665
#region Constructors and Destructors
5766

5867
/// <summary>
@@ -61,19 +70,18 @@ public class BasicAuthenticationHandler : AuthenticationHandler<BasicAuthenticat
6170
/// <param name="options">
6271
/// The options.
6372
/// </param>
64-
/// <param name="logger">
65-
/// The logger.
73+
/// <param name="loggerFactory">
74+
/// The logger factory.
6675
/// </param>
6776
/// <param name="encoder">
6877
/// The encoder.
6978
/// </param>
7079
/// <param name="clock">
7180
/// The clock.
7281
/// </param>
73-
public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
74-
: base(options, logger, encoder, clock)
75-
{
76-
}
82+
public BasicAuthenticationHandler(IOptionsMonitor<BasicAuthenticationOptions> options, ILoggerFactory loggerFactory, UrlEncoder encoder, ISystemClock clock)
83+
: base(options, loggerFactory, encoder, clock)
84+
=> this.logger = loggerFactory.CreateLogger<BasicAuthenticationHandler>();
7785

7886
#endregion
7987

@@ -108,7 +116,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
108116
// there is no authorization header so there is nothing to do by this middleware
109117
if (authorizationHeaderValues.Length == 0)
110118
{
111-
this.Logger.LogDebug("'Authorization' header is not present in the request.");
119+
this.logger.AuthorizationHeaderNotPresent();
112120
return AuthenticateResult.NoResult();
113121
}
114122

@@ -117,7 +125,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
117125
// Authorization header is not 'Basic' so there is nothing to do by this middleware
118126
if (string.IsNullOrEmpty(basicAuthorizationHeader))
119127
{
120-
this.Logger.LogDebug("'Authorization' header is not in 'Basic' scheme in the request.");
128+
this.logger.AuthorizationHeaderNotBasic();
121129
return AuthenticateResult.NoResult();
122130
}
123131

@@ -170,7 +178,8 @@ protected override Task HandleChallengeAsync(AuthenticationProperties context)
170178
return Task.CompletedTask;
171179
}
172180

173-
if (this.Options.AjaxRequestOptions.SuppressWwwAuthenticateHeader && this.Request.Headers.TryGetValue(this.Options.AjaxRequestOptions.HeaderName, out StringValues value))
181+
if (this.Options.AjaxRequestOptions.SuppressWwwAuthenticateHeader &&
182+
this.Request.Headers.TryGetValue(this.Options.AjaxRequestOptions.HeaderName, out StringValues value))
174183
{
175184
if (value == this.Options.AjaxRequestOptions.HeaderValue)
176185
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="LoggerExtensions.cs" company="Marcin Smółka">
3+
// Copyright (c) Marcin Smółka. All rights reserved.
4+
// </copyright>
5+
// -----------------------------------------------------------------------
6+
7+
namespace ZNetCS.AspNetCore.Authentication.Basic;
8+
9+
#region Usings
10+
11+
using System;
12+
using System.Diagnostics.CodeAnalysis;
13+
14+
using Microsoft.Extensions.Logging;
15+
16+
#endregion
17+
18+
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "Internal")]
19+
internal static class LoggerExtensions
20+
{
21+
#region Static Fields
22+
23+
private static readonly Action<ILogger, Exception?> AuthorizationHeaderNotBasicDefinition =
24+
LoggerMessage.Define(LogLevel.Debug, new EventId(100, "AuthorizationHeaderNotPresent"), "'Authorization' header is not in 'Basic' scheme in the request");
25+
26+
private static readonly Action<ILogger, Exception?> AuthorizationHeaderNotPresentDefinition =
27+
LoggerMessage.Define(LogLevel.Debug, new EventId(100, "AuthorizationHeaderNotPresent"), "'Authorization' header is not present in the request");
28+
29+
#endregion
30+
31+
#region Public Methods
32+
33+
public static void AuthorizationHeaderNotBasic(this ILogger logger)
34+
{
35+
AuthorizationHeaderNotBasicDefinition(logger, null);
36+
}
37+
38+
public static void AuthorizationHeaderNotPresent(this ILogger logger)
39+
{
40+
AuthorizationHeaderNotPresentDefinition(logger, null);
41+
}
42+
43+
#endregion
44+
}

0 commit comments

Comments
 (0)