Browse Source

Logging improved.

pull/322/head
Sebastian 7 years ago
parent
commit
3c32bdc855
  1. 22
      src/Squidex.Infrastructure/Log/FileChannel.cs
  2. 27
      src/Squidex.Infrastructure/Log/Internal/FileLogProcessor.cs
  3. 3
      src/Squidex/Config/Domain/LoggingServices.cs

22
src/Squidex.Infrastructure/Log/FileChannel.cs

@ -9,9 +9,11 @@ using Squidex.Infrastructure.Log.Internal;
namespace Squidex.Infrastructure.Log namespace Squidex.Infrastructure.Log
{ {
public sealed class FileChannel : DisposableObjectBase, ILogChannel, IInitializable public sealed class FileChannel : DisposableObjectBase, ILogChannel
{ {
private readonly FileLogProcessor processor; private readonly FileLogProcessor processor;
private readonly object lockObject = new object();
private bool isInitialized;
public FileChannel(string path) public FileChannel(string path)
{ {
@ -30,12 +32,20 @@ namespace Squidex.Infrastructure.Log
public void Log(SemanticLogLevel logLevel, string message) public void Log(SemanticLogLevel logLevel, string message)
{ {
processor.EnqueueMessage(new LogMessageEntry { Message = message }); if (!isInitialized)
} {
lock (lockObject)
{
if (!isInitialized)
{
processor.Initialize();
public void Initialize() isInitialized = true;
{ }
processor.Connect(); }
}
processor.EnqueueMessage(new LogMessageEntry { Message = message });
} }
} }
} }

27
src/Squidex.Infrastructure/Log/Internal/FileLogProcessor.cs

@ -20,6 +20,7 @@ namespace Squidex.Infrastructure.Log.Internal
private readonly BlockingCollection<LogMessageEntry> messageQueue = new BlockingCollection<LogMessageEntry>(MaxQueuedMessages); private readonly BlockingCollection<LogMessageEntry> messageQueue = new BlockingCollection<LogMessageEntry>(MaxQueuedMessages);
private readonly Task outputTask; private readonly Task outputTask;
private readonly string path; private readonly string path;
private StreamWriter writer;
public FileLogProcessor(string path) public FileLogProcessor(string path)
{ {
@ -45,16 +46,33 @@ namespace Squidex.Infrastructure.Log.Internal
throw; throw;
} }
} }
finally
{
writer.Dispose();
}
} }
} }
public void Connect() public void Initialize()
{ {
var fileInfo = new FileInfo(path); var fileInfo = new FileInfo(path);
try
{
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
if (!fileInfo.Directory.Exists) var fs = new FileStream(fileInfo.FullName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
writer = new StreamWriter(fs, Encoding.UTF8);
writer.AutoFlush = true;
writer.WriteLine($"--- Started Logging {DateTime.UtcNow} ---", 1);
}
catch (Exception ex)
{ {
throw new ConfigurationException($"Log directory '{fileInfo.Directory.FullName}' does not exist."); throw new ConfigurationException($"Log directory '{fileInfo.Directory.FullName}' does not exist or cannot be created.", ex);
} }
} }
@ -71,8 +89,7 @@ namespace Squidex.Infrastructure.Log.Internal
{ {
try try
{ {
File.AppendAllText(path, entry.Message + Environment.NewLine, Encoding.UTF8); writer.WriteLine(entry.Message);
break; break;
} }
catch (Exception ex) catch (Exception ex)

3
src/Squidex/Config/Domain/LoggingServices.cs

@ -38,8 +38,7 @@ namespace Squidex.Config.Domain
if (!string.IsNullOrWhiteSpace(loggingFile)) if (!string.IsNullOrWhiteSpace(loggingFile))
{ {
services.AddSingletonAs(file ?? (file = new FileChannel(loggingFile))) services.AddSingletonAs(file ?? (file = new FileChannel(loggingFile)))
.As<ILogChannel>() .As<ILogChannel>();
.As<IInitializable>();
} }
var useColors = config.GetValue<bool>("logging:colors"); var useColors = config.GetValue<bool>("logging:colors");

Loading…
Cancel
Save