From 89f748cea4214adbddc274604b669bf53fac6f19 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Thu, 17 Oct 2019 10:17:14 +0200 Subject: [PATCH] #1 Simple logging to ln.logger.Logger instance implemented --- HTTPServer.cs | 11 +++++++++ HTTPServerConnection.cs | 50 +++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/HTTPServer.cs b/HTTPServer.cs index ec33347..edc6e87 100644 --- a/HTTPServer.cs +++ b/HTTPServer.cs @@ -8,6 +8,7 @@ using ln.http.resources.session; using ln.logging; using ln.types.threads; using ln.types; +using System.Globalization; namespace ln.http @@ -26,8 +27,12 @@ namespace ln.http public Func CreateServerConnection { get; set; } + public Logger Logger { get; set; } + bool shutdown = false; + + Dictionary tcpListeners = new Dictionary(); Dictionary applications = new Dictionary(); @@ -37,6 +42,7 @@ namespace ln.http public HTTPServer() { + Logger = Logger.Default; CreateServerConnection = (HTTPServer httpServer, TcpClient tcpClient) => new HTTPServerConnection(httpServer, tcpClient); SessionCache = new SessionCache(); @@ -157,5 +163,10 @@ namespace ln.http } } + public void Log(DateTime startTime,double duration,HttpRequest httpRequest,HttpResponse httpResponse) + { + Logger.Log(LogLevel.INFO, "{0} {1} {2} {3}",startTime.ToString("yyyyMMdd-HH:mm:ss"),duration.ToString(CultureInfo.InvariantCulture),httpRequest.Hostname,httpRequest.RequestURL); + } + } } diff --git a/HTTPServerConnection.cs b/HTTPServerConnection.cs index 5166a45..e417ff4 100644 --- a/HTTPServerConnection.cs +++ b/HTTPServerConnection.cs @@ -25,10 +25,15 @@ namespace ln.http public event HTTPServerConnectionEvent AbortRequested; + public DateTime Created { get; } + public DateTime Interpreted { get; set; } + public DateTime Finished { get; set; } + public HTTPServerConnection(HTTPServer httpServer,TcpClient tcpClient) { HTTPServer = httpServer; TcpClient = tcpClient; + Created = DateTime.Now; } public virtual HttpResponse GetResponse(HttpRequest httpRequest,HttpApplication httpApplication) => httpApplication.GetResponse(httpRequest); @@ -63,6 +68,8 @@ namespace ln.http { CurrentRequest.ApplySession(HTTPServer.SessionCache); + Interpreted = DateTime.Now; + try { HttpApplication application = HTTPServer.GetHttpApplication(new URI(CurrentRequest.BaseURI.ToString())); @@ -82,31 +89,36 @@ namespace ln.http response.StatusCode = 500; response.ContentWriter.WriteLine("Exception caught: {0}", e); } - } - setState("sending response"); - if (response == null) - { - Logging.Log(LogLevel.DEBUG, "Request {0} returned no Response", CurrentRequest); - } - else - { - if (!response.HasCustomContentStream) + setState("sending response"); + + if (response == null) { - response.ContentWriter.Flush(); - MemoryStream cstream = (MemoryStream)response.ContentStream; - cstream.Position = 0; - - response.SetHeader("content-length", cstream.Length.ToString()); + Logging.Log(LogLevel.DEBUG, "Request {0} returned no Response", CurrentRequest); } + else + { + if (!response.HasCustomContentStream) + { + response.ContentWriter.Flush(); + MemoryStream cstream = (MemoryStream)response.ContentStream; + cstream.Position = 0; - if (CurrentRequest.Session != null) - HTTPServer?.SessionCache?.ApplySessionID(response, CurrentRequest.Session); + response.SetHeader("content-length", cstream.Length.ToString()); + } - response.AddCookie("LN_SEEN", DateTime.Now.ToString()); + if (CurrentRequest.Session != null) + HTTPServer?.SessionCache?.ApplySessionID(response, CurrentRequest.Session); - SendResponse(TcpClient.GetStream(), response); - TcpClient.Close(); + response.AddCookie("LN_SEEN", DateTime.Now.ToString()); + + SendResponse(TcpClient.GetStream(), response); + TcpClient.Close(); + + Finished = DateTime.Now; + + HTTPServer.Log(Created, (Finished - Created).TotalMilliseconds, CurrentRequest, response); + } } } finally