Codehelp
Codehelp

Reputation: 4747

Logging the request received by a WCF service

is there a way to log the incoming request to a WCF service?

I have a simple WCF service hosted as Windows Service. The WCF service writes logs on a daily basis. Some times there a bad-requests coming in and I would want to log then too.

Is there a way to log the SOAP request that comes in? That way it would help to show the client what mistakes they are making.

Regards.

Upvotes: 2

Views: 5757

Answers (3)

sbp
sbp

Reputation: 963

I'd like to add to @RoelF 's response since he mentioned below

Although I'm not sure how to exactly log the content of the SOAP messages, this method can provide you with a lot of extra info, expecially if you implement a timed logging.

So here is the line you'd use to get the SOAP request from those AfterCall BeforeCall methods.

Message message = OperationContext.Current.RequestContext.RequestMessage;

Then you could log message.ToString() . This should log the SOAP request that you are after.

Upvotes: 3

krembanan
krembanan

Reputation: 1428

You can configure tracing and level of tracing in the configuration file of your service. To view the log files, Microsoft has supplied a special trace log viewer. Normally you can just double click the trace files to open them in this viewer.

This has been answered many times on SO, example How to turn on WCF tracing?

Documentation on MSDN http://msdn.microsoft.com/en-us/library/ms733025.aspx

Sample configuration

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true" >
        <listeners>
             <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource"
              switchValue="Information, ActivityTracing">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
        <add name="xml"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="Error.svclog" />
    </sharedListeners>
  </system.diagnostics>
</configuration>

Upvotes: 1

RoelF
RoelF

Reputation: 7573

You could try to implement an IServiceBehavior and an IParameterInspector class, which will handle the logging.

IServiceBehavior
The ApplyDispatchBehavior is the most important method to implement:

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            foreach (var endpoint in dispatcher.Endpoints)
            {
                foreach (var dispatchOperation in endpoint.DispatchRuntime.Operations)
                {
                    dispatchOperation.ParameterInspectors.Add(new LoggingParameterInspector(/*reference to your logger*/));
                }
            }
        }
    }

You have to apply this ServiceBehaviorto your service, either by making it an Attribute on your service implementation class, or by adding it to the definition at startup time.

IParameterInspector
this interface has two methods: BeforeCall and AfterCall. In the implementation of these methods you can access the operation name and the inputs given to the method. This allows you to create a clean and simple trace of all calls to the service.

Although I'm not sure how to exactly log the content of the SOAP messages, this method can provide you with a lot of extra info, expecially if you implement a timed logging.

Upvotes: 2

Related Questions