ln.http/router/LoggingRouter.cs

60 lines
1.5 KiB
C#

// /**
// * File: LoggingRouter.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using ln.logging;
using System.Diagnostics;
namespace ln.http.router
{
public class LoggingRouter : IHttpRouter
{
IHttpRouter Next { get; }
Logger Logger { get; }
public LoggingRouter(IHttpRouter next)
:this(next, Logger.Default)
{}
public LoggingRouter(IHttpRouter next,Logger logger)
{
Next = next;
Logger = logger;
}
public HttpResponse Route(HttpRoutingContext routingContext, HttpRequest httpRequest)
{
DateTime start = DateTime.Now;
HttpResponse response = null;
try
{
response = Next.Route(routingContext, httpRequest);
}
catch (Exception e)
{
throw;
}
finally
{
DateTime end = DateTime.Now;
TimeSpan duration = end - start;
Logger.Log(LogLevel.INFO, "{0} {1} {2} {3} {4}",
start,
duration,
response?.StatusCode.ToString() ?? "-",
httpRequest.Method,
httpRequest.RequestURL
);
}
return response;
}
}
}