ln.http/ln.http/router/LoggingRequestRouter.cs

61 lines
1.6 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
{
HttpRouterDelegate Next { get; set; }
Logger Logger { get; }
public LoggingRouter(HttpRouterDelegate next)
:this(next, Logger.Default)
{}
public LoggingRouter(HttpRouterDelegate next,Logger logger)
{
Next = next;
Logger = logger;
}
public bool Route(HttpContext httpContext)
{
DateTime start = DateTime.Now;
bool success = false;
try
{
success = Next(httpContext);
}
catch (Exception e)
{
throw;
}
finally
{
DateTime end = DateTime.Now;
TimeSpan duration = end - start;
Logger.Log(LogLevel.INFO, "{0} {1} {2} {3} {4} {5} {6}",
start,
end,
duration,
httpContext.Response?.StatusCode.ToString() ?? "-",
httpContext.AuthenticatedPrincipal?.ToString() ?? "-",
httpContext.Request.Method,
httpContext.Request.RequestUri
);
}
return success;
}
}
}