#1 Simple logging to ln.logger.Logger instance implemented

master
Harald Wolff 2019-10-17 10:17:14 +02:00
parent 7bd7e4f8d6
commit 89f748cea4
2 changed files with 42 additions and 19 deletions

View File

@ -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<HTTPServer,TcpClient,HTTPServerConnection> CreateServerConnection { get; set; }
public Logger Logger { get; set; }
bool shutdown = false;
Dictionary<IPEndPoint, TcpListener> tcpListeners = new Dictionary<IPEndPoint, TcpListener>();
Dictionary<URI, HttpApplication> applications = new Dictionary<URI, HttpApplication>();
@ -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);
}
}
}

View File

@ -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