浏览代码

trim logging

Luke Pulverenti 9 年之前
父节点
当前提交
3b0b607836

+ 31 - 3
MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs

@@ -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>
         /// <summary>
         /// Overridable method that can be used to implement a custom hnandler
         /// Overridable method that can be used to implement a custom hnandler
         /// </summary>
         /// </summary>
@@ -271,6 +290,14 @@ namespace MediaBrowser.Server.Implementations.HttpServer
             var operationName = httpReq.OperationName;
             var operationName = httpReq.OperationName;
             var localPath = url.LocalPath;
             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) ||
             if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) ||
                 string.Equals(localPath, "/emby/", 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);
                 task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                 //Matches Exceptions handled in HttpListenerBase.InitTask()
                 //Matches Exceptions handled in HttpListenerBase.InitTask()
 
 
-                var urlString = url.ToString();
-
                 task.ContinueWith(x =>
                 task.ContinueWith(x =>
                 {
                 {
                     var statusCode = httpRes.StatusCode;
                     var statusCode = httpRes.StatusCode;
 
 
                     var duration = DateTime.Now - date;
                     var duration = DateTime.Now - date;
 
 
-                    LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration);
+                    if (enableLog)
+                    {
+                        LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration);
+                    }
 
 
                 }, TaskContinuationOptions.None);
                 }, TaskContinuationOptions.None);
                 return task;
                 return task;

+ 19 - 0
MediaBrowser.Server.Implementations/HttpServer/LoggerUtils.cs

@@ -1,11 +1,30 @@
 using MediaBrowser.Model.Logging;
 using MediaBrowser.Model.Logging;
 using System;
 using System;
 using System.Globalization;
 using System.Globalization;
+using System.IO;
+using SocketHttpListener.Net;
 
 
 namespace MediaBrowser.Server.Implementations.HttpServer
 namespace MediaBrowser.Server.Implementations.HttpServer
 {
 {
     public static class LoggerUtils
     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>
         /// <summary>
         /// Logs the response.
         /// Logs the response.
         /// </summary>
         /// </summary>

+ 2 - 40
MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs

@@ -78,10 +78,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
         {
         {
             var request = context.Request;
             var request = context.Request;
 
 
-            LogRequest(_logger, request);
-
             if (request.IsWebSocketRequest)
             if (request.IsWebSocketRequest)
             {
             {
+                LoggerUtils.LogRequest(_logger, request);
+
                 ProcessWebSocketRequest(context);
                 ProcessWebSocketRequest(context);
                 return Task.FromResult(true);
                 return Task.FromResult(true);
             }
             }
@@ -156,44 +156,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
             return req;
             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)
         private void HandleError(Exception ex, HttpListenerContext context)
         {
         {
             var httpReq = GetRequest(context);
             var httpReq = GetRequest(context);