From 2e59a93facfb737164524c008d26f13081ef1eb5 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 13 Jan 2022 20:34:49 +0000 Subject: [PATCH] Fix log reading issue --- server/AyaNova/Controllers/LogFilesController.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/AyaNova/Controllers/LogFilesController.cs b/server/AyaNova/Controllers/LogFilesController.cs index db428942..470a8360 100644 --- a/server/AyaNova/Controllers/LogFilesController.cs +++ b/server/AyaNova/Controllers/LogFilesController.cs @@ -10,6 +10,7 @@ using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using System.Threading.Tasks; using System; +using System.IO; namespace AyaNova.Api.Controllers { @@ -77,9 +78,17 @@ namespace AyaNova.Api.Controllers //Log - // EventLogProcessor.AddEntry(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.LogFile, AyaEvent.Retrieved,logname), ct); - return Content(System.IO.File.ReadAllText(logFilePath)); + //nlog update now locks file for writing so needs to be opened this way + FileStreamOptions fso = new FileStreamOptions(); + fso.Access = FileAccess.Read; + fso.Mode = FileMode.Open; + fso.Share = FileShare.ReadWrite; + using (StreamReader sr = new StreamReader(logFilePath, fso)) + { + return Content(sr.ReadToEnd()); + } + //return Content(System.IO.File.ReadAllText(logFilePath)); } @@ -142,7 +151,8 @@ namespace AyaNova.Api.Controllers var logFilePath = System.IO.Path.Combine(ServerBootConfig.AYANOVA_LOG_PATH, logname); if (!System.IO.File.Exists(logFilePath)) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); - + + //Note: this works with new Nlog file locking for write so it must be opening it as read and shareable return PhysicalFile(logFilePath, "text/plain", $"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}-{logname}"); }