ServiceStack - 如何禁用默认的异常记录 [英] ServiceStack - how to disable default exception logging
问题描述
在与的ServiceStack文档线,我们有一个全球性的服务异常处理程序。该文件说,这一处理程序应然后登录异常调用 DtoUtils.HandleException
,就像这样:
In line with the ServiceStack documentation, we have a global service exception handler. The docs say that this handler should log the exception then call DtoUtils.HandleException
, like this:
private object LogServiceException(object request, Exception exception)
{
var message = string.Format("Here we make a custom message...");
_logger.Error(message, exception);
return DtoUtils.HandleException(this, request, exception);
}
这导致错误被记录两次,因为的 DTOUtils.HandleException
还记录它,在一个不太定制的格式。是的,我更喜欢这样的DTOUtils记录和不想只是使用它。
This results in the error being logged twice, since DTOUtils.HandleException
also logs it, in a less customised format. Yes, I much prefer this to the DTOUtils logging and don't want to just use that.
我们如何关闭 DTOUtils
日志,同时保留其他的功能?没有人喜欢得到两倍多错误的电子邮件,因为他们应该。
How do we turn off DTOUtils
logging while retaining the rest of the functionality? Nobody likes getting twice as many error emails as they should.
推荐答案
我希望下面的代码可以解决你的问题。
I hope the following code solves your problem.
在AppHost.Configure
in AppHost.Configure
LogManager.LogFactory = new ServiceStack.Logging.Support.Logging.ConsoleLogFactory();
然后APPHOST类
then in AppHost class
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
在ServiceRunner类
in the ServiceRunner class
public class MyServiceRunner<T> : ServiceRunner<T>
{
public override object HandleException(IRequestContext requestContext, T request, Exception ex)
{
if ( isYourCondition )
{
ResponseStatus rs = new ResponseStatus("error1", "your_message");
// optionally you can add custom response errors
rs.Errors = new List<ResponseError>();
rs.Errors.Add(new ResponseError());
rs.Errors[0].ErrorCode = "more details 2";
// create an ErrorResponse with the ResponseStatus as parameter
var errorResponse = DtoUtils.CreateErrorResponse(request, ex, rs);
// log the error
Log.Error("your_message", ex);
return errorResponse;
}
else
return base.HandleException(requestContext, request, ex);
}
}
如果您返回base.HandleException ,它在内部调用DtoUtils.HandleException。
你会在控制台,一个日志错误只。
if you return the base.HandleException, it calls internally the DtoUtils.HandleException. You will see in console, one log error only.
看在客户端,如果你处理的自定义错误的WebServiceException。
In client, if you handle the WebServiceException for custom errors.
catch (WebServiceException err)
{
if ( err.ResponseStatus.Errors != null)
{ // do something with err.ResponseStatus.Errors[0].ErrorCode;
}
}
这篇关于ServiceStack - 如何禁用默认的异常记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!