diff --git a/Core/Servers/Http/RequestLogger.cs b/Core/Servers/Http/RequestLogger.cs new file mode 100644 index 00000000..bf455a0b --- /dev/null +++ b/Core/Servers/Http/RequestLogger.cs @@ -0,0 +1,7 @@ +namespace Core.Servers.Http; + +// This is a dummy class to use for SourceContext in Serilog, do not remove! +public class RequestLogger +{ + +} diff --git a/Core/Servers/Http/SptHttpListener.cs b/Core/Servers/Http/SptHttpListener.cs index ce92477f..86f53b93 100644 --- a/Core/Servers/Http/SptHttpListener.cs +++ b/Core/Servers/Http/SptHttpListener.cs @@ -18,6 +18,7 @@ public class SptHttpListener : IHttpListener protected readonly HttpRouter _router; protected readonly IEnumerable _serializers; protected readonly ISptLogger _logger; + protected readonly ISptLogger _requestLogger; protected readonly HttpResponseUtil _httpResponseUtil; protected readonly LocalisationService _localisationService; protected readonly JsonUtil _jsonUtil; @@ -25,7 +26,7 @@ public class SptHttpListener : IHttpListener HttpRouter httpRouter, // TODO: delay required IEnumerable serializers, ISptLogger logger, - // TODO: requestsLogger: ISptLogger, + ISptLogger requestsLogger, JsonUtil jsonUtil, HttpResponseUtil httpHttpResponseUtil, LocalisationService localisationService @@ -34,6 +35,7 @@ public class SptHttpListener : IHttpListener _router = httpRouter; _serializers = serializers; _logger = logger; + _requestLogger = requestsLogger; _httpResponseUtil = httpHttpResponseUtil; _localisationService = localisationService; _jsonUtil = jsonUtil; @@ -135,7 +137,8 @@ public class SptHttpListener : IHttpListener SendZlibJson(resp, output, sessionID); } // Console.WriteLine($"Response: {output}"); - // TODO: this.LogRequest(req, output); + + LogRequest(req, output); } /** @@ -152,28 +155,17 @@ public class SptHttpListener : IHttpListener * @param req Incoming message request * @param output Output string */ - /* TODO: log requests - protected logRequest(req: IncomingMessage, output: string): void { - // - if (ProgramStatics.ENTRY_TYPE !== EntryType.RELEASE) { - const log = new Response(req.method, output); - this.requestsLogger.info(`RESPONSE=${this.jsonUtil.serialize(log)}`); - } + protected void LogRequest(HttpRequest req, string output) + { + // TODO: when do we want to log these? + //if (ProgramStatics.ENTRY_TYPE !== EntryType.RELEASE) { + var log = new Response(req.Method, output); + _requestLogger.Info($"RESPONSE={_jsonUtil.Serialize(log)}"); + //} } - */ public string GetResponse(string sessionID, HttpRequest req, string? body) { - /* TODO: REQUEST LOGGER - if (ProgramStatics.ENTRY_TYPE !== EntryType.RELEASE) { - // Parse quest info into object - const data = typeof info === "object" ? info : this.jsonUtil.deserialize(info); - - const log = new Request(req.method, new RequestData(req.url, req.headers, data)); - this.requestsLogger.info(`REQUEST=${this.jsonUtil.serialize(log)}`); - } - */ - var output = _router.GetResponse(req, sessionID, body, out var deserializedObject); /* route doesn't exist or response is not properly set up */ if (string.IsNullOrEmpty(output)) { @@ -181,6 +173,14 @@ public class SptHttpListener : IHttpListener _logger.Info(_jsonUtil.Serialize(deserializedObject)); output = _httpResponseUtil.GetBody(null, 404, $"UNHANDLED RESPONSE: {req.Path}"); } + /* TODO: REQUEST LOGGER + if (ProgramStatics.ENTRY_TYPE !== EntryType.RELEASE) { + */ + // Parse quest info into object + + var log = new Request(req.Method, new RequestData(req.Path, req.Headers, deserializedObject)); + _requestLogger.Info($"REQUEST={_jsonUtil.Serialize(log)}"); + //} return output; } @@ -209,5 +209,10 @@ public class SptHttpListener : IHttpListener resp.StartAsync().Wait(); resp.CompleteAsync().Wait(); } - + + record Response(string Method, string jsonData); + record Request(string Method, object output); + record RequestData(string Url, object Headers, object Data); } + + diff --git a/Core/Utils/Cloners/ICloner.cs b/Core/Utils/Cloners/ICloner.cs index d8a1978f..8c4fc691 100644 --- a/Core/Utils/Cloners/ICloner.cs +++ b/Core/Utils/Cloners/ICloner.cs @@ -1,5 +1,6 @@ namespace Core.Utils.Cloners; -public interface ICloner { - public T Clone(T obj); +public interface ICloner +{ + public T? Clone(T? obj); } diff --git a/Core/Utils/Cloners/JsonCloner.cs b/Core/Utils/Cloners/JsonCloner.cs index 3950b0b3..6844144b 100644 --- a/Core/Utils/Cloners/JsonCloner.cs +++ b/Core/Utils/Cloners/JsonCloner.cs @@ -10,7 +10,7 @@ public class JsonCloner : ICloner { _jsonUtil = jsonUtil; } - public T Clone(T obj) + public T? Clone(T? obj) { return _jsonUtil.Deserialize(_jsonUtil.Serialize(obj)); } diff --git a/Core/Utils/DatabaseImporter.cs b/Core/Utils/DatabaseImporter.cs index 69872b4f..cc2bd037 100644 --- a/Core/Utils/DatabaseImporter.cs +++ b/Core/Utils/DatabaseImporter.cs @@ -128,9 +128,8 @@ public class DatabaseImporter : OnLoad { _logger.Info(_localisationService.GetText("importing_database")); - var dataToImport = (DatabaseTables) await _importerUtil.LoadRecursiveAsync( + var dataToImport = await _importerUtil.LoadRecursiveAsync( $"{filePath}database/", - typeof(DatabaseTables), OnReadValidate ); diff --git a/Core/Utils/ImporterUtil.cs b/Core/Utils/ImporterUtil.cs index 25b9fa28..06ca11f5 100644 --- a/Core/Utils/ImporterUtil.cs +++ b/Core/Utils/ImporterUtil.cs @@ -20,12 +20,21 @@ public class ImporterUtil _jsonUtil = jsonUtil; } + public Task LoadRecursiveAsync( + string filepath, + Action? onReadCallback = null, + Action? onObjectDeserialized = null + ) + { + return LoadRecursiveAsync(filepath, typeof(T), onReadCallback, onObjectDeserialized).ContinueWith(res => (T) res.Result); + } + /** * Load files into js objects recursively (asynchronous) * @param filepath Path to folder with files * @returns Promise return T type associated with this class */ - public Task LoadRecursiveAsync( + protected Task LoadRecursiveAsync( string filepath, Type loadedType, Action? onReadCallback = null, diff --git a/Server/Program.cs b/Server/Program.cs index 649a9356..c0c17575 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -60,6 +60,7 @@ public static class Program } catch (Exception ex) { + Console.WriteLine(ex); logger.LogCritical(ex, "Critical exception, stopping server..."); // throw ex; } diff --git a/Server/Server.csproj b/Server/Server.csproj index 899454fc..81803f74 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -37,6 +37,7 @@ + diff --git a/Server/appsettings.json b/Server/appsettings.json index c1936e7a..50182e24 100644 --- a/Server/appsettings.json +++ b/Server/appsettings.json @@ -14,9 +14,53 @@ "Args": { "configure": [ { - "Name": "Console", + "Name": "Logger", "Args": { - "formatter": "Server.Logger.ConsoleFormatter::Default, Server" + "configureLogger": { + "Filter": [ + { + "Name": "ByExcluding", + "Args": { + "expression": "StartsWith(SourceContext, 'Core.Servers.Http.RequestLogger')" + } + } + ], + "WriteTo": [ + { + "Name": "Console", + "Args": { + "formatter": "Server.Logger.ConsoleFormatter::Default, Server" + } + } + ] + } + } + }, + { + "Name": "Logger", + "Args": { + "configureLogger": { + "Filter": [ + { + "Name": "ByIncludingOnly", + "Args": { + "expression": "StartsWith(SourceContext, 'Core.Servers.Http.RequestLogger')" + } + } + ], + "WriteTo": [ + { + "Name": "File", + "Args": { + "formatter": "Server.Logger.FileFormatter::Default, Server", + "path": "./user/logs/requests.txt", + "fileSizeLimitBytes": "20971520", + "rollOnFileSizeLimit": true, + "rollingInterval": "Day" + } + } + ] + } } }, { @@ -33,14 +77,6 @@ } } ], - "Filter": [ - { - "Name": "ByExcluding", - "Args": { - "expression": "StartsWith(SourceContext, 'TADAADA')" - } - } - ], "Enrich": [ "FromLogContext", "WithExceptionDetails" diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 5c0de1e2..ea6ebb54 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -2,7 +2,6 @@ net9.0 - latest enable enable @@ -20,4 +19,12 @@ + + + + + + + +