trim logging

This commit is contained in:
Luke Pulverenti 2016-01-22 22:10:21 -05:00
parent 8570789373
commit 3b0b607836
3 changed files with 52 additions and 43 deletions

View File

@ -256,6 +256,25 @@ namespace MediaBrowser.Server.Implementations.HttpServer
}
}
private readonly Dictionary<string, int> _skipLogExtensions = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
{".js", 0},
{".css", 0},
{".woff", 0},
{".woff2", 0},
{".ttf", 0},
{".html", 0}
};
private bool EnableLogging(string url)
{
var parts = url.Split(new[] { '?' }, 2);
var extension = Path.GetExtension(parts[0]);
return string.IsNullOrWhiteSpace(extension) || !_skipLogExtensions.ContainsKey(extension);
}
/// <summary>
/// Overridable method that can be used to implement a custom hnandler
/// </summary>
@ -271,6 +290,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
var operationName = httpReq.OperationName;
var localPath = url.LocalPath;
var urlString = url.ToString();
var enableLog = EnableLogging(urlString);
if (enableLog)
{
LoggerUtils.LogRequest(_logger, urlString, httpReq.HttpMethod, httpReq.UserAgent);
}
if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) ||
string.Equals(localPath, "/emby/", StringComparison.OrdinalIgnoreCase))
{
@ -333,15 +360,16 @@ namespace MediaBrowser.Server.Implementations.HttpServer
task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
//Matches Exceptions handled in HttpListenerBase.InitTask()
var urlString = url.ToString();
task.ContinueWith(x =>
{
var statusCode = httpRes.StatusCode;
var duration = DateTime.Now - date;
LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration);
if (enableLog)
{
LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration);
}
}, TaskContinuationOptions.None);
return task;

View File

@ -1,11 +1,30 @@
using MediaBrowser.Model.Logging;
using System;
using System.Globalization;
using System.IO;
using SocketHttpListener.Net;
namespace MediaBrowser.Server.Implementations.HttpServer
{
public static class LoggerUtils
{
/// <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();
logger.Info("{0} {1}. UserAgent: {2}", (request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod), url, request.UserAgent ?? string.Empty);
}
public static void LogRequest(ILogger logger, string url, string method, string userAgent)
{
logger.Info("{0} {1}. UserAgent: {2}", ("HTTP " + method), url, userAgent ?? string.Empty);
}
/// <summary>
/// Logs the response.
/// </summary>

View File

@ -78,10 +78,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
var request = context.Request;
LogRequest(_logger, request);
if (request.IsWebSocketRequest)
{
LoggerUtils.LogRequest(_logger, request);
ProcessWebSocketRequest(context);
return Task.FromResult(true);
}
@ -156,44 +156,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
return req;
}
/// <summary>
/// Logs the request.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="request">The request.</param>
private static void LogRequest(ILogger logger, HttpListenerRequest request)
{
var url = request.Url.ToString();
var extension = Path.GetExtension(url);
if (string.Equals(extension, ".js", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (string.Equals(extension, ".css", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (string.Equals(extension, ".woff", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (string.Equals(extension, ".woff2", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (string.Equals(extension, ".ttf", StringComparison.OrdinalIgnoreCase))
{
return;
}
if (string.Equals(extension, ".html", StringComparison.OrdinalIgnoreCase))
{
return;
}
logger.Info("{0} {1}. UserAgent: {2}", (request.IsWebSocketRequest ? "WS" : "HTTP " + request.HttpMethod), url, request.UserAgent ?? string.Empty);
}
private void HandleError(Exception ex, HttpListenerContext context)
{
var httpReq = GetRequest(context);