Calling ToString() on a generated message type normally
returns the JSON representation. If a message type implements this interface,
then the ToDiagnosticString() method will be called instead of the regular
JSON formatting code, but only when ToString() is called either on the message itself
or on another message which contains it. This does not affect the normal JSON formatting of
the message.
For example, if you create a proto message representing a GUID, the internal
representation may be a bytes field or four fixed32 fields. However, when debugging
it may be more convenient to see a result in the same format as Guid provides.
This interface extends IMessage to avoid it accidentally being implemented
on types other than messages, where it would not be used by anything in the framework.
Methods
ToDiagnosticString()
string ToDiagnosticString()
Returns a string representation of this object, for diagnostic purposes.
A string representation of this object, for diagnostic purposes.
Remarks
This method is called when a message is formatted as part of a ToString()
call. It does not affect the JSON representation used by JsonFormatter other than
in calls to ToDiagnosticString(IMessage). While it is recommended
that the result is valid JSON, this is never assumed by the Protobuf library.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003e\u003ccode\u003eICustomDiagnosticMessage\u003c/code\u003e is an interface for message types that require a custom string format for diagnostic outputs, extending the \u003ccode\u003eIMessage\u003c/code\u003e interface.\u003c/p\u003e\n"],["\u003cp\u003eImplementing \u003ccode\u003eICustomDiagnosticMessage\u003c/code\u003e allows a message to provide a custom string representation via the \u003ccode\u003eToDiagnosticString()\u003c/code\u003e method, replacing the default JSON representation when \u003ccode\u003eToString()\u003c/code\u003e is invoked.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eToDiagnosticString()\u003c/code\u003e method provides a specialized string output for debugging, which can differ from the standard JSON formatting and is only engaged when \u003ccode\u003eToString()\u003c/code\u003e is called directly or indirectly.\u003c/p\u003e\n"],["\u003cp\u003eThis interface does not change how the Protobuf library handles normal JSON formatting, only affecting diagnostic string outputs and making it more convenient for users to debug their messages.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eICustomDiagnosticMessage\u003c/code\u003e interface offers methods and extensions such as MergeFrom and WriteTo that can be used to manipulate the message.\u003c/p\u003e\n"]]],[],null,["# Interface ICustomDiagnosticMessage (3.27.1)\n\nVersion latestkeyboard_arrow_down\n\n- [3.27.1 (latest)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ICustomDiagnosticMessage)\n- [3.23.0](/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.ICustomDiagnosticMessage)\n- [3.15.8](/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.ICustomDiagnosticMessage) \n\n public interface ICustomDiagnosticMessage : IMessage\n\nA message type that has a custom string format for diagnostic purposes. \n\nInherited Members\n-----------------\n\n[IMessage.MergeFrom(CodedInputStream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage#Google_Protobuf_IMessage_MergeFrom_Google_Protobuf_CodedInputStream_) \n[IMessage.WriteTo(CodedOutputStream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage#Google_Protobuf_IMessage_WriteTo_Google_Protobuf_CodedOutputStream_) \n[IMessage.CalculateSize()](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage#Google_Protobuf_IMessage_CalculateSize) \n[IMessage.Descriptor](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage#Google_Protobuf_IMessage_Descriptor)\n\nNamespace\n---------\n\n[Google.Protobuf](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)\n\nAssembly\n--------\n\nGoogle.Protobuf.dll\n\nRemarks\n-------\n\n\nCalling [ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring) on a generated message type normally\nreturns the JSON representation. If a message type implements this interface,\nthen the [ToDiagnosticString()](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ICustomDiagnosticMessage#Google_Protobuf_ICustomDiagnosticMessage_ToDiagnosticString) method will be called instead of the regular\nJSON formatting code, but only when `ToString()` is called either on the message itself\nor on another message which contains it. This does not affect the normal JSON formatting of\nthe message.\n\n\nFor example, if you create a proto message representing a GUID, the internal\nrepresentation may be a `bytes` field or four `fixed32` fields. However, when debugging\nit may be more convenient to see a result in the same format as [Guid](https://learn.microsoft.com/dotnet/api/system.guid) provides.\n\nThis interface extends [IMessage](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IMessage) to avoid it accidentally being implemented\non types other than messages, where it would not be used by anything in the framework.\n\nMethods\n-------\n\n### ToDiagnosticString()\n\n string ToDiagnosticString()\n\nReturns a string representation of this object, for diagnostic purposes.\n\n**Remarks** \nThis method is called when a message is formatted as part of a [ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)\ncall. It does not affect the JSON representation used by [JsonFormatter](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter) other than\nin calls to [ToDiagnosticString(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.JsonFormatter#Google_Protobuf_JsonFormatter_ToDiagnosticString_Google_Protobuf_IMessage_). While it is recommended\nthat the result is valid JSON, this is never assumed by the Protobuf library.\n\nExtension Methods\n-----------------\n\n[MessageExtensions.IsInitialized(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_IsInitialized_Google_Protobuf_IMessage_) \n[MessageExtensions.MergeDelimitedFrom(IMessage, Stream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeDelimitedFrom_Google_Protobuf_IMessage_System_IO_Stream_) \n[MessageExtensions.MergeFrom(IMessage, ByteString)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_Google_Protobuf_ByteString_) \n[MessageExtensions.MergeFrom(IMessage, ReadOnlySequence\\\u003cbyte\\\u003e)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_System_Buffers_ReadOnlySequence_System_Byte__) \n[MessageExtensions.MergeFrom(IMessage, byte\\[\\])](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_System_Byte___) \n[MessageExtensions.MergeFrom(IMessage, byte\\[\\], int, int)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_System_Byte___System_Int32_System_Int32_) \n[MessageExtensions.MergeFrom(IMessage, Stream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_System_IO_Stream_) \n[MessageExtensions.MergeFrom(IMessage, ReadOnlySpan\\\u003cbyte\\\u003e)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_MergeFrom_Google_Protobuf_IMessage_System_ReadOnlySpan_System_Byte__) \n[MessageExtensions.ToByteArray(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_ToByteArray_Google_Protobuf_IMessage_) \n[MessageExtensions.ToByteString(IMessage)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_ToByteString_Google_Protobuf_IMessage_) \n[MessageExtensions.WriteDelimitedTo(IMessage, Stream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_WriteDelimitedTo_Google_Protobuf_IMessage_System_IO_Stream_) \n[MessageExtensions.WriteTo(IMessage, IBufferWriter\\\u003cbyte\\\u003e)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_WriteTo_Google_Protobuf_IMessage_System_Buffers_IBufferWriter_System_Byte__) \n[MessageExtensions.WriteTo(IMessage, Stream)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_WriteTo_Google_Protobuf_IMessage_System_IO_Stream_) \n[MessageExtensions.WriteTo(IMessage, Span\\\u003cbyte\\\u003e)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageExtensions#Google_Protobuf_MessageExtensions_WriteTo_Google_Protobuf_IMessage_System_Span_System_Byte__)"]]