From a4ba1b2c6a53030fb56797b238fee8e93c57b742 Mon Sep 17 00:00:00 2001 From: Jawad Date: Tue, 7 May 2024 19:55:55 +0500 Subject: [PATCH] Added RollingFileLogFactory to support logging in separate files, allowing for improved organization and management of log data. --- QuickFIXn/Logger/FileLog.cs | 22 ++++++++++++-- QuickFIXn/Logger/RollingFileLogFactory.cs | 35 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 QuickFIXn/Logger/RollingFileLogFactory.cs diff --git a/QuickFIXn/Logger/FileLog.cs b/QuickFIXn/Logger/FileLog.cs index d7addf67a..cfb602428 100755 --- a/QuickFIXn/Logger/FileLog.cs +++ b/QuickFIXn/Logger/FileLog.cs @@ -25,7 +25,14 @@ public class FileLog : ILog /// path separator (i.e. "/" will become "\" on windows, else "\" will become "/" on all other platforms) /// /// - public FileLog(string fileLogPath, SessionID sessionId) + public FileLog(string fileLogPath, SessionID sessionId) : this(fileLogPath, sessionId, false) + { + } + /// + /// + /// + /// + public FileLog(string fileLogPath, SessionID sessionId, bool createDailyLogFile) { string prefix = Prefix(sessionId); @@ -34,8 +41,17 @@ public FileLog(string fileLogPath, SessionID sessionId) if (!System.IO.Directory.Exists(normalizedPath)) System.IO.Directory.CreateDirectory(normalizedPath); - _messageLogFileName = System.IO.Path.Combine(normalizedPath, prefix + ".messages.current.log"); - _eventLogFileName = System.IO.Path.Combine(normalizedPath, prefix + ".event.current.log"); + if (createDailyLogFile) + { + string currentDate = DateTime.UtcNow.ToString("yyyyMMdd"); + _messageLogFileName = System.IO.Path.Combine(fileLogPath, $"{currentDate}.{prefix}.messages.log"); + _eventLogFileName = System.IO.Path.Combine(fileLogPath, $"{currentDate}.{prefix}.event.log"); + } + else + { + _messageLogFileName = System.IO.Path.Combine(normalizedPath, prefix + ".messages.current.log"); + _eventLogFileName = System.IO.Path.Combine(normalizedPath, prefix + ".event.current.log"); + } _messageLog = new System.IO.StreamWriter(_messageLogFileName, true); _eventLog = new System.IO.StreamWriter(_eventLogFileName, true); diff --git a/QuickFIXn/Logger/RollingFileLogFactory.cs b/QuickFIXn/Logger/RollingFileLogFactory.cs new file mode 100644 index 000000000..d789bab16 --- /dev/null +++ b/QuickFIXn/Logger/RollingFileLogFactory.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace QuickFix.Logger +{ + /// + /// Creates a message store that stores messages in a file separated by date + /// + public class RollingFileLogFactory : ILogFactory + { + private readonly SessionSettings _settings; + + #region LogFactory Members + + public RollingFileLogFactory(SessionSettings settings) + { + _settings = settings; + } + + /// + /// Creates a file-based message store separated by date + /// + /// session ID for the message store + /// + public ILog Create(SessionID sessionId) + { + return new FileLog(_settings.Get(sessionId).GetString(SessionSettings.FILE_LOG_PATH), sessionId,true); + } + + #endregion + } +}