Logging message contents

Component: NServiceBus
NuGet NServiceBus (6.x)

When NServiceBus sends a message, it writes the result of the ToString() method of the message class to the log. By default, this writes the name of the message type only. To write the full message contents to the log, override the ToString() method of the relevant message class. Here's an example:

public class MessageToLog :
    IMessage
{
    public Guid EventId { get; set; }
    public DateTime? Time { get; set; }
    public TimeSpan Duration { get; set; }

    public override string ToString()
    {
        return $"MyMessage: EventId={EventId}, Time={Time}, Duration={Duration}";
    }
}
NServiceBus only makes these calls at a log threshold of DEBUG or lower.

Last modified