From b691eab0ee0a685a827617b3fb1666f32ec1666e Mon Sep 17 00:00:00 2001 From: Michael Green <84688932+michael-j-green@users.noreply.github.com> Date: Thu, 14 Dec 2023 01:09:19 +1100 Subject: [PATCH] Resolve a critical error in new logging code (#231) --- gaseous-server/Classes/Common.cs | 6 ++-- gaseous-server/Classes/Logging.cs | 34 ++++++++++++------ .../Controllers/V1.0/FilterController.cs | 2 +- .../Controllers/V1.0/GamesController.cs | 2 +- .../Controllers/V1.0/RomsController.cs | 2 +- .../Controllers/V1.0/SignaturesController.cs | 2 +- gaseous-server/Program.cs | 35 ++++++++----------- .../Support/Database/MySQL/gaseous-1008.sql | 2 +- 8 files changed, 45 insertions(+), 40 deletions(-) diff --git a/gaseous-server/Classes/Common.cs b/gaseous-server/Classes/Common.cs index 81d5dea..519776b 100644 --- a/gaseous-server/Classes/Common.cs +++ b/gaseous-server/Classes/Common.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Security.Cryptography; namespace gaseous_server.Classes @@ -137,5 +136,4 @@ namespace gaseous_server.Classes public static object GetData(string name) => state.TryGetValue(name, out AsyncLocal data) ? data.Value : null; } -} - +} \ No newline at end of file diff --git a/gaseous-server/Classes/Logging.cs b/gaseous-server/Classes/Logging.cs index dfa9447..c6ccdc7 100644 --- a/gaseous-server/Classes/Logging.cs +++ b/gaseous-server/Classes/Logging.cs @@ -76,24 +76,38 @@ namespace gaseous_server.Classes } string correlationId; - if (CallContext.GetData("CorrelationId").ToString() == null) + try + { + if (CallContext.GetData("CorrelationId").ToString() == null) + { + correlationId = ""; + } + else + { + correlationId = CallContext.GetData("CorrelationId").ToString(); + } + } + catch { correlationId = ""; } - else - { - correlationId = CallContext.GetData("CorrelationId").ToString(); - } string callingProcess; - if (CallContext.GetData("CallingProcess").ToString() == null) + try + { + if (CallContext.GetData("CallingProcess").ToString() == null) + { + callingProcess = ""; + } + else + { + callingProcess = CallContext.GetData("CallingProcess").ToString(); + } + } + catch { callingProcess = ""; } - else - { - callingProcess = CallContext.GetData("CallingProcess").ToString(); - } Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); string sql = "DELETE FROM ServerLogs WHERE EventTime < @EventRententionDate; INSERT INTO ServerLogs (EventTime, EventType, Process, Message, Exception, CorrelationId, CallingProcess) VALUES (@EventTime, @EventType, @Process, @Message, @Exception, @correlationid, @callingprocess);"; diff --git a/gaseous-server/Controllers/V1.0/FilterController.cs b/gaseous-server/Controllers/V1.0/FilterController.cs index 422d7d7..ad25624 100644 --- a/gaseous-server/Controllers/V1.0/FilterController.cs +++ b/gaseous-server/Controllers/V1.0/FilterController.cs @@ -18,7 +18,7 @@ namespace gaseous_server.Controllers [ApiVersion("1.1")] [Authorize] [ApiController] - public class FilterController : ControllerBase + public class FilterController : Controller { private readonly UserManager _userManager; private readonly SignInManager _signInManager; diff --git a/gaseous-server/Controllers/V1.0/GamesController.cs b/gaseous-server/Controllers/V1.0/GamesController.cs index 0f4e634..9c55f1b 100644 --- a/gaseous-server/Controllers/V1.0/GamesController.cs +++ b/gaseous-server/Controllers/V1.0/GamesController.cs @@ -22,7 +22,7 @@ namespace gaseous_server.Controllers [ApiVersion("1.1")] [Authorize] [ApiController] - public class GamesController : ControllerBase + public class GamesController : Controller { [MapToApiVersion("1.0")] [HttpGet] diff --git a/gaseous-server/Controllers/V1.0/RomsController.cs b/gaseous-server/Controllers/V1.0/RomsController.cs index e2129d8..167604e 100644 --- a/gaseous-server/Controllers/V1.0/RomsController.cs +++ b/gaseous-server/Controllers/V1.0/RomsController.cs @@ -22,7 +22,7 @@ namespace gaseous_server.Controllers [ApiVersion("1.1")] [Authorize] [ApiController] - public class RomsController : ControllerBase + public class RomsController : Controller { [MapToApiVersion("1.0")] [MapToApiVersion("1.1")] diff --git a/gaseous-server/Controllers/V1.0/SignaturesController.cs b/gaseous-server/Controllers/V1.0/SignaturesController.cs index 5eb0e07..4040125 100644 --- a/gaseous-server/Controllers/V1.0/SignaturesController.cs +++ b/gaseous-server/Controllers/V1.0/SignaturesController.cs @@ -18,7 +18,7 @@ namespace gaseous_server.Controllers [ApiVersion("1.0")] [ApiVersion("1.1")] [Authorize] - public class SignaturesController : ControllerBase + public class SignaturesController : Controller { /// /// Get the current signature counts from the database diff --git a/gaseous-server/Program.cs b/gaseous-server/Program.cs index 26c9134..0d876aa 100644 --- a/gaseous-server/Program.cs +++ b/gaseous-server/Program.cs @@ -295,29 +295,22 @@ using (var scope = app.Services.CreateScope()) await roleManager.CreateAsync(applicationRole, CancellationToken.None); } } - - // // set up administrator account - // var userManager = scope.ServiceProvider.GetRequiredService(); - // if (await userManager.FindByNameAsync("admin@localhost", CancellationToken.None) == null) - // { - // ApplicationUser adminUser = new ApplicationUser{ - // Id = Guid.NewGuid().ToString(), - // Email = "admin@localhost", - // NormalizedEmail = "ADMIN@LOCALHOST", - // EmailConfirmed = true, - // UserName = "administrator", - // NormalizedUserName = "ADMINISTRATOR" - // }; - - // //set user password - // PasswordHasher ph = new PasswordHasher(); - // adminUser.PasswordHash = ph.HashPassword(adminUser, "letmein"); - - // await userManager.CreateAsync(adminUser, CancellationToken.None); - // await userManager.AddToRoleAsync(adminUser, "Admin", CancellationToken.None); - // } } + + + +app.Use(async (context, next) => +{ + // set the correlation id + string correlationId = Guid.NewGuid().ToString(); + CallContext.SetData("CorrelationId", correlationId); + CallContext.SetData("CallingProcess", context.Request.Method + ": " + context.Request.Path); + + context.Response.Headers.Add("x-correlation-id", correlationId.ToString()); + await next(); +}); + app.UseAuthorization(); app.UseDefaultFiles(); diff --git a/gaseous-server/Support/Database/MySQL/gaseous-1008.sql b/gaseous-server/Support/Database/MySQL/gaseous-1008.sql index ed3c3fe..603ce77 100644 --- a/gaseous-server/Support/Database/MySQL/gaseous-1008.sql +++ b/gaseous-server/Support/Database/MySQL/gaseous-1008.sql @@ -21,6 +21,6 @@ ADD INDEX `idx_SecondaryColumn` (`ThemesId` ASC) VISIBLE; ALTER TABLE `ServerLogs` ADD COLUMN `CorrelationId` VARCHAR(45) NULL AFTER `Exception`, -ADD COLUMN `CallingProcess` VARCHAR(45) NULL AFTER `CorrelationId`, +ADD COLUMN `CallingProcess` VARCHAR(255) NULL AFTER `CorrelationId`, ADD INDEX `idx_CorrelationId` (`CorrelationId` ASC) VISIBLE, ADD INDEX `idx_CallingProcess` (`CallingProcess` ASC) VISIBLE; \ No newline at end of file