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

56 lines
2.1 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
2013-12-07 15:52:38 +00:00
using System;
2015-07-08 16:10:34 +00:00
using System.Globalization;
2017-05-09 18:51:26 +00:00
using MediaBrowser.Model.Services;
2013-12-07 15:52:38 +00:00
2016-11-08 18:44:23 +00:00
namespace Emby.Server.Implementations.HttpServer
2013-12-07 15:52:38 +00:00
{
public static class LoggerUtils
{
2017-05-09 18:51:26 +00:00
public static void LogRequest(ILogger logger, string url, string method, string userAgent, QueryParamCollection headers)
2016-01-23 03:10:21 +00:00
{
2017-05-09 18:51:26 +00:00
if (headers == null)
{
logger.LogInformation("{0} {1}. UserAgent: {2}", "HTTP " + method, url, userAgent ?? string.Empty);
2017-05-09 18:51:26 +00:00
}
else
{
2017-08-30 18:52:29 +00:00
var headerText = string.Empty;
var index = 0;
foreach (var i in headers)
{
if (index > 0)
{
headerText += ", ";
}
headerText += i.Name + "=" + i.Value;
index++;
}
2017-05-09 18:51:26 +00:00
logger.LogInformation("HTTP {0} {1}. {2}", method, url, headerText);
2017-05-09 18:51:26 +00:00
}
2016-01-23 03:10:21 +00:00
}
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>
2017-05-09 18:51:26 +00:00
public static void LogResponse(ILogger logger, int statusCode, string url, string endPoint, TimeSpan duration, QueryParamCollection headers)
2013-12-07 15:52:38 +00:00
{
2015-10-28 19:40:38 +00:00
var durationMs = duration.TotalMilliseconds;
2016-06-14 19:21:26 +00:00
var logSuffix = durationMs >= 1000 && durationMs < 60000 ? "ms (slow)" : "ms";
2015-10-28 19:40:38 +00:00
2017-08-30 18:52:29 +00:00
//var headerText = headers == null ? string.Empty : "Headers: " + string.Join(", ", headers.Where(i => i.Name.IndexOf("Access-", StringComparison.OrdinalIgnoreCase) == -1).Select(i => i.Name + "=" + i.Value).ToArray());
var headerText = string.Empty;
logger.LogInformation("HTTP Response {0} to {1}. Time: {2}{3}. {4} {5}", statusCode, endPoint, Convert.ToInt32(durationMs).ToString(CultureInfo.InvariantCulture), logSuffix, url, headerText);
2013-12-07 15:52:38 +00:00
}
}
}