Update ElasticConfiguration URIs and IndexFormats in appsettings for Leave and Insignia
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m12s

This commit is contained in:
Suphonchai Phoonsawat 2026-01-22 12:24:27 +07:00
parent d945deae4f
commit 2b737de23b
3 changed files with 50 additions and 30 deletions

View file

@ -154,25 +154,37 @@ namespace BMA.EHR.Domain.Middlewares
memoryStream.Seek(0, SeekOrigin.Begin);
}
// ทำ logging แบบ fire-and-forget เพื่อไม่ block response
_ = Task.Run(async () =>
// เก็บข้อมูลที่จำเป็นจาก HttpContext ก่อนที่มันจะถูก dispose
var logData = new
{
try
{
await LogRequestAsync(context, _elasticClient!, startTime, stopwatch, pf, keycloakId, requestBodyJson, responseBodyForLogging, caughtException);
}
catch (Exception ex)
{
Console.WriteLine($"Background logging error: {ex.Message}");
}
});
// เขียนข้อมูลกลับไปยัง original Response body
RemoteIpAddress = context.Connection.RemoteIpAddress?.ToString(),
HostValue = context.Request.Host.Value,
Method = context.Request.Method,
Path = context.Request.Path.ToString(),
QueryString = context.Request.QueryString.ToString(),
StatusCode = context.Response.StatusCode,
ContentType = context.Response.ContentType ?? ""
};
// เขียนข้อมูลกลับไปยัง original Response body ก่อน
if (memoryStream.Length > 0)
{
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(originalBodyStream);
}
// ทำ logging แบบ await
Console.WriteLine("[DEBUG] Starting logging...");
try
{
await LogRequestAsync(_elasticClient!, startTime, stopwatch, pf, keycloakId, requestBodyJson, responseBodyForLogging, caughtException, logData);
Console.WriteLine("[DEBUG] Logging completed successfully");
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Logging error: {ex.Message}");
Console.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
}
}
}
@ -402,15 +414,16 @@ namespace BMA.EHR.Domain.Middlewares
}
}
private async Task LogRequestAsync(HttpContext context, ElasticClient client, DateTime startTime, Stopwatch stopwatch,
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, string? responseBodyForLogging, Exception? caughtException)
private async Task LogRequestAsync(ElasticClient client, DateTime startTime, Stopwatch stopwatch,
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, string? responseBodyForLogging, Exception? caughtException, dynamic contextData)
{
Console.WriteLine("[DEBUG] LogRequestAsync called");
try
{
var processTime = stopwatch.ElapsedMilliseconds;
var endTime = DateTime.UtcNow;
var statusCode = caughtException != null ? (int)HttpStatusCode.InternalServerError : context.Response.StatusCode;
var statusCode = caughtException != null ? (int)HttpStatusCode.InternalServerError : (int)contextData.StatusCode;
var logType = caughtException != null ? "error" : statusCode switch
{
@ -425,12 +438,11 @@ namespace BMA.EHR.Domain.Middlewares
// ใช้ response body ที่ส่งมาจากการอ่านก่อนหน้า
if (!string.IsNullOrEmpty(responseBodyForLogging))
{
var contentType = context.Response.ContentType ?? "";
var contentType = (string)contextData.ContentType;
var isFileResponse = !contentType.StartsWith("application/json") && !contentType.StartsWith("text/html") && (
contentType.StartsWith("application/") ||
contentType.StartsWith("image/") ||
contentType.StartsWith("audio/") ||
context.Response.Headers.ContainsKey("Content-Disposition")
contentType.StartsWith("audio/")
);
if (isFileResponse)
@ -470,15 +482,15 @@ namespace BMA.EHR.Domain.Middlewares
var logData = new
{
logType = logType,
ip = context.Connection.RemoteIpAddress?.ToString(),
rootId = pf?.RootDnaId,
ip = (string)contextData.RemoteIpAddress,
rootId = pf?.RootId,
systemName = SystemName,
startTimeStamp = startTime.ToString("o"),
endTimeStamp = endTime.ToString("o"),
processTime = processTime,
host = context.Request.Host.Value,
method = context.Request.Method,
endpoint = context.Request.Path + context.Request.QueryString,
host = (string)contextData.HostValue,
method = (string)contextData.Method,
endpoint = (string)contextData.Path + (string)contextData.QueryString,
responseCode = statusCode == 304 ? "200" : statusCode.ToString(),
responseDescription = message,
input = requestBodyJson,
@ -489,11 +501,19 @@ namespace BMA.EHR.Domain.Middlewares
exception = caughtException?.ToString()
};
await client.IndexDocumentAsync(logData);
Console.WriteLine($"[DEBUG] Sending log to Elasticsearch: {logType} - {(string)contextData.Method} {(string)contextData.Path}");
var response = await client.IndexDocumentAsync(logData);
Console.WriteLine($"[DEBUG] Elasticsearch response: IsValid={response.IsValid}, Index={response.Index}");
if (!response.IsValid)
{
Console.WriteLine($"[ERROR] Elasticsearch error: {response.OriginalException?.Message ?? response.ServerError?.ToString()}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error logging request: {ex.Message}");
Console.WriteLine($"[ERROR] Error logging request: {ex.Message}");
Console.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
}
}

View file

@ -9,8 +9,8 @@
}
},
"ElasticConfiguration": {
"Uri": "http://192.168.1.40:9200",
"IndexFormat": "bma-ehr-log-index",
"Uri": "http://192.168.1.63:9200",
"IndexFormat": "hrms-log-index",
"SystemName": "insignia"
},
"AllowedHosts": "*",

View file

@ -9,8 +9,8 @@
}
},
"ElasticConfiguration": {
"Uri": "http://192.168.1.40:9200",
"IndexFormat": "bma-ehr-log-index",
"Uri": "http://192.168.1.63:9200",
"IndexFormat": "hrms-log-index",
"SystemName": "leave"
},
"AllowedHosts": "*",