Logging message contents

Component: NServiceBus
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

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 :
    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