From 3b0b607836ad1ecd89579c72b019668bd4675a73 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 22 Jan 2016 22:10:21 -0500 Subject: [PATCH] trim logging --- .../HttpServer/HttpListenerHost.cs | 34 +++++++++++++-- .../HttpServer/LoggerUtils.cs | 19 +++++++++ .../SocketSharp/WebSocketSharpListener.cs | 42 +------------------ 3 files changed, 52 insertions(+), 43 deletions(-) diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 6a23a84977..8d8d7f6f75 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -256,6 +256,25 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } + private readonly Dictionary _skipLogExtensions = new Dictionary(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); + } + /// /// Overridable method that can be used to implement a custom hnandler /// @@ -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; diff --git a/MediaBrowser.Server.Implementations/HttpServer/LoggerUtils.cs b/MediaBrowser.Server.Implementations/HttpServer/LoggerUtils.cs index fae702023a..0b8caaa6e0 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/LoggerUtils.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/LoggerUtils.cs @@ -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 { + /// + /// Logs the request. + /// + /// The logger. + /// The request. + 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); + } + /// /// Logs the response. /// diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index a91b1e3ed2..a029e09555 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -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; } - /// - /// Logs the request. - /// - /// The logger. - /// The request. - 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);