#1 Simple logging to ln.logger.Logger instance implemented
parent
7bd7e4f8d6
commit
89f748cea4
|
@ -8,6 +8,7 @@ using ln.http.resources.session;
|
||||||
using ln.logging;
|
using ln.logging;
|
||||||
using ln.types.threads;
|
using ln.types.threads;
|
||||||
using ln.types;
|
using ln.types;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
|
||||||
namespace ln.http
|
namespace ln.http
|
||||||
|
@ -26,8 +27,12 @@ namespace ln.http
|
||||||
|
|
||||||
public Func<HTTPServer,TcpClient,HTTPServerConnection> CreateServerConnection { get; set; }
|
public Func<HTTPServer,TcpClient,HTTPServerConnection> CreateServerConnection { get; set; }
|
||||||
|
|
||||||
|
public Logger Logger { get; set; }
|
||||||
|
|
||||||
bool shutdown = false;
|
bool shutdown = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Dictionary<IPEndPoint, TcpListener> tcpListeners = new Dictionary<IPEndPoint, TcpListener>();
|
Dictionary<IPEndPoint, TcpListener> tcpListeners = new Dictionary<IPEndPoint, TcpListener>();
|
||||||
Dictionary<URI, HttpApplication> applications = new Dictionary<URI, HttpApplication>();
|
Dictionary<URI, HttpApplication> applications = new Dictionary<URI, HttpApplication>();
|
||||||
|
|
||||||
|
@ -37,6 +42,7 @@ namespace ln.http
|
||||||
|
|
||||||
public HTTPServer()
|
public HTTPServer()
|
||||||
{
|
{
|
||||||
|
Logger = Logger.Default;
|
||||||
CreateServerConnection = (HTTPServer httpServer, TcpClient tcpClient) => new HTTPServerConnection(httpServer, tcpClient);
|
CreateServerConnection = (HTTPServer httpServer, TcpClient tcpClient) => new HTTPServerConnection(httpServer, tcpClient);
|
||||||
|
|
||||||
SessionCache = new SessionCache();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,15 @@ namespace ln.http
|
||||||
|
|
||||||
public event HTTPServerConnectionEvent AbortRequested;
|
public event HTTPServerConnectionEvent AbortRequested;
|
||||||
|
|
||||||
|
public DateTime Created { get; }
|
||||||
|
public DateTime Interpreted { get; set; }
|
||||||
|
public DateTime Finished { get; set; }
|
||||||
|
|
||||||
public HTTPServerConnection(HTTPServer httpServer,TcpClient tcpClient)
|
public HTTPServerConnection(HTTPServer httpServer,TcpClient tcpClient)
|
||||||
{
|
{
|
||||||
HTTPServer = httpServer;
|
HTTPServer = httpServer;
|
||||||
TcpClient = tcpClient;
|
TcpClient = tcpClient;
|
||||||
|
Created = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual HttpResponse GetResponse(HttpRequest httpRequest,HttpApplication httpApplication) => httpApplication.GetResponse(httpRequest);
|
public virtual HttpResponse GetResponse(HttpRequest httpRequest,HttpApplication httpApplication) => httpApplication.GetResponse(httpRequest);
|
||||||
|
@ -63,6 +68,8 @@ namespace ln.http
|
||||||
{
|
{
|
||||||
CurrentRequest.ApplySession(HTTPServer.SessionCache);
|
CurrentRequest.ApplySession(HTTPServer.SessionCache);
|
||||||
|
|
||||||
|
Interpreted = DateTime.Now;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpApplication application = HTTPServer.GetHttpApplication(new URI(CurrentRequest.BaseURI.ToString()));
|
HttpApplication application = HTTPServer.GetHttpApplication(new URI(CurrentRequest.BaseURI.ToString()));
|
||||||
|
@ -82,31 +89,36 @@ namespace ln.http
|
||||||
response.StatusCode = 500;
|
response.StatusCode = 500;
|
||||||
response.ContentWriter.WriteLine("Exception caught: {0}", e);
|
response.ContentWriter.WriteLine("Exception caught: {0}", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
setState("sending response");
|
|
||||||
|
|
||||||
if (response == null)
|
setState("sending response");
|
||||||
{
|
|
||||||
Logging.Log(LogLevel.DEBUG, "Request {0} returned no Response", CurrentRequest);
|
if (response == null)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!response.HasCustomContentStream)
|
|
||||||
{
|
{
|
||||||
response.ContentWriter.Flush();
|
Logging.Log(LogLevel.DEBUG, "Request {0} returned no Response", CurrentRequest);
|
||||||
MemoryStream cstream = (MemoryStream)response.ContentStream;
|
|
||||||
cstream.Position = 0;
|
|
||||||
|
|
||||||
response.SetHeader("content-length", cstream.Length.ToString());
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!response.HasCustomContentStream)
|
||||||
|
{
|
||||||
|
response.ContentWriter.Flush();
|
||||||
|
MemoryStream cstream = (MemoryStream)response.ContentStream;
|
||||||
|
cstream.Position = 0;
|
||||||
|
|
||||||
if (CurrentRequest.Session != null)
|
response.SetHeader("content-length", cstream.Length.ToString());
|
||||||
HTTPServer?.SessionCache?.ApplySessionID(response, CurrentRequest.Session);
|
}
|
||||||
|
|
||||||
response.AddCookie("LN_SEEN", DateTime.Now.ToString());
|
if (CurrentRequest.Session != null)
|
||||||
|
HTTPServer?.SessionCache?.ApplySessionID(response, CurrentRequest.Session);
|
||||||
|
|
||||||
SendResponse(TcpClient.GetStream(), response);
|
response.AddCookie("LN_SEEN", DateTime.Now.ToString());
|
||||||
TcpClient.Close();
|
|
||||||
|
SendResponse(TcpClient.GetStream(), response);
|
||||||
|
TcpClient.Close();
|
||||||
|
|
||||||
|
Finished = DateTime.Now;
|
||||||
|
|
||||||
|
HTTPServer.Log(Created, (Finished - Created).TotalMilliseconds, CurrentRequest, response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|
Loading…
Reference in New Issue