improve logging

This commit is contained in:
2024-12-09 06:29:05 +01:00
parent b5edc53623
commit cdd7915142

View File

@@ -132,7 +132,7 @@ public class ApiServer(string apiUrl) : IApiServer
}
catch (Exception ex)
{
Log.ErrorFormat("Error at getting context: ", ex.ToString());
Log.Error("Error retriving context", ex);
context = null;
}
@@ -145,6 +145,7 @@ public class ApiServer(string apiUrl) : IApiServer
return;
// Check context
Log.Info("Request retrived for " + context.Request.RawUrl);
OnCheckContext?.Invoke(this, new(context));
try
{
@@ -152,7 +153,7 @@ public class ApiServer(string apiUrl) : IApiServer
}
catch (Exception ex)
{
Log.ErrorFormat("Error at checking context: ", ex.ToString());
Log.Error("Error checking context", ex);
if (DebugMode)
throw;
}
@@ -168,11 +169,11 @@ public class ApiServer(string apiUrl) : IApiServer
protected virtual void CheckContext(HttpListenerContext context)
{
Log.Info("Start handling request");
Log.Debug("Start handling request");
void close()
{
Log.Info("End handling request");
Log.Debug("End handling request");
context.Response.OutputStream.Close();
}
@@ -182,7 +183,7 @@ public class ApiServer(string apiUrl) : IApiServer
var query = context.Request.Url?.Query;
if (string.IsNullOrWhiteSpace(path))
{
Log.Info("Request has no path");
Log.Warn("Request has no path");
close();
return;
}
@@ -191,7 +192,7 @@ public class ApiServer(string apiUrl) : IApiServer
Log.Debug("Find handler");
if (!TryGetHandler(path, query, context.Request.HttpMethod, out var handler))
{
Log.Info("Request handler couldn't be found");
Log.Warn("Request handler couldn't be found");
close();
return;
}
@@ -246,7 +247,7 @@ public class ApiServer(string apiUrl) : IApiServer
Log.Debug("Create response");
if (result.ResultJson is not null)
{
Log.Info("Sending response");
Log.Info("Sending response for " + context.Request.RawUrl);
context.Response.ContentType = "application/json";
using StreamWriter output = new(context.Response.OutputStream);
output.Write(result.ResultJson);