fix: Log files were empty & incorrect formatting

pull/76/head
Robert Dailey 3 years ago
parent 23c6871d41
commit bba4d2a08a

@ -1,13 +1,16 @@
using System.IO.Abstractions;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Display;
using TrashLib;
namespace Recyclarr.Logging;
public class DelayedFileSink : IDelayedFileSink
public sealed class DelayedFileSink : IDelayedFileSink
{
private readonly IAppPaths _paths;
private readonly Lazy<StreamWriter> _stream;
private ITextFormatter? _formatter;
public DelayedFileSink(IAppPaths paths, IFileSystem fs)
{
@ -26,7 +29,8 @@ public class DelayedFileSink : IDelayedFileSink
return;
}
_stream.Value.WriteLine(logEvent.RenderMessage());
_formatter?.Format(logEvent, _stream.Value);
_stream.Value.Flush();
}
public void Dispose()
@ -36,4 +40,9 @@ public class DelayedFileSink : IDelayedFileSink
_stream.Value.Close();
}
}
public void SetTemplate(string template)
{
_formatter = new MessageTemplateTextFormatter(template);
}
}

@ -4,4 +4,5 @@ namespace Recyclarr.Logging;
public interface IDelayedFileSink : ILogEventSink, IDisposable
{
void SetTemplate(string template);
}

@ -5,23 +5,26 @@ namespace Recyclarr.Logging;
public class LoggerFactory
{
private const string ConsoleTemplate = "[{Level:u3}] {Message:lj}{NewLine}{Exception}";
private readonly LoggingLevelSwitch _logLevel;
private readonly IDelayedFileSink _fileSink;
private readonly Func<IDelayedFileSink> _fileSinkFactory;
public LoggerFactory(LoggingLevelSwitch logLevel, IDelayedFileSink fileSink)
public LoggerFactory(LoggingLevelSwitch logLevel, Func<IDelayedFileSink> fileSinkFactory)
{
_logLevel = logLevel;
_fileSink = fileSink;
_fileSinkFactory = fileSinkFactory;
}
public ILogger Create()
{
const string consoleTemplate = "[{Level:u3}] {Message:lj}{NewLine}{Exception}";
var fileSink = _fileSinkFactory();
fileSink.SetTemplate(ConsoleTemplate);
return new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(outputTemplate: consoleTemplate, levelSwitch: _logLevel)
.WriteTo.Sink(_fileSink)
.WriteTo.Console(outputTemplate: ConsoleTemplate, levelSwitch: _logLevel)
.WriteTo.Sink(fileSink)
.CreateLogger();
}
}

Loading…
Cancel
Save