jellyfin/Emby.Server.Implementations/HttpServer/LoggerUtils.cs

44 lines
1.7 KiB
C#
Raw Normal View History

2014-07-08 17:46:11 -07:00
using MediaBrowser.Model.Logging;
2013-12-07 08:52:38 -07:00
using System;
2015-07-08 09:10:34 -07:00
using System.Globalization;
2016-01-22 20:10:21 -07:00
using SocketHttpListener.Net;
2013-12-07 08:52:38 -07:00
2016-11-08 11:44:23 -07:00
namespace Emby.Server.Implementations.HttpServer
2013-12-07 08:52:38 -07:00
{
public static class LoggerUtils
{
2016-01-22 20:10:21 -07:00
/// <summary>
/// Logs the request.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="request">The request.</param>
public static void LogRequest(ILogger logger, HttpListenerRequest request)
{
var url = request.Url.ToString();
2016-03-27 14:11:27 -07:00
logger.Info("{0} {1}. UserAgent: {2}", request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod, url, request.UserAgent ?? string.Empty);
2016-01-22 20:10:21 -07:00
}
public static void LogRequest(ILogger logger, string url, string method, string userAgent)
{
2016-03-27 14:11:27 -07:00
logger.Info("{0} {1}. UserAgent: {2}", "HTTP " + method, url, userAgent ?? string.Empty);
2016-01-22 20:10:21 -07:00
}
2013-12-07 08:52:38 -07:00
/// <summary>
/// Logs the response.
/// </summary>
/// <param name="logger">The logger.</param>
2014-07-08 17:46:11 -07:00
/// <param name="statusCode">The status code.</param>
2013-12-07 08:52:38 -07:00
/// <param name="url">The URL.</param>
/// <param name="endPoint">The end point.</param>
/// <param name="duration">The duration.</param>
2014-07-08 17:46:11 -07:00
public static void LogResponse(ILogger logger, int statusCode, string url, string endPoint, TimeSpan duration)
2013-12-07 08:52:38 -07:00
{
2015-10-28 12:40:38 -07:00
var durationMs = duration.TotalMilliseconds;
2016-06-14 12:21:26 -07:00
var logSuffix = durationMs >= 1000 && durationMs < 60000 ? "ms (slow)" : "ms";
2015-10-28 12:40:38 -07:00
logger.Info("HTTP Response {0} to {1}. Time: {2}{3}. {4}", statusCode, endPoint, Convert.ToInt32(durationMs).ToString(CultureInfo.InvariantCulture), logSuffix, url);
2013-12-07 08:52:38 -07:00
}
}
}