Skip to content

Commit 2888f51

Browse files
committed
logging impprovements added
1 parent 0491034 commit 2888f51

15 files changed

+183
-50
lines changed

Auth/AuthResponseService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
using Fiscalapi.XmlDownloader.Common;
2121
using Fiscalapi.XmlDownloader.Common.Enums;
2222
using Fiscalapi.XmlDownloader.Common.Http;
23+
using Microsoft.Extensions.Logging;
2324

2425
namespace Fiscalapi.XmlDownloader.Auth;
2526

2627
public static class AuthResponseService
2728
{
28-
public static AuthResponse Build(SatResponse satResponse, ICredential credential)
29+
public static AuthResponse Build(SatResponse satResponse, ICredential credential, ILogger logger)
2930
{
3031
/*
3132
*<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

Auth/AuthService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,30 @@ namespace Fiscalapi.XmlDownloader.Auth;
2828
/// </summary>
2929
public class AuthService : SatService, IAuthService
3030
{
31+
/// <summary>
32+
/// Constructor for dependency injection scenarios
33+
/// </summary>
34+
/// <param name="httpClient">HttpClient instance</param>
35+
/// <param name="logger">Logger instance</param>
36+
public AuthService(HttpClient httpClient, ILogger<AuthService> logger)
37+
: base(httpClient, logger)
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Constructor for direct instantiation scenarios
43+
/// </summary>
44+
/// <param name="logger">Logger instance</param>
45+
public AuthService(ILogger<AuthService> logger)
46+
: base(logger)
47+
{
48+
}
49+
3150
/// <summary>
3251
/// Authenticates with SAT using the provided credential and returns the authentication token
3352
/// </summary>
3453
public async Task<AuthResponse> AuthenticateAsync(ICredential credential,
35-
CancellationToken cancellationToken = default, ILogger? logger = null)
54+
ILogger logger, CancellationToken cancellationToken = default)
3655
{
3756
// Generate Sat XML security token ID
3857
var uuid = CreateSecurityToken();
@@ -53,7 +72,7 @@ public async Task<AuthResponse> AuthenticateAsync(ICredential credential,
5372
cancellationToken: cancellationToken);
5473

5574
// Map response
56-
var authResponse = AuthResponseService.Build(satResponse, credential);
75+
var authResponse = AuthResponseService.Build(satResponse, credential, logger);
5776

5877
return authResponse;
5978
}

Auth/IAuthService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ public interface IAuthService
3232
/// <param name="cancellationToken">CancellationToken</param>
3333
/// <param name="logger">Logger</param>
3434
/// <returns></returns>
35-
Task<AuthResponse> AuthenticateAsync(ICredential credential, CancellationToken cancellationToken = default,
36-
ILogger? logger = null);
35+
Task<AuthResponse> AuthenticateAsync(ICredential credential, ILogger logger, CancellationToken cancellationToken = default);
3736
}

Common/Http/SatService.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System.Net;
1818
using System.Text;
19+
using Microsoft.Extensions.Logging;
1920

2021
namespace Fiscalapi.XmlDownloader.Common.Http;
2122

@@ -26,25 +27,28 @@ public abstract class SatService : IDisposable
2627
{
2728
private readonly HttpClient _httpClient;
2829
private readonly bool _ownsHttpClient;
30+
private readonly ILogger _logger;
2931
private bool _disposed;
3032

3133
protected bool IsDebugEnabled { get; set; }
3234

3335
/// <summary>
3436
/// Constructor for dependency injection scenarios
3537
/// </summary>
36-
protected SatService(HttpClient httpClient)
38+
protected SatService(HttpClient httpClient, ILogger logger)
3739
{
3840
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
41+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3942
_ownsHttpClient = false;
4043
}
4144

4245
/// <summary>
4346
/// Constructor for direct instantiation scenarios
4447
/// </summary>
45-
protected SatService()
48+
protected SatService(ILogger logger)
4649
{
4750
_httpClient = new HttpClient();
51+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
4852
_ownsHttpClient = true;
4953
}
5054

@@ -100,6 +104,8 @@ protected async Task<SatResponse> SendRequestAsync(string url, string action, st
100104
{
101105
if (IsDebugEnabled)
102106
LogError(ex, url, action);
107+
108+
_logger.LogError(ex, "Error sending SOAP request to {Url} with action {Action}", url, action);
103109
104110
return new SatResponse
105111
{
@@ -118,7 +124,7 @@ protected async Task<SatResponse> SendRequestAsync(string url, string action, st
118124
/// <param name="url">Request URL</param>
119125
/// <param name="soapAction">SOAP action header</param>
120126
/// <param name="payload">Request payload</param>
121-
private static void LogRequest(HttpRequestMessage request, string url, string soapAction, string payload)
127+
private void LogRequest(HttpRequestMessage request, string url, string soapAction, string payload)
122128
{
123129
try
124130
{
@@ -150,11 +156,11 @@ private static void LogRequest(HttpRequestMessage request, string url, string so
150156
sb.AppendLine(payload);
151157
sb.AppendLine("=== END REQUEST ===");
152158

153-
Console.WriteLine(sb.ToString());
159+
_logger.LogDebug(sb.ToString());
154160
}
155161
catch (Exception ex)
156162
{
157-
Console.WriteLine($"Error logging request: {ex.Message}");
163+
_logger.LogError(ex, "Error logging request");
158164
}
159165
}
160166

@@ -165,7 +171,7 @@ private static void LogRequest(HttpRequestMessage request, string url, string so
165171
/// <param name="responseContent">Response content</param>
166172
/// <param name="url">Original request URL</param>
167173
/// <param name="soapAction">Original SOAP action</param>
168-
private static void LogResponse(HttpResponseMessage response, string responseContent, string url,
174+
private void LogResponse(HttpResponseMessage response, string responseContent, string url,
169175
string soapAction)
170176
{
171177
try
@@ -200,11 +206,11 @@ private static void LogResponse(HttpResponseMessage response, string responseCon
200206
sb.AppendLine(string.IsNullOrEmpty(responseContent) ? "[Empty Response]" : responseContent);
201207
sb.AppendLine("=== END RESPONSE ===");
202208

203-
Console.WriteLine(sb.ToString());
209+
_logger.LogDebug(sb.ToString());
204210
}
205211
catch (Exception ex)
206212
{
207-
Console.WriteLine($"Error logging response: {ex.Message}");
213+
_logger.LogError(ex, "Error logging response");
208214
}
209215
}
210216

@@ -214,7 +220,7 @@ private static void LogResponse(HttpResponseMessage response, string responseCon
214220
/// <param name="ex">Exception that occurred</param>
215221
/// <param name="url">Request URL</param>
216222
/// <param name="soapAction">SOAP action</param>
217-
private static void LogError(Exception ex, string url, string soapAction)
223+
private void LogError(Exception ex, string url, string soapAction)
218224
{
219225
try
220226
{
@@ -235,11 +241,11 @@ private static void LogError(Exception ex, string url, string soapAction)
235241
sb.AppendLine(ex.StackTrace);
236242
sb.AppendLine("=== END ERROR ===");
237243

238-
Console.WriteLine(sb.ToString());
244+
_logger.LogError(sb.ToString());
239245
}
240246
catch (Exception logEx)
241247
{
242-
Console.WriteLine($"Error logging exception: {logEx.Message}");
248+
_logger.LogError(logEx, "Error logging exception");
243249
}
244250
}
245251

Download/DownloadResponseService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
using Fiscalapi.XmlDownloader.Common.Http;
2020
using Fiscalapi.XmlDownloader.Download.Models;
2121
using Fiscalapi.XmlDownloader.Download.Models.Sat;
22+
using Microsoft.Extensions.Logging;
2223

2324
namespace Fiscalapi.XmlDownloader.Download;
2425

2526
public static class DownloadResponseService
2627
{
27-
public static DownloadResponse Build(SatResponse satResponse)
28+
public static DownloadResponse Build(SatResponse satResponse, ILogger logger)
2829
{
2930
if (satResponse.IsSuccessStatusCode)
3031
{

Download/DownloadService.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ namespace Fiscalapi.XmlDownloader.Download;
2929
/// </summary>
3030
public class DownloadService : SatService, IDownloadService
3131
{
32+
/// <summary>
33+
/// Constructor for dependency injection scenarios
34+
/// </summary>
35+
/// <param name="httpClient">HttpClient instance</param>
36+
/// <param name="logger">Logger instance</param>
37+
public DownloadService(HttpClient httpClient, ILogger<DownloadService> logger)
38+
: base(httpClient, logger)
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Constructor for direct instantiation scenarios
44+
/// </summary>
45+
/// <param name="logger">Logger instance</param>
46+
public DownloadService(ILogger<DownloadService> logger)
47+
: base(logger)
48+
{
49+
}
50+
3251
/// <summary>
3352
/// Downloads a package from SAT using the provided credential, authentication token and package ID.s
3453
/// </summary>
@@ -39,7 +58,7 @@ public class DownloadService : SatService, IDownloadService
3958
/// <param name="logger">Logger</param>
4059
/// <returns>DownloadResponse</returns>
4160
public async Task<DownloadResponse> DownloadAsync(ICredential credential, Token authToken, string packageId,
42-
CancellationToken cancellationToken = default, ILogger? logger = null)
61+
ILogger logger, CancellationToken cancellationToken = default)
4362
{
4463
var toDigest = CreateDigest(packageId, credential.Certificate.Rfc);
4564
var signature = CreateSignature(credential, toDigest);
@@ -52,7 +71,7 @@ public async Task<DownloadResponse> DownloadAsync(ICredential credential, Token
5271
token: authToken.Value,
5372
cancellationToken: cancellationToken);
5473

55-
var downloadResponse = DownloadResponseService.Build(satResponse);
74+
var downloadResponse = DownloadResponseService.Build(satResponse, logger);
5675

5776
return downloadResponse;
5877
}

Download/IDownloadService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ public interface IDownloadService
3636
/// <param name="logger">Logger</param>
3737
/// <returns>DownloadResponse</returns>
3838
Task<DownloadResponse> DownloadAsync(ICredential credential, Token authToken, string packageId,
39-
CancellationToken cancellationToken = default, ILogger? logger = null);
39+
ILogger logger, CancellationToken cancellationToken = default);
4040
}

DownloaderExtensions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,13 @@ public static TEnum ToEnumElement<TEnum>(this string code) where TEnum : struct,
170170
/// <returns>The service collection for chaining</returns>
171171
public static IServiceCollection AddXmlDownloader(this IServiceCollection services)
172172
{
173-
// Register the services
174-
services.AddScoped<IAuthService, AuthService>();
175-
services.AddScoped<IQueryService, QueryService>();
176-
services.AddScoped<IVerifyService, VerifyService>();
177-
services.AddScoped<IDownloadService, DownloadService>();
173+
// Register HttpClient factory for SAT services
174+
services.AddHttpClient<IAuthService, AuthService>();
175+
services.AddHttpClient<IQueryService, QueryService>();
176+
services.AddHttpClient<IVerifyService, VerifyService>();
177+
services.AddHttpClient<IDownloadService, DownloadService>();
178+
179+
// Register non-HTTP services as scoped
178180
services.AddScoped<IFileStorageService, FileStorageService>();
179181
services.AddScoped<IXmlDownloaderService, XmlDownloaderService>();
180182

Query/IQueryService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ public interface IQueryService
3636
/// <param name="logger">Logger</param>
3737
/// <returns>QueryResponse</returns>
3838
Task<QueryResponse> CreateAsync(ICredential credential, Token authToken,
39-
QueryParameters parameters, CancellationToken cancellationToken, ILogger? logger = null);
39+
QueryParameters parameters, ILogger logger, CancellationToken cancellationToken = default);
4040
}

Query/QueryResponseService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
using Fiscalapi.XmlDownloader.Common.Http;
2020
using Fiscalapi.XmlDownloader.Query.Models;
2121
using Fiscalapi.XmlDownloader.Query.Models.Sat;
22+
using Microsoft.Extensions.Logging;
2223

2324
namespace Fiscalapi.XmlDownloader.Query;
2425

2526
public static class QueryResponseService
2627
{
27-
public static QueryResponse Build(SatResponse satResponse)
28+
public static QueryResponse Build(SatResponse satResponse, ILogger logger)
2829
{
2930
if (satResponse.IsSuccessStatusCode)
3031
{

0 commit comments

Comments
 (0)