From b1df61f7cededfdea119e98296f74e1313e4ffe3 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Mon, 10 Sep 2012 15:11:40 -0400 Subject: [PATCH] Added ThreadedLogger --- MediaBrowser.Api/ApiService.cs | 2 +- MediaBrowser.Common/Logging/BaseLogger.cs | 4 + MediaBrowser.Common/Logging/StreamLogger.cs | 4 +- MediaBrowser.Common/Logging/ThreadedLogger.cs | 75 +++++++++++++++++++ .../MediaBrowser.Common.csproj | 1 + 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 MediaBrowser.Common/Logging/ThreadedLogger.cs diff --git a/MediaBrowser.Api/ApiService.cs b/MediaBrowser.Api/ApiService.cs index 06929de73..a439a88d2 100644 --- a/MediaBrowser.Api/ApiService.cs +++ b/MediaBrowser.Api/ApiService.cs @@ -1,13 +1,13 @@ using MediaBrowser.Controller; using MediaBrowser.Model.DTO; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Entities.Movies; using MediaBrowser.Model.Entities.TV; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; -using MediaBrowser.Model.Entities.Movies; namespace MediaBrowser.Api { diff --git a/MediaBrowser.Common/Logging/BaseLogger.cs b/MediaBrowser.Common/Logging/BaseLogger.cs index fbd287750..16eb96e20 100644 --- a/MediaBrowser.Common/Logging/BaseLogger.cs +++ b/MediaBrowser.Common/Logging/BaseLogger.cs @@ -79,6 +79,10 @@ namespace MediaBrowser.Common.Logging LogEntry(row); } + protected virtual void Flush() + { + } + public virtual void Dispose() { } diff --git a/MediaBrowser.Common/Logging/StreamLogger.cs b/MediaBrowser.Common/Logging/StreamLogger.cs index 058c7e69c..caeb803bd 100644 --- a/MediaBrowser.Common/Logging/StreamLogger.cs +++ b/MediaBrowser.Common/Logging/StreamLogger.cs @@ -7,7 +7,7 @@ namespace MediaBrowser.Common.Logging /// /// Provides a Logger that can write to any Stream /// - public class StreamLogger : BaseLogger + public class StreamLogger : ThreadedLogger { private Stream Stream { get; set; } @@ -17,7 +17,7 @@ namespace MediaBrowser.Common.Logging Stream = stream; } - protected override void LogEntry(LogRow row) + protected override void AsyncLogMessage(LogRow row) { byte[] bytes = new UTF8Encoding().GetBytes(row.ToString() + Environment.NewLine); Stream.Write(bytes, 0, bytes.Length); diff --git a/MediaBrowser.Common/Logging/ThreadedLogger.cs b/MediaBrowser.Common/Logging/ThreadedLogger.cs new file mode 100644 index 000000000..8713ac224 --- /dev/null +++ b/MediaBrowser.Common/Logging/ThreadedLogger.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; + +namespace MediaBrowser.Common.Logging +{ + public abstract class ThreadedLogger : BaseLogger + { + Thread loggingThread; + Queue queue = new Queue(); + AutoResetEvent hasNewItems = new AutoResetEvent(false); + volatile bool terminate = false; + bool waiting = false; + + public ThreadedLogger() + : base() + { + loggingThread = new Thread(new ThreadStart(ProcessQueue)); + loggingThread.IsBackground = true; + loggingThread.Start(); + } + + + void ProcessQueue() + { + while (!terminate) + { + waiting = true; + hasNewItems.WaitOne(10000, true); + waiting = false; + + Queue queueCopy; + lock (queue) + { + queueCopy = new Queue(queue); + queue.Clear(); + } + + foreach (var log in queueCopy) + { + log(); + } + } + } + + protected override void LogEntry(LogRow row) + { + lock (queue) + { + queue.Enqueue(() => AsyncLogMessage(row)); + } + hasNewItems.Set(); + } + + protected abstract void AsyncLogMessage(LogRow row); + + protected override void Flush() + { + while (!waiting) + { + Thread.Sleep(1); + } + } + + public override void Dispose() + { + Flush(); + terminate = true; + hasNewItems.Set(); + base.Dispose(); + } + } +} diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index ed46a0e26..1ea8ba4a4 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -83,6 +83,7 @@ +