Probably trivial question.. I want to implement my own error handler to log errors and monitor what's going on. At this point I don't want to provide my own faults to the clients. I want it to be transparent - just like default WCF behavior. How should I implement ProvideFault
to achieve this?
可能微不足道的问题. .我想实现我自己的错误处理程序来记录错误并监视正在发生的事情。此时,我不想向客户提供我自己的错误。我希望它是透明的——就像默认的WCF行为一样。我应该如何实现ProvideFault来实现这一点?
namespace IDATT.Web.Services
{
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
public class MyServiceErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// ????
}
public bool HandleError(Exception error)
{
return true;
}
}
}
2 个解决方案
#1
2
You can leave it empty. I do this to log errors to Elmah with no issues.
你可以让它空着。我这样做是为了将错误记录到Elmah,没有任何问题。
EDIT
编辑
I'm completely wrong on this. After looking at my implementation I do the following. As you can see, HandleError
is the method that is basically empty and the logging takes place in ProvideFault
.
我完全错了。在查看我的实现之后,我将执行以下操作。如您所见,HandleError是基本为空的方法,而日志记录是在ProvideFault中进行的。
public class ElmahErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error == null)
{
return;
}
ErrorSignal.FromCurrentContext().Raise(error);
}
}
Returning true
in HandleError
will make WCF think that no error occured. That means if you are just logging the error, this should always return false
. If the error is not handled, ProvideFault
is called and this is where you want to do the logging. You do not need to provide a fault Message.
在HandleError中返回true将使WCF认为没有发生错误。这意味着如果您只是记录错误,那么它应该总是返回false。如果错误没有被处理,则会调用ProvideFault,这是您希望进行日志记录的地方。您不需要提供错误消息。
#2
2
From the documentation (http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx):
从文档(http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx):
Implement the HandleError method to ensure error-related behaviors, including error logging, assuring a fail fast, shutting down the application, and so on.
实现HandleError方法以确保与错误相关的行为,包括错误日志记录、确保快速失败、关闭应用程序等等。
Also the link above notes that only HandleError is called after the response has been sent back to the client. So be nice to your client (don't make them wait while you log), leave ProvideFault blank and perform logging operations in HandleError.
此外,上面的链接还指出,只有在响应发送回客户端之后才调用HandleError。所以要善待你的客户端(不要让他们在你登录时等待),在HandleError中留下ProvideFault空白并执行日志操作。
#1
2
You can leave it empty. I do this to log errors to Elmah with no issues.
你可以让它空着。我这样做是为了将错误记录到Elmah,没有任何问题。
EDIT
编辑
I'm completely wrong on this. After looking at my implementation I do the following. As you can see, HandleError
is the method that is basically empty and the logging takes place in ProvideFault
.
我完全错了。在查看我的实现之后,我将执行以下操作。如您所见,HandleError是基本为空的方法,而日志记录是在ProvideFault中进行的。
public class ElmahErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error == null)
{
return;
}
ErrorSignal.FromCurrentContext().Raise(error);
}
}
Returning true
in HandleError
will make WCF think that no error occured. That means if you are just logging the error, this should always return false
. If the error is not handled, ProvideFault
is called and this is where you want to do the logging. You do not need to provide a fault Message.
在HandleError中返回true将使WCF认为没有发生错误。这意味着如果您只是记录错误,那么它应该总是返回false。如果错误没有被处理,则会调用ProvideFault,这是您希望进行日志记录的地方。您不需要提供错误消息。
#2
2
From the documentation (http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx):
从文档(http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx):
Implement the HandleError method to ensure error-related behaviors, including error logging, assuring a fail fast, shutting down the application, and so on.
实现HandleError方法以确保与错误相关的行为,包括错误日志记录、确保快速失败、关闭应用程序等等。
Also the link above notes that only HandleError is called after the response has been sent back to the client. So be nice to your client (don't make them wait while you log), leave ProvideFault blank and perform logging operations in HandleError.
此外,上面的链接还指出,只有在响应发送回客户端之后才调用HandleError。所以要善待你的客户端(不要让他们在你登录时等待),在HandleError中留下ProvideFault空白并执行日志操作。