Mel
Mel

Reputation: 60

WCF Error Handler Exception Logging

I use IErrorHandler in my project for handle exceptions.

But how can i log incoming method parameter with exception. I want to get Request parameter for logging.

Sample Method:

public Response GetData(Request request) {
    return new Response();
}

Upvotes: 1

Views: 1249

Answers (3)

Xs10tial
Xs10tial

Reputation: 143

Two ways:

  1. The native WCF logger will capture all requests & responses when set to verbose however, these files tend to get real big, real quick.

  2. Use log4net (search google to download)

    private static readonly ILog log = LogManager.GetLogger(typeof(MyClass));

    public class MyClass
    

    { ...

    public Foo DoSomething(string arg)
    {
        try
        {
           //do something
        }
        catch(Exception e)
        {
           log.error(string.format("{0} Arguements: {1}", e.Tostring(), arg);
        }
    }
    

    }

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

You don't have such information in IErrorHandler - you can only parse raw message in ProvideFault method.

You can try to use another approach - implement custom IOperationInvoker and in Invoke method do something like:

// Just synchronous implementation - for asynchronous handle InvokeBegin and InvokeEnd
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
  try
  {
    // Run common invoker - you will create new Invoker as decorator for existing one.
    return innerInvoker.Invoke(instance, inputs, outputs);
  }
  catch(Exception e)
  {
    // Handle error here
  }
}

Operation invoker is responsible for selection correct operation in service and ivoke it. It is just idea - I haven't tested it.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039338

You could get the request message like this:

Message requestMessage = OperationContext.Current.RequestContext.RequestMessage;

What I do usually is to log the entire request XML.

Upvotes: 4

Related Questions