C# ServiceStack-如何禁用默认异常日志记录
根据,我们有一个全局服务异常处理程序。文档说该处理程序应该记录异常,然后调用C# ServiceStack-如何禁用默认异常日志记录,c#,logging,log4net,
servicestack,C#,Logging,Log4net,
servicestack,根据,我们有一个全局服务异常处理程序。文档说该处理程序应该记录异常,然后调用DtoUtils.HandleException,如下所示: private object LogServiceException(object request, Exception exception) { var message = string.Format("Here we make a custom message..."); _logger.Error(message, exception);
DtoUtils.HandleException
,如下所示:
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日志,我不想只使用它
如何在保留其余功能的同时关闭
DTOUtils
日志记录?没有人喜欢收到两倍于他们应该收到的错误电子邮件。我希望下面的代码能够解决您的问题
根据文件
及
在AppHost.Configure中
LogManager.LogFactory = new ServiceStack.Logging.Support.Logging.ConsoleLogFactory();
然后在AppHost类中
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
不要调用
DtoUtils.HandleException,因为它会记录错误。也不要调用ServiceRunner.HandleException
,它会调用DtoUtils.HandleException
调用DtoUtils.CreateErrorResponse
进行响应(它由DtoUtils.HandleException
使用)。ToResponseStatus
帮助程序也位于DtoUtils
我的AppServiceRunner现在是这样的:
public class AppServiceRunner<T> : ServiceRunner<T>
{
public AppServiceRunner(AppHost appHost, ActionContext actionContext)
: base(appHost, actionContext)
{
}
public override object HandleException(IRequestContext requestContext,
T request, Exception ex)
{
LogException(requestContext, request, ex);
var responseStatus = ex.ToResponseStatus();
return DtoUtils.CreateErrorResponse(request, ex, responseStatus);
}
private void LogException(IRequestContext requestContext, T request, Exception ex)
{
// since AppHost.CreateServiceRunner can be called before AppHost.Configure
// don't get the logger in the constructor, only make it when it is needed
var logger = MakeLogger();
var requestType = typeof(T);
var message = string.Format("Exception at URI:'{0}' on service {1} : {2}",
requestContext.AbsoluteUri, requestType.Name, request.ToJson());
logger.Error(message, ex);
}
private static ILog MakeLogger()
{
return LogManager.GetLogger(typeof(AppServiceRunner<T>));
}
}
公共类AppServiceRunner:ServiceRunner
{
公共AppServiceRunner(AppHost AppHost、ActionContext ActionContext)
:base(appHost、actionContext)
{
}
公共重写对象HandleException(IRequestContext requestContext,
T请求,异常(例如)
{
LogException(requestContext、request、ex);
var responseStatus=例如ToResponseStatus();
返回DtoUtils.CreateErrorResponse(请求、ex、responseStatus);
}
私有void日志异常(IRequestContext requestContext、T请求、异常ex)
{
//因为可以在AppHost.Configure之前调用AppHost.CreateServiceRunner
//不要在构造函数中获取记录器,只在需要时进行
var logger=MakeLogger();
var requestType=typeof(T);
var message=string.Format(“服务{1}:{2}上URI:'{0}'处的异常”,
requestContext.AbsoluteUri,requestType.Name,request.ToJson();
记录器错误(消息,ex);
}
私有静态ilogmakelogger()
{
返回LogManager.GetLogger(typeof(AppServiceRunner));
}
}
现在,我得到的唯一服务错误是由这段代码生成的错误。假设您需要在异常处理程序中登录是否安全?如果不是,为什么不直接从那里删除它并让DtoUtils记录异常?如果您创建ServiceRunner,正如您在同一文档中所读到的,错误会记录两次?是的,它使用ServiceRunner记录了两次。ServiceRunner上的文档非常稀少,但是如果您创建自己的ServiceRunner,您可以从IRequestContext记录url,然后调用“return base.HandleException(requestContext,request,ex);”。。它调用DtoUtils.HandleException,它做两件事-做出错误响应并记录异常。我希望这是一个更单一的责任。我没有时间去测试它。但是在我的代码中,我不返回base.HandleException,或者在条件下返回它。我使用DtoUtils.CreateErrorResponse创建响应。。。在这种情况下,它被记录了两次、一次,或者根本没有记录?是的,CreateErrorResponse看起来是一种方法,我正在测试它。你能更新答案吗?为什么你认为我的答案从不直接调用DtoUtils.HandleException;还是间接地?你看过我的答案了吗?我不明白你的意思。你的问题是关于“错误被记录两次……没有人喜欢得到两倍多的错误”。这对我不起作用。ServiceStack仍然自己写入日志。实际上它确实工作了,但我在调用另一个服务时出错,ServiceClientBase总是写入错误,而不管配置如何。
catch (WebServiceException err)
{
if ( err.ResponseStatus.Errors != null)
{ // do something with err.ResponseStatus.Errors[0].ErrorCode;
}
}
public class AppServiceRunner<T> : ServiceRunner<T>
{
public AppServiceRunner(AppHost appHost, ActionContext actionContext)
: base(appHost, actionContext)
{
}
public override object HandleException(IRequestContext requestContext,
T request, Exception ex)
{
LogException(requestContext, request, ex);
var responseStatus = ex.ToResponseStatus();
return DtoUtils.CreateErrorResponse(request, ex, responseStatus);
}
private void LogException(IRequestContext requestContext, T request, Exception ex)
{
// since AppHost.CreateServiceRunner can be called before AppHost.Configure
// don't get the logger in the constructor, only make it when it is needed
var logger = MakeLogger();
var requestType = typeof(T);
var message = string.Format("Exception at URI:'{0}' on service {1} : {2}",
requestContext.AbsoluteUri, requestType.Name, request.ToJson());
logger.Error(message, ex);
}
private static ILog MakeLogger()
{
return LogManager.GetLogger(typeof(AppServiceRunner<T>));
}
}