Fixed default logger behaviour on closing
This commit is contained in:
parent
defa69f03b
commit
66ba0cf287
|
@ -37,7 +37,7 @@ namespace SweetLib.Utils.Logger
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The default <see cref="ILogMemory"/> which will be used for any logging action, if no custom <see cref="ILogMemory"/> is set as parameter.
|
/// The default <see cref="ILogMemory"/> which will be used for any logging action, if no custom <see cref="ILogMemory"/> is set as parameter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static ILogMemory DefaultLogMemory = new ArchivableConsoleLogMemory();
|
public static ILogMemory DefaultLogMemory { get; set; } = new ArchivableConsoleLogMemory();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Will log a message into the global <see cref="DefaultLogMemory"/>.
|
/// Will log a message into the global <see cref="DefaultLogMemory"/>.
|
||||||
|
|
|
@ -8,7 +8,7 @@ using SweetLib.Utils.Logger.Message;
|
||||||
|
|
||||||
namespace SweetLib.Utils.Logger.Memory
|
namespace SweetLib.Utils.Logger.Memory
|
||||||
{
|
{
|
||||||
public class ArchivableConsoleLogMemory : ILogMemory
|
public class ArchivableConsoleLogMemory : ILogMemory, IDisposable
|
||||||
{
|
{
|
||||||
private string TempFile { get; } = Path.GetTempFileName();
|
private string TempFile { get; } = Path.GetTempFileName();
|
||||||
|
|
||||||
|
@ -26,40 +26,54 @@ namespace SweetLib.Utils.Logger.Memory
|
||||||
{
|
{
|
||||||
ArchiveFile = archiveFile;
|
ArchiveFile = archiveFile;
|
||||||
|
|
||||||
QueueTimer = new Timer(e => ProcessQueue(), null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
|
QueueTimer = new Timer(e =>
|
||||||
|
{
|
||||||
|
if (Monitor.TryEnter(this))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ProcessQueue();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Monitor.Exit(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
~ArchivableConsoleLogMemory()
|
~ArchivableConsoleLogMemory()
|
||||||
{
|
{
|
||||||
Dispose(false);
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessQueue(bool isDisposing = false)
|
private void ProcessQueue(bool disposing = false)
|
||||||
{
|
{
|
||||||
|
// quick exit
|
||||||
if (LogQueue.IsEmpty)
|
if (LogQueue.IsEmpty)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// if we are disposing no need to lock. This might cause issues!
|
if (!disposing)
|
||||||
if (!isDisposing)
|
|
||||||
{
|
{
|
||||||
lock (this)
|
lock (this)
|
||||||
{
|
{
|
||||||
if (LogQueue.IsEmpty)
|
while (!LogQueue.IsEmpty)
|
||||||
return;
|
{
|
||||||
|
|
||||||
LogMessage message;
|
LogMessage message;
|
||||||
if (LogQueue.TryDequeue(out message))
|
if (LogQueue.TryDequeue(out message))
|
||||||
File.AppendAllText(TempFile, message.ToString());
|
File.AppendAllText(TempFile, message.ToString() + Environment.NewLine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (LogQueue.IsEmpty)
|
while (!LogQueue.IsEmpty)
|
||||||
return;
|
{
|
||||||
|
|
||||||
LogMessage message;
|
LogMessage message;
|
||||||
if (LogQueue.TryDequeue(out message))
|
if (LogQueue.TryDequeue(out message))
|
||||||
File.AppendAllText(TempFile, message.ToString());
|
File.AppendAllText(TempFile, message.ToString() + Environment.NewLine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,8 +109,6 @@ namespace SweetLib.Utils.Logger.Memory
|
||||||
|
|
||||||
|
|
||||||
public void Forget(LogMessage message)
|
public void Forget(LogMessage message)
|
||||||
{
|
|
||||||
lock (this)
|
|
||||||
{
|
{
|
||||||
var lines = File.ReadAllLines(TempFile).ToList();
|
var lines = File.ReadAllLines(TempFile).ToList();
|
||||||
foreach (var line in lines)
|
foreach (var line in lines)
|
||||||
|
@ -107,7 +119,6 @@ namespace SweetLib.Utils.Logger.Memory
|
||||||
|
|
||||||
File.WriteAllLines(TempFile, lines);
|
File.WriteAllLines(TempFile, lines);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void Archive(string fullFileName = null)
|
public void Archive(string fullFileName = null)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue