Skip to content

Commit 54b06c1

Browse files
authored
fix(logging): fix null object reference without logging configured (#70)
1 parent f2d6a70 commit 54b06c1

File tree

9 files changed

+57
-24
lines changed

9 files changed

+57
-24
lines changed

APIMatic.Core.Test/TestBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using APIMatic.Core.Test.MockTypes.Authentication;
77
using APIMatic.Core.Types;
88
using APIMatic.Core.Utilities.Logger.Configuration;
9+
using Microsoft.Extensions.Logging.Abstractions;
910
using RichardSzalay.MockHttp;
1011

1112
namespace APIMatic.Core.Test
@@ -55,7 +56,7 @@ protected enum MockServer { Server1, Server2 }
5556
("{language}", "my lang"),
5657
("{version}", "1.*.*")
5758
})
58-
.LoggingConfig(SdkLoggingConfiguration.Default())
59+
.LoggingConfig(null)
5960
.ApiCallback(ApiCallBack)
6061
.Build()
6162
);

APIMatic.Core.Test/Utilities/Logger/RequestTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public void LogRequest_NotConfigured_NoLogSent()
1919
var logger = new TestLogger();
2020
var loggingConfiguration = LoggerHelper.GetSdkLoggingConfiguration(logger: NullLogger.Instance);
2121
var sdkLogger = new SdkLogger(loggingConfiguration);
22-
var request = new CoreRequest(HttpMethod.Post, "https://example.com/api/resource", null, null, null);
22+
var request = new CoreRequest(HttpMethod.Post, "https://example.com/api/resource",
23+
new Dictionary<string, string>(), null, null);
2324

2425
// Act
2526
sdkLogger.LogRequest(request);

APIMatic.Core/ApiCall.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ApiCall<Request, Response, Context, ApiException, ReturnType, Respo
3636
private readonly Func<Response, ResponseType, ReturnType> returnTypeCreator;
3737
private Enum apiCallServer;
3838
private RequestBuilder requestBuilder;
39-
private readonly SdkLogger _sdkLogger;
39+
private readonly ISdkLogger _sdkLogger;
4040

4141
/// <summary>
4242
/// Creates a new instance of ApiCall
@@ -54,7 +54,7 @@ public ApiCall(GlobalConfiguration configuration, ICompatibilityFactory<Request,
5454
arraySerialization = serialization;
5555
this.returnTypeCreator = returnTypeCreator;
5656
responseHandler = new ResponseHandler<Request, Response, Context, ApiException, ResponseType>(compatibility, globalErrors);
57-
_sdkLogger = new SdkLogger(configuration.SdkLoggingConfiguration);
57+
_sdkLogger = SdkLoggerFactory.Create(configuration.SdkLoggingConfiguration);
5858
}
5959

6060
/// <summary>

APIMatic.Core/Utilities/Logger/Configuration/SdkLoggingConfiguration.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SdkLoggingConfiguration
2222
/// <summary>
2323
/// Gets or sets whether sensitive headers should be masked in logs.
2424
/// </summary>
25-
public bool MaskSensitiveHeaders { get; }
25+
public bool MaskSensitiveHeaders { get; }
2626

2727
/// <summary>
2828
/// Gets or sets the configuration for request logging.
@@ -34,11 +34,6 @@ public class SdkLoggingConfiguration
3434
/// </summary>
3535
public ResponseLoggingConfiguration ResponseLoggingConfiguration { get; }
3636

37-
/// <summary>
38-
/// Gets a value indicating whether the logging configuration is fully set up.
39-
/// </summary>
40-
public bool IsConfigured => Logger != NullLogger.Instance;
41-
4237
private SdkLoggingConfiguration(ILogger logger, LogLevel? logLevel, bool maskSensitiveHeaders,
4338
RequestLoggingConfiguration requestLoggingConfiguration,
4439
ResponseLoggingConfiguration responseLoggingConfiguration)
@@ -49,19 +44,12 @@ private SdkLoggingConfiguration(ILogger logger, LogLevel? logLevel, bool maskSen
4944
RequestLoggingConfiguration = requestLoggingConfiguration;
5045
ResponseLoggingConfiguration = responseLoggingConfiguration;
5146
}
52-
53-
54-
public static SdkLoggingConfiguration Default() =>
55-
new SdkLoggingConfiguration(NullLogger.Instance, null, true,
56-
RequestLoggingConfiguration.Default(),
57-
ResponseLoggingConfiguration.Default());
5847

5948
public static SdkLoggingConfiguration Console() =>
6049
new SdkLoggingConfiguration(ConsoleLogger.Instance, null, true,
6150
RequestLoggingConfiguration.Default(),
6251
ResponseLoggingConfiguration.Default());
6352

64-
6553
public static SdkLoggingConfiguration Builder(ILogger logger, LogLevel? logLevel, bool maskSensitiveHeaders,
6654
RequestLoggingConfiguration requestLoggingConfiguration,
6755
ResponseLoggingConfiguration responseLoggingConfiguration)

APIMatic.Core/Utilities/Logger/ConsoleLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public class ConsoleLogger : ILogger
1414
/// Returns the shared instance of <see cref="T:Microsoft.Extensions.Logging.Abstractions.NullLogger" />.
1515
/// </summary>
1616
public static ConsoleLogger Instance { get; } = new ConsoleLogger();
17-
18-
17+
1918
/// <summary>
2019
/// Initializes a new instance of the <see cref="T:Microsoft.Extensions.Logging.Abstractions.NullLogger" /> class.
2120
/// </summary>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using APIMatic.Core.Types.Sdk;
2+
3+
namespace APIMatic.Core.Utilities.Logger
4+
{
5+
internal interface ISdkLogger
6+
{
7+
/// <summary>
8+
/// Logs the details of a request.
9+
/// </summary>
10+
/// <param name="request">The request to be logged.</param>
11+
void LogRequest(CoreRequest request);
12+
13+
/// <summary>
14+
/// Logs the details of a response.
15+
/// </summary>
16+
/// <param name="response">The response to be logged.</param>
17+
void LogResponse(CoreResponse response);
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using APIMatic.Core.Types.Sdk;
2+
3+
namespace APIMatic.Core.Utilities.Logger
4+
{
5+
internal class NullSdkLogger : ISdkLogger
6+
{
7+
public void LogRequest(CoreRequest request)
8+
{
9+
// Method intentionally left empty.
10+
}
11+
12+
public void LogResponse(CoreResponse response)
13+
{
14+
// Method intentionally left empty.
15+
}
16+
}
17+
}

APIMatic.Core/Utilities/Logger/SdkLogger.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ namespace APIMatic.Core.Utilities.Logger
99
/// <summary>
1010
/// Provides logging functionality for SDK operations.
1111
/// </summary>
12-
internal class SdkLogger
12+
internal class SdkLogger : ISdkLogger
1313
{
1414
private readonly ILogger _logger;
1515
private readonly Func<LogLevel, LogLevel> _getOverridenLogLevel;
1616
private readonly RequestLoggingConfiguration _requestConfiguration;
1717
private readonly ResponseLoggingConfiguration _responseConfiguration;
18-
private readonly bool _isConfigured;
1918
private readonly bool _maskSensitiveHeaders;
2019

2120
/// <summary>
@@ -28,7 +27,6 @@ public SdkLogger(SdkLoggingConfiguration loggingConfiguration)
2827
_getOverridenLogLevel = level => loggingConfiguration.LogLevel.GetValueOrDefault(level);
2928
_requestConfiguration = loggingConfiguration.RequestLoggingConfiguration;
3029
_responseConfiguration = loggingConfiguration.ResponseLoggingConfiguration;
31-
_isConfigured = loggingConfiguration.IsConfigured;
3230
_maskSensitiveHeaders = loggingConfiguration.MaskSensitiveHeaders;
3331
}
3432

@@ -38,7 +36,6 @@ public SdkLogger(SdkLoggingConfiguration loggingConfiguration)
3836
/// <param name="request">The request to be logged.</param>
3937
public void LogRequest(CoreRequest request)
4038
{
41-
if (!_isConfigured) return;
4239
var localLogLevel = _getOverridenLogLevel(LogLevel.Information);
4340
var contentTypeHeader = request.Headers.GetContentType();
4441
var url = _requestConfiguration.IncludeQueryInPath ? request.QueryUrl : ParseQueryPath(request.QueryUrl);
@@ -66,7 +63,6 @@ public void LogRequest(CoreRequest request)
6663
/// <param name="response">The response to be logged.</param>
6764
public void LogResponse(CoreResponse response)
6865
{
69-
if (!_isConfigured) return;
7066
var localLogLevel = _getOverridenLogLevel(LogLevel.Information);
7167
var contentTypeHeader = response.Headers.GetContentType();
7268
var contentLengthHeader = response.Headers.GetContentLength();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using APIMatic.Core.Utilities.Logger.Configuration;
2+
3+
namespace APIMatic.Core.Utilities.Logger
4+
{
5+
internal static class SdkLoggerFactory
6+
{
7+
public static ISdkLogger Create(SdkLoggingConfiguration sdkLoggingConfiguration) =>
8+
sdkLoggingConfiguration == null
9+
? (ISdkLogger)new NullSdkLogger()
10+
: new SdkLogger(sdkLoggingConfiguration);
11+
}
12+
}

0 commit comments

Comments
 (0)