Skip to content

Commit f333fcb

Browse files
committed
fix(logging): fix null object reference without logging configured
1 parent f2d6a70 commit f333fcb

File tree

8 files changed

+54
-17
lines changed

8 files changed

+54
-17
lines changed

APIMatic.Core.Test/TestBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ protected enum MockServer { Server1, Server2 }
5555
("{language}", "my lang"),
5656
("{version}", "1.*.*")
5757
})
58-
.LoggingConfig(SdkLoggingConfiguration.Default())
5958
.ApiCallback(ApiCallBack)
6059
.Build()
6160
);

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 & 8 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.
@@ -49,19 +49,12 @@ private SdkLoggingConfiguration(ILogger logger, LogLevel? logLevel, bool maskSen
4949
RequestLoggingConfiguration = requestLoggingConfiguration;
5050
ResponseLoggingConfiguration = responseLoggingConfiguration;
5151
}
52-
53-
54-
public static SdkLoggingConfiguration Default() =>
55-
new SdkLoggingConfiguration(NullLogger.Instance, null, true,
56-
RequestLoggingConfiguration.Default(),
57-
ResponseLoggingConfiguration.Default());
5852

5953
public static SdkLoggingConfiguration Console() =>
6054
new SdkLoggingConfiguration(ConsoleLogger.Instance, null, true,
6155
RequestLoggingConfiguration.Default(),
6256
ResponseLoggingConfiguration.Default());
6357

64-
6558
public static SdkLoggingConfiguration Builder(ILogger logger, LogLevel? logLevel, bool maskSensitiveHeaders,
6659
RequestLoggingConfiguration requestLoggingConfiguration,
6760
ResponseLoggingConfiguration responseLoggingConfiguration)
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)