Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 28 additions & 88 deletions Common/Http/SatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ protected async Task<SatResponse> SendRequestAsync(string url, string action, st

try
{
// Log request
if (IsDebugEnabled)
LogRequest(request, url, action, payload);
LogRequest(request, url, action, payload);

using var response =
await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken);
var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);

// Log response
if (IsDebugEnabled)
LogResponse(response, responseContent, url, action);
LogResponse(response, responseContent, url, action);

return new SatResponse
{
Expand All @@ -102,8 +98,7 @@ protected async Task<SatResponse> SendRequestAsync(string url, string action, st
}
catch (Exception ex)
{
if (IsDebugEnabled)
LogError(ex, url, action);
LogError(ex, url, action);

_logger.LogError(ex, "Error sending SOAP request to {Url} with action {Action}", url, action);

Expand All @@ -128,35 +123,12 @@ private void LogRequest(HttpRequestMessage request, string url, string soapActio
{
try
{
var sb = new StringBuilder();
sb.AppendLine("=== SOAP REQUEST ===");
sb.AppendLine($"HTTP Method: {request.Method}");
sb.AppendLine($"URL: {url}");
sb.AppendLine($"SOAP Action: {soapAction}");
sb.AppendLine($"Timestamp: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");

// Headers
sb.AppendLine("Headers:");
foreach (var header in request.Headers)
{
sb.AppendLine($" {header.Key}: {string.Join(", ", header.Value)}");
}

// Content headers
if (request.Content?.Headers != null)
{
sb.AppendLine("Content Headers:");
foreach (var header in request.Content.Headers)
{
sb.AppendLine($" {header.Key}: {string.Join(", ", header.Value)}");
}
}

sb.AppendLine("Raw Request Body:");
sb.AppendLine(payload);
sb.AppendLine("=== END REQUEST ===");

_logger.LogDebug(sb.ToString());
var serverDateTime = DateTime.Now;
var serverTimeZone = TimeZoneInfo.Local;
var timestamp = $"{serverDateTime:yyyy-MM-dd HH:mm:ss} {serverTimeZone.Id}";

_logger.LogInformation("SOAP Request - Endpoint: {Url}, HTTP Method: {Method}, Timestamp: {Timestamp}, RawRequest: {RawRequest}",
url, request.Method, timestamp, payload);
}
catch (Exception ex)
{
Expand All @@ -176,72 +148,40 @@ private void LogResponse(HttpResponseMessage response, string responseContent, s
{
try
{
var sb = new StringBuilder();
sb.AppendLine("=== SOAP RESPONSE ===");
sb.AppendLine($"URL: {url}");
sb.AppendLine($"SOAP Action: {soapAction}");
sb.AppendLine($"HTTP Status Code: {(int)response.StatusCode} {response.StatusCode}");
sb.AppendLine($"Reason Phrase: {response.ReasonPhrase}");
sb.AppendLine($"Success: {response.IsSuccessStatusCode}");
sb.AppendLine($"Timestamp: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");

// Response headers
sb.AppendLine("Response Headers:");
foreach (var header in response.Headers)
var serverDateTime = DateTime.Now;
var serverTimeZone = TimeZoneInfo.Local;
var timestamp = $"{serverDateTime:yyyy-MM-dd HH:mm:ss} {serverTimeZone.Id}";
var statusCode = (int)response.StatusCode;
var rawResponse = string.IsNullOrEmpty(responseContent) ? "[Empty Response]" : responseContent;

if (response.IsSuccessStatusCode)
{
sb.AppendLine($" {header.Key}: {string.Join(", ", header.Value)}");
_logger.LogInformation("SOAP Response - Endpoint: {Url}, HTTP Status Code: {StatusCode}, Timestamp: {Timestamp}, RawResponse: {RawResponse}",
url, statusCode, timestamp, rawResponse);
}

// Content headers
if (response.Content?.Headers != null)
else
{
sb.AppendLine("Content Headers:");
foreach (var header in response.Content.Headers)
{
sb.AppendLine($" {header.Key}: {string.Join(", ", header.Value)}");
}
_logger.LogError("SOAP Response - Endpoint: {Url}, HTTP Status Code: {StatusCode}, Timestamp: {Timestamp}, RawResponse: {RawResponse}",
url, statusCode, timestamp, rawResponse);
}

sb.AppendLine("Raw Response Body:");
sb.AppendLine(string.IsNullOrEmpty(responseContent) ? "[Empty Response]" : responseContent);
sb.AppendLine("=== END RESPONSE ===");

_logger.LogDebug(sb.ToString());
}
catch (Exception ex)
{
_logger.LogError(ex, "Error logging response");
}
}

/// <summary>
/// Logs error details when an exception occurs
/// </summary>
/// <param name="ex">Exception that occurred</param>
/// <param name="url">Request URL</param>
/// <param name="soapAction">SOAP action</param>
private void LogError(Exception ex, string url, string soapAction)
{
try
{
var sb = new StringBuilder();
sb.AppendLine("=== SOAP ERROR ===");
sb.AppendLine($"URL: {url}");
sb.AppendLine($"SOAP Action: {soapAction}");
sb.AppendLine($"Timestamp: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");
sb.AppendLine($"Exception Type: {ex.GetType().Name}");
sb.AppendLine($"Message: {ex.Message}");

if (ex.InnerException != null)
{
sb.AppendLine($"Inner Exception: {ex.InnerException.Message}");
}

sb.AppendLine("Stack Trace:");
sb.AppendLine(ex.StackTrace);
sb.AppendLine("=== END ERROR ===");

_logger.LogError(sb.ToString());
var serverDateTime = DateTime.Now;
var serverTimeZone = TimeZoneInfo.Local;
var timestamp = $"{serverDateTime:yyyy-MM-dd HH:mm:ss} {serverTimeZone.Id}";
var innerExceptionMessage = ex.InnerException != null ? ex.InnerException.Message : null;

_logger.LogError(ex, "SOAP Error - Endpoint: {Url}, SOAP Action: {SoapAction}, Timestamp: {Timestamp}, Exception Type: {ExceptionType}, Message: {Message}, Inner Exception: {InnerException}",
url, soapAction, timestamp, ex.GetType().Name, ex.Message, innerExceptionMessage);
}
catch (Exception logEx)
{
Expand Down
2 changes: 1 addition & 1 deletion XmlDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>5.0.8</Version>
<Version>5.0.10</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<PackageVersion>$(Version)</PackageVersion>
Expand Down