From 0a147104f179114d9a8aece0f7f80647404a2c2a Mon Sep 17 00:00:00 2001 From: JerryImMouse Date: Fri, 28 Jun 2024 01:49:21 +0500 Subject: [PATCH] file handler and method parsing fix --- Jerry.Utilities/HttpUtility/HttpContext.cs | 11 +++++++++-- .../Logging/Handlers/FileLogHandler.cs | 16 +++++++++++++++- Jerry.Utilities/Logging/LogStructs/LogExt.cs | 2 +- Jerry.Utilities/Utility/StringExtensions.cs | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 Jerry.Utilities/Utility/StringExtensions.cs diff --git a/Jerry.Utilities/HttpUtility/HttpContext.cs b/Jerry.Utilities/HttpUtility/HttpContext.cs index 163911e..3a5d9c8 100644 --- a/Jerry.Utilities/HttpUtility/HttpContext.cs +++ b/Jerry.Utilities/HttpUtility/HttpContext.cs @@ -1,6 +1,7 @@ using System.Net; using System.Text; using System.Text.Json; +using Jerry.Utilities.Utility; using Method = Jerry.Utilities.HttpUtility.HttpMethod; namespace Jerry.Utilities.HttpUtility; @@ -25,9 +26,15 @@ public class HttpContext #region Private private void ParseHttpMethod() { - if (!Enum.TryParse(_nativeContext.Request.HttpMethod, out Method method)) + var method = _nativeContext.Request.HttpMethod; + + method = method + .ToLower() + .CapitalizeFirstLetter(); + + if (!Enum.TryParse(method, out Method parsedMethod)) HttpMethod = Method.Get; - HttpMethod = method; + HttpMethod = parsedMethod; } private Dictionary GetHeaders() diff --git a/Jerry.Utilities/Logging/Handlers/FileLogHandler.cs b/Jerry.Utilities/Logging/Handlers/FileLogHandler.cs index 4ce2006..3a99d61 100644 --- a/Jerry.Utilities/Logging/Handlers/FileLogHandler.cs +++ b/Jerry.Utilities/Logging/Handlers/FileLogHandler.cs @@ -25,16 +25,30 @@ #endregion --- License --- using System.Text; +using Jerry.Utilities.IoC.General; using Jerry.Utilities.Logging.Interfaces; using Jerry.Utilities.Logging.LogStructs; using Serilog.Events; namespace Jerry.Utilities.Logging.Handlers; - public class FileLogHandler : ILogHandler, IDisposable { private readonly TextWriter _writer; + private static FileLogHandler? _instance; + + public static FileLogHandler Instance + { + get + { + if (_instance != null) + return _instance; + + _instance = new FileLogHandler("./logs.log"); + return _instance; + } + set => _instance = value; + } public FileLogHandler(string path) { Directory.CreateDirectory(Path.GetDirectoryName(path)!); diff --git a/Jerry.Utilities/Logging/LogStructs/LogExt.cs b/Jerry.Utilities/Logging/LogStructs/LogExt.cs index 1512791..fcb3e79 100644 --- a/Jerry.Utilities/Logging/LogStructs/LogExt.cs +++ b/Jerry.Utilities/Logging/LogStructs/LogExt.cs @@ -35,7 +35,7 @@ public static List ToHandlers(this HandlerFlags flags) if (flags.HasFlag(HandlerFlags.File)) { - handlerInstances.Add(new FileLogHandler("./logs.log")); + handlerInstances.Add(FileLogHandler.Instance); } //TODO: loki handler for advanced logging diff --git a/Jerry.Utilities/Utility/StringExtensions.cs b/Jerry.Utilities/Utility/StringExtensions.cs new file mode 100644 index 0000000..99c624c --- /dev/null +++ b/Jerry.Utilities/Utility/StringExtensions.cs @@ -0,0 +1,14 @@ +namespace Jerry.Utilities.Utility; + +public static class StringExtensions +{ + public static string CapitalizeFirstLetter(this string str) + { + if (str.Length == 0) + throw new NullReferenceException("String is null"); + + var array = str.ToCharArray(); + array[0] = char.ToUpper(array[0]); + return new string(array); + } +} \ No newline at end of file