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 Re开发者_如何转开发sponse GetData(Request request) {
return new Response();
}
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.
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.
Two ways:
The native WCF logger will capture all requests & responses when set to verbose however, these files tend to get real big, real quick.
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); } }
}
精彩评论