aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Logging/SimpleLogManager.cs
diff options
context:
space:
mode:
authorstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
committerstefan <stefan@hegedues.at>2018-09-12 19:26:21 +0200
commit48facb797ed912e4ea6b04b17d1ff190ac2daac4 (patch)
tree8dae77a31670a888d733484cb17dd4077d5444e8 /Emby.Server.Implementations/Logging/SimpleLogManager.cs
parentc32d8656382a0eacb301692e0084377fc433ae9b (diff)
Update to 3.5.2 and .net core 2.1
Diffstat (limited to 'Emby.Server.Implementations/Logging/SimpleLogManager.cs')
-rw-r--r--Emby.Server.Implementations/Logging/SimpleLogManager.cs83
1 files changed, 71 insertions, 12 deletions
diff --git a/Emby.Server.Implementations/Logging/SimpleLogManager.cs b/Emby.Server.Implementations/Logging/SimpleLogManager.cs
index 6129f38c4..390814c34 100644
--- a/Emby.Server.Implementations/Logging/SimpleLogManager.cs
+++ b/Emby.Server.Implementations/Logging/SimpleLogManager.cs
@@ -31,7 +31,7 @@ namespace Emby.Server.Implementations.Logging
return new NamedLogger(name, this);
}
- public void ReloadLogger(LogSeverity severity)
+ public async Task ReloadLogger(LogSeverity severity, CancellationToken cancellationToken)
{
LogSeverity = severity;
@@ -39,19 +39,23 @@ namespace Emby.Server.Implementations.Logging
if (logger != null)
{
logger.Dispose();
+ await TryMoveToArchive(logger.Path, cancellationToken).ConfigureAwait(false);
}
- var path = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
+ var newPath = Path.Combine(LogDirectory, LogFilePrefix + ".txt");
- _fileLogger = new FileLogger(path);
+ if (File.Exists(newPath))
+ {
+ newPath = await TryMoveToArchive(newPath, cancellationToken).ConfigureAwait(false);
+ }
+
+ _fileLogger = new FileLogger(newPath);
if (LoggerLoaded != null)
{
try
{
-
LoggerLoaded(this, EventArgs.Empty);
-
}
catch (Exception ex)
{
@@ -60,6 +64,42 @@ namespace Emby.Server.Implementations.Logging
}
}
+ private async Task<string> TryMoveToArchive(string file, CancellationToken cancellationToken, int retryCount = 0)
+ {
+ var archivePath = GetArchiveFilePath();
+
+ try
+ {
+ File.Move(file, archivePath);
+
+ return file;
+ }
+ catch (FileNotFoundException)
+ {
+ return file;
+ }
+ catch (DirectoryNotFoundException)
+ {
+ return file;
+ }
+ catch
+ {
+ if (retryCount >= 50)
+ {
+ return GetArchiveFilePath();
+ }
+
+ await Task.Delay(100, cancellationToken).ConfigureAwait(false);
+
+ return await TryMoveToArchive(file, cancellationToken, retryCount + 1).ConfigureAwait(false);
+ }
+ }
+
+ private string GetArchiveFilePath()
+ {
+ return Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
+ }
+
public event EventHandler LoggerLoaded;
public void Flush()
@@ -104,10 +144,12 @@ namespace Emby.Server.Implementations.Logging
if (logger != null)
{
logger.Dispose();
+
+ var task = TryMoveToArchive(logger.Path, CancellationToken.None);
+ Task.WaitAll(task);
}
_fileLogger = null;
- GC.SuppressFinalize(this);
}
}
@@ -119,9 +161,13 @@ namespace Emby.Server.Implementations.Logging
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly BlockingCollection<string> _queue = new BlockingCollection<string>();
+ public string Path { get; set; }
+
public FileLogger(string path)
{
- Directory.CreateDirectory(Path.GetDirectoryName(path));
+ Path = path;
+
+ Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
_fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 32768);
_cancellationTokenSource = new CancellationTokenSource();
@@ -144,6 +190,10 @@ namespace Emby.Server.Implementations.Logging
}
_fileStream.Write(bytes, 0, bytes.Length);
+ if (_disposed)
+ {
+ return;
+ }
_fileStream.Flush(true);
}
@@ -177,13 +227,22 @@ namespace Emby.Server.Implementations.Logging
public void Dispose()
{
- _cancellationTokenSource.Cancel();
-
- Flush();
+ if (_disposed)
+ {
+ return;
+ }
_disposed = true;
- _fileStream.Dispose();
- GC.SuppressFinalize(this);
+ _cancellationTokenSource.Cancel();
+
+ var stream = _fileStream;
+ if (stream != null)
+ {
+ using (stream)
+ {
+ stream.Flush(true);
+ }
+ }
}
}