diff --git a/QuickFIXn/Session.cs b/QuickFIXn/Session.cs index 51c348046..d7e576d43 100755 --- a/QuickFIXn/Session.cs +++ b/QuickFIXn/Session.cs @@ -29,8 +29,7 @@ public class Session : IDisposable private readonly SessionState _state; private readonly IMessageFactory _msgFactory; private readonly bool _appDoesEarlyIntercept; - - private const LogLevel MessagesLogLevel = LogLevel.Information; + private readonly LogLevel _messagesLogLevel; #region Properties @@ -239,7 +238,8 @@ internal Session( int heartBtInt, IQuickFixLoggerFactory loggerFactory, IMessageFactory msgFactory, - string senderDefaultApplVerId) + string senderDefaultApplVerId, + LogLevel messagesLogLevel) { _schedule = sessionSchedule; _msgFactory = msgFactory; @@ -249,13 +249,14 @@ internal Session( SessionID = sessId; DataDictionaryProvider = new DataDictionaryProvider(dataDictProvider); SenderDefaultApplVerID = senderDefaultApplVerId; - + SessionDataDictionary = DataDictionaryProvider.GetSessionDataDictionary(SessionID.BeginString); ApplicationDataDictionary = SessionID.IsFIXT ? DataDictionaryProvider.GetApplicationDataDictionary(SenderDefaultApplVerID) : SessionDataDictionary; ILogger logger = loggerFactory.CreateSessionLogger(sessId); + _messagesLogLevel = messagesLogLevel; _state = new SessionState(isInitiator, logger, heartBtInt, storeFactory.Create(sessId)); @@ -368,14 +369,14 @@ public bool Send(string message) if (_responder is null) return false; - if (Log.IsEnabled(MessagesLogLevel)) + if (Log.IsEnabled(_messagesLogLevel)) { using (Log.BeginScope(new Dictionary { { "MessageType", Message.GetMsgType(message) } })) { - Log.Log(MessagesLogLevel, LogEventIds.OutgoingMessage, "{Message}", + Log.Log(_messagesLogLevel, LogEventIds.OutgoingMessage, "{Message}", LogAssist.RedactSensitiveFields(message, RedactFieldsInLogs, RedactionLogText)); } } @@ -542,20 +543,20 @@ private void NextMessage(string msgStr) { try { - if (Log.IsEnabled(MessagesLogLevel)) + if (Log.IsEnabled(_messagesLogLevel)) { using (Log.BeginScope(new Dictionary { { "MessageType", Message.GetMsgType(msgStr) } })) { - Log.Log(MessagesLogLevel, LogEventIds.IncomingMessage, "{Message}", + Log.Log(_messagesLogLevel, LogEventIds.IncomingMessage, "{Message}", LogAssist.RedactSensitiveFields(msgStr, RedactFieldsInLogs, RedactionLogText)); } } } catch (Exception) { - Log.Log(MessagesLogLevel, LogEventIds.IncomingMessage, "{Message}", + Log.Log(_messagesLogLevel, LogEventIds.IncomingMessage, "{Message}", LogAssist.RedactSensitiveFields(msgStr, RedactFieldsInLogs, RedactionLogText)); } diff --git a/QuickFIXn/SessionFactory.cs b/QuickFIXn/SessionFactory.cs index 29660ff7c..23aa8679b 100755 --- a/QuickFIXn/SessionFactory.cs +++ b/QuickFIXn/SessionFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Microsoft.Extensions.Logging; using QuickFix.Logger; using QuickFix.Store; using QuickFix.Util; @@ -99,6 +100,16 @@ public Session Create(SessionID sessionId, SettingsDictionary settings) if(defaultApplVerId is not null) senderDefaultApplVerId = defaultApplVerId.Value; + LogLevel messagesLogLevel = LogLevel.Information; + string? messagesLogLevelName = null; + if(settings.Has(SessionSettings.MESSAGES_LOG_LEVEL)) + { + messagesLogLevelName = settings.GetString(SessionSettings.MESSAGES_LOG_LEVEL); + if (!Enum.TryParse(messagesLogLevelName, true, out LogLevel logLevel)) + throw new ConfigError($"Invalid {SessionSettings.MESSAGES_LOG_LEVEL} value: {messagesLogLevelName}"); + messagesLogLevel = logLevel; + } + Session session = new Session( isInitiator, _application, @@ -109,7 +120,8 @@ public Session Create(SessionID sessionId, SettingsDictionary settings) heartBtInt, _loggerFactory, sessionMsgFactory, - senderDefaultApplVerId); + senderDefaultApplVerId, + messagesLogLevel); if (settings.Has("MillisecondsInTimeStamp")) { throw new ApplicationException( diff --git a/QuickFIXn/SessionSettings.cs b/QuickFIXn/SessionSettings.cs index 46e20c72d..39866cf4c 100755 --- a/QuickFIXn/SessionSettings.cs +++ b/QuickFIXn/SessionSettings.cs @@ -38,6 +38,7 @@ public class SessionSettings public const string SOCKET_CONNECT_PORT = "SocketConnectPort"; public const string RECONNECT_INTERVAL = "ReconnectInterval"; public const string FILE_LOG_PATH = "FileLogPath"; + public const string MESSAGES_LOG_LEVEL = "MessagesLogLevel"; public const string FILE_STORE_PATH = "FileStorePath"; public const string REFRESH_ON_LOGON = "RefreshOnLogon"; public const string RESET_ON_LOGON = "ResetOnLogon";