Update ElasticConfiguration URIs and IndexFormats in appsettings for Leave and Insignia
This commit is contained in:
parent
d945deae4f
commit
d6285f5742
3 changed files with 50 additions and 30 deletions
|
|
@ -154,25 +154,37 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ทำ logging แบบ fire-and-forget เพื่อไม่ block response
|
// เก็บข้อมูลที่จำเป็นจาก HttpContext ก่อนที่มันจะถูก dispose
|
||||||
_ = Task.Run(async () =>
|
var logData = new
|
||||||
{
|
{
|
||||||
try
|
RemoteIpAddress = context.Connection.RemoteIpAddress?.ToString(),
|
||||||
{
|
HostValue = context.Request.Host.Value,
|
||||||
await LogRequestAsync(context, _elasticClient!, startTime, stopwatch, pf, keycloakId, requestBodyJson, responseBodyForLogging, caughtException);
|
Method = context.Request.Method,
|
||||||
}
|
Path = context.Request.Path.ToString(),
|
||||||
catch (Exception ex)
|
QueryString = context.Request.QueryString.ToString(),
|
||||||
{
|
StatusCode = context.Response.StatusCode,
|
||||||
Console.WriteLine($"Background logging error: {ex.Message}");
|
ContentType = context.Response.ContentType ?? ""
|
||||||
}
|
};
|
||||||
});
|
|
||||||
|
|
||||||
// เขียนข้อมูลกลับไปยัง original Response body
|
// เขียนข้อมูลกลับไปยัง original Response body ก่อน
|
||||||
if (memoryStream.Length > 0)
|
if (memoryStream.Length > 0)
|
||||||
{
|
{
|
||||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
await memoryStream.CopyToAsync(originalBodyStream);
|
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,
|
private async Task LogRequestAsync(ElasticClient client, DateTime startTime, Stopwatch stopwatch,
|
||||||
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, string? responseBodyForLogging, Exception? caughtException)
|
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, string? responseBodyForLogging, Exception? caughtException, dynamic contextData)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("[DEBUG] LogRequestAsync called");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var processTime = stopwatch.ElapsedMilliseconds;
|
var processTime = stopwatch.ElapsedMilliseconds;
|
||||||
var endTime = DateTime.UtcNow;
|
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
|
var logType = caughtException != null ? "error" : statusCode switch
|
||||||
{
|
{
|
||||||
|
|
@ -425,12 +438,11 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
// ใช้ response body ที่ส่งมาจากการอ่านก่อนหน้า
|
// ใช้ response body ที่ส่งมาจากการอ่านก่อนหน้า
|
||||||
if (!string.IsNullOrEmpty(responseBodyForLogging))
|
if (!string.IsNullOrEmpty(responseBodyForLogging))
|
||||||
{
|
{
|
||||||
var contentType = context.Response.ContentType ?? "";
|
var contentType = (string)contextData.ContentType;
|
||||||
var isFileResponse = !contentType.StartsWith("application/json") && !contentType.StartsWith("text/html") && (
|
var isFileResponse = !contentType.StartsWith("application/json") && !contentType.StartsWith("text/html") && (
|
||||||
contentType.StartsWith("application/") ||
|
contentType.StartsWith("application/") ||
|
||||||
contentType.StartsWith("image/") ||
|
contentType.StartsWith("image/") ||
|
||||||
contentType.StartsWith("audio/") ||
|
contentType.StartsWith("audio/")
|
||||||
context.Response.Headers.ContainsKey("Content-Disposition")
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isFileResponse)
|
if (isFileResponse)
|
||||||
|
|
@ -470,15 +482,15 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
var logData = new
|
var logData = new
|
||||||
{
|
{
|
||||||
logType = logType,
|
logType = logType,
|
||||||
ip = context.Connection.RemoteIpAddress?.ToString(),
|
ip = (string)contextData.RemoteIpAddress,
|
||||||
rootId = pf?.RootDnaId,
|
rootId = pf?.RootId,
|
||||||
systemName = SystemName,
|
systemName = SystemName,
|
||||||
startTimeStamp = startTime.ToString("o"),
|
startTimeStamp = startTime.ToString("o"),
|
||||||
endTimeStamp = endTime.ToString("o"),
|
endTimeStamp = endTime.ToString("o"),
|
||||||
processTime = processTime,
|
processTime = processTime,
|
||||||
host = context.Request.Host.Value,
|
host = (string)contextData.HostValue,
|
||||||
method = context.Request.Method,
|
method = (string)contextData.Method,
|
||||||
endpoint = context.Request.Path + context.Request.QueryString,
|
endpoint = (string)contextData.Path + (string)contextData.QueryString,
|
||||||
responseCode = statusCode == 304 ? "200" : statusCode.ToString(),
|
responseCode = statusCode == 304 ? "200" : statusCode.ToString(),
|
||||||
responseDescription = message,
|
responseDescription = message,
|
||||||
input = requestBodyJson,
|
input = requestBodyJson,
|
||||||
|
|
@ -489,11 +501,19 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
exception = caughtException?.ToString()
|
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)
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ElasticConfiguration": {
|
"ElasticConfiguration": {
|
||||||
"Uri": "http://192.168.1.40:9200",
|
"Uri": "http://192.168.1.63:9200",
|
||||||
"IndexFormat": "bma-ehr-log-index",
|
"IndexFormat": "hrms-log-index",
|
||||||
"SystemName": "insignia"
|
"SystemName": "insignia"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ElasticConfiguration": {
|
"ElasticConfiguration": {
|
||||||
"Uri": "http://192.168.1.40:9200",
|
"Uri": "http://192.168.1.63:9200",
|
||||||
"IndexFormat": "bma-ehr-log-index",
|
"IndexFormat": "hrms-log-index",
|
||||||
"SystemName": "leave"
|
"SystemName": "leave"
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue