diff --git a/MediaBrowser.Api/ApiService.cs b/MediaBrowser.Api/ApiService.cs
index 06929de73a..a439a88d2a 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 fbd2877503..16eb96e208 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 058c7e69c5..caeb803bd1 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 0000000000..8713ac2248
--- /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 ed46a0e262..1ea8ba4a47 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -83,6 +83,7 @@
+