Search code examples
c#wcfsoapasp.net-coresvcutil.exe

WCF client logging dotnet core


I'm using asp.net core on windows and have a file with classes generated by the dotnet-svcutil. I'm using nlog for the logging purpose. Is there a way I can log all the raw requests and responses to and from the external service?

Already tried logman https://github.com/dotnet/wcf/blob/master/Documentation/HowToUseETW.md, but first - it doesn't show the raw soap, only events, and second - I need logs to be logged by the configured nlog.


Solution

  • Found the answer here: https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-messages-on-the-client

    The order of actions:

    1. Implement the System.ServiceModel.Dispatcher.IClientMessageInspector interface. Here you can inspect/modify/log messages
    2. Implement a System.ServiceModel.Description.IEndpointBehavior or System.ServiceModel.Description.IContractBehavior depending upon the scope at which you want to insert the client message inspector. System.ServiceModel.Description.IEndpointBehavior allows you to change behavior at the endpoint level. System.ServiceModel.Description.IContractBehavior allows you to change behavior at the contract level.
    3. Insert the behavior prior to calling the ClientBase.Open or the ICommunicationObject.Open method on the System.ServiceModel.ChannelFactory.