Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Log Exactly The Messages Sent and Received From a WCF Client

Tags:

.net

logging

wcf

Please do not answer using the WCF Trace tool unless give explicit instructions on how to capture the actual message including headers and faults. This link does not work.

Also, do not answer IClientMessageInspector unless you know how to get it to include all headers (which it doesn't) and capture responses that have fault elements that don't parse.

With pre-wcf web services, you could write a SoapExtension that worked flawlessly.

like image 401
JohnOpincar Avatar asked May 22 '12 14:05

JohnOpincar


People also ask

How do I check WCF logs?

Viewing Event Logs. Event logging is enabled automatically by default, and there is no mechanism to disable it. Events logged by WCF can be viewed using the Event Viewer.

How do I enable tracing and message logging in WCF?

Windows Communication Foundation (WCF) does not log messages by default. To activate message logging, you must add a trace listener to the System. ServiceModel. MessageLogging trace source and set attributes for the <messagelogging> element in the configuration file.

How can I trace a WCF service call?

Tracing is not enabled by default. To activate tracing, you must create a trace listener and set a trace level other than "Off" for the selected trace source in configuration; otherwise, WCF does not generate any traces. If you do not specify a listener, tracing is automatically disabled.

How will you specify a method is available to access by client in WCF?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.


3 Answers

write a custom message encoder. it has access to all headers. deoending on how generic you want your solution to be you may need to write it such that it gets in the ctor the real encoder.

just a few days ago I implemented a "Wrapper encoder" in this thread. that encoder changed the message. you don't need to do this, you can just log it and pass it to the transport as I also did.

like image 80
Yaron Naveh Avatar answered Sep 22 '22 05:09

Yaron Naveh


A class implementing IEndpointBehavior allows you to trap and log inbound/outbound messages.

See an example here http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.applydispatchbehavior.aspx

You'll also need a class implementing IDispatchMessageInspector

like image 26
lcryder Avatar answered Sep 22 '22 05:09

lcryder


I found this as well:

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="messages"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData="c:\log\wcfMessages.svclog" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>

<system.serviceModel>
    <diagnostics>
        <messageLogging
                 logEntireMessage="true"
                 logMalformedMessages="true"
                 logMessagesAtServiceLevel="true"
                 logMessagesAtTransportLevel="true"
                 maxMessagesToLog="1000000"
                 maxSizeOfMessageToLog="10000000"/>
    </diagnostics>
</system.serviceModel>

It's not ideal since you have to use a tool to view the messages but it does seem to capture the actual messages with all headers and faults, etc.

like image 33
JohnOpincar Avatar answered Sep 23 '22 05:09

JohnOpincar