1616
1717using System . Net ;
1818using System . Text ;
19+ using Microsoft . Extensions . Logging ;
1920
2021namespace 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
0 commit comments