2014-07-09 00:46:11 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer
|
|
|
|
|
{
|
|
|
|
|
public static class LoggerUtils
|
|
|
|
|
{
|
2014-01-22 22:38:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logs the request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <param name="request">The request.</param>
|
2014-07-09 00:46:11 +00:00
|
|
|
|
public static void LogRequest(ILogger logger, HttpListenerRequest request)
|
2013-12-07 15:52:38 +00:00
|
|
|
|
{
|
|
|
|
|
var log = new StringBuilder();
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
//var headers = string.Join(",", request.Headers.AllKeys.Where(i => !string.Equals(i, "cookie", StringComparison.OrdinalIgnoreCase) && !string.Equals(i, "Referer", StringComparison.OrdinalIgnoreCase)).Select(k => k + "=" + request.Headers[k]));
|
2014-01-29 21:54:40 +00:00
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
//log.AppendLine("Ip: " + request.RemoteEndPoint + ". Headers: " + headers);
|
2013-12-07 15:52:38 +00:00
|
|
|
|
|
2014-01-22 22:38:48 +00:00
|
|
|
|
var type = request.IsWebSocketRequest ? "Web Socket" : "HTTP " + request.HttpMethod;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
|
2014-01-22 22:38:48 +00:00
|
|
|
|
logger.LogMultiline(type + " " + request.Url, LogSeverity.Debug, log);
|
2013-12-07 15:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logs the response.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2014-07-09 00:46:11 +00:00
|
|
|
|
/// <param name="statusCode">The status code.</param>
|
2013-12-07 15:52:38 +00:00
|
|
|
|
/// <param name="url">The URL.</param>
|
|
|
|
|
/// <param name="endPoint">The end point.</param>
|
|
|
|
|
/// <param name="duration">The duration.</param>
|
2014-07-09 00:46:11 +00:00
|
|
|
|
public static void LogResponse(ILogger logger, int statusCode, string url, string endPoint, TimeSpan duration)
|
2013-12-07 15:52:38 +00:00
|
|
|
|
{
|
|
|
|
|
var log = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
log.AppendLine(string.Format("Url: {0}", url));
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
//log.AppendLine("Headers: " + string.Join(",", response.Headers.AllKeys.Select(k => k + "=" + response.Headers[k])));
|
2013-12-07 15:52:38 +00:00
|
|
|
|
|
2014-07-09 00:46:11 +00:00
|
|
|
|
var responseTime = string.Format(". Response time: {0} ms.", duration.TotalMilliseconds);
|
2013-12-07 15:52:38 +00:00
|
|
|
|
|
2014-01-22 22:38:48 +00:00
|
|
|
|
var msg = "HTTP Response " + statusCode + " to " + endPoint + responseTime;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
|
|
|
|
|
logger.LogMultiline(msg, LogSeverity.Debug, log);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|