Skip to content

Commit

Permalink
Add message type to log scope
Browse files Browse the repository at this point in the history
  • Loading branch information
jkulubya committed Dec 4, 2024
1 parent 581d651 commit ed7f591
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
5 changes: 1 addition & 4 deletions QuickFIXn/Logger/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
throw new NotImplementedException();
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;

public void Dispose()
{
Expand Down
32 changes: 27 additions & 5 deletions QuickFIXn/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,22 @@ public virtual bool Send(Message message)
/// Sends a message
/// </summary>
/// <param name="message"></param>
/// <param name="messageType"></param>
/// <returns></returns>
public bool Send(string message)
public bool Send(string message, string messageType)
{
lock (_sync)
{
if (_responder is null)
return false;
Log.Log(LogLevel.Information, LogEventIds.OutgoingMessage, "{Message}", message);
using (Log.BeginScope(new Dictionary<string, object>
{
{"MessageType", messageType}
}))
{
Log.Log(LogLevel.Information, LogEventIds.OutgoingMessage, "{Message}", message);
}

return _responder.Send(message);
}
}
Expand Down Expand Up @@ -513,7 +521,21 @@ public void Next(string msgStr)
/// <param name="msgStr"></param>
private void NextMessage(string msgStr)
{
Log.Log(LogLevel.Information, LogEventIds.IncomingMessage, "{Message}", msgStr);
try
{
var messageType = Message.IdentifyType(msgStr);
using (Log.BeginScope(new Dictionary<string, object>
{
{"MessageType", messageType.Value}
}))
{
Log.Log(LogLevel.Information, LogEventIds.IncomingMessage, "{Message}", msgStr);
}
}
catch (Exception)
{
Log.Log(LogLevel.Information, LogEventIds.IncomingMessage, "{Message}", msgStr);
}

MessageBuilder msgBuilder = new MessageBuilder(
msgStr,
Expand Down Expand Up @@ -789,7 +811,7 @@ protected void NextResendRequest(Message resendReq)
{
GenerateSequenceReset(resendReq, begin, msgSeqNum);
}
Send(msg.ConstructString());
Send(msg.ConstructString(), msg.Header.GetString(Tags.MsgType));
begin = 0;
}
current = msgSeqNum + 1;
Expand Down Expand Up @@ -1572,7 +1594,7 @@ protected bool SendRaw(Message message, SeqNumType seqNum)
string messageString = message.ConstructString();
if (0 == seqNum)
Persist(message, messageString);
return Send(messageString);
return Send(messageString, message.Header.GetString(Tags.MsgType));
}
}

Expand Down

0 comments on commit ed7f591

Please sign in to comment.