using System.Text; using NLog; using NLog.Targets; using OliverBooth.Services; namespace OliverBooth.Logging; /// /// Represents an NLog target which writes its output to a log file on disk. /// internal sealed class LogFileTarget : TargetWithLayout { private readonly LoggingService _loggingService; /// /// Initializes a new instance of the class. /// /// The name of the log target. /// The . public LogFileTarget(string name, LoggingService loggingService) { _loggingService = loggingService; Name = name; } /// protected override void Write(LogEventInfo logEvent) { _loggingService.ArchiveLogFilesAsync(false).GetAwaiter().GetResult(); using FileStream stream = _loggingService.LogFile.Open(FileMode.Append, FileAccess.Write); using var writer = new StreamWriter(stream, Encoding.UTF8); writer.Write(Layout.Render(logEvent)); if (logEvent.Exception is { } exception) writer.Write($": {exception}"); writer.WriteLine(); } }