2013-04-14 33 views

回答

4

如果你有一个现有的记录解决方案,那么你可以使用ServiceStack.Logging.Elmah项目。它可以通过NuGet获得。

除最初预定的记录器外,异常,错误和致命的调用将记录到Elmah。对于所有其他日志类型,仅使用原始日志记录器。

所以,如果你已经使用log4net的,那么你可以像这样

ElmahLogFactory factory = new ElmahLogFactory(new Log4NetFactory()); 

配置ELMAH如果您不想在现有的日志那么你可以加入研究ELMAH任何ASP包裹英寸NET网站。没有理由不会因为您使用ServiceStack而无法正常工作。

3
using ServiceStack.Logging; 
using ServiceStack.Logging.Elmah; 
using ServiceStack.Logging.NLogger; 

public AppHost() 
     : base(
      "description", 
      typeof(MyService).Assembly) 
    { 
     LogManager.LogFactory = new ElmahLogFactory(new NLogFactory()); 
    } 

    public override void Configure(Container container) 
    { 
     this.ServiceExceptionHandler += (request, exception) => 
      { 
       // log your exceptions here 
       HttpContext context = HttpContext.Current; 
       ErrorLog.GetDefault(context).Log(new Error(exception, context)); 

       // call default exception handler or prepare your own custom response 
       return DtoUtils.HandleException(this, request, exception); 
      }; 

     // rest of your config 
    } 
} 

现在你的ServiceStack错误出现在Elmah中(假设你已经设置了web.config等)。

+0

只是要清楚,答案上面使用异常单一入口点... https://开头的github .COM/ServiceStack/ServiceStack /维基/错误处理 – Darren

2

其实kampsj的答案比Gavin的更好,因为Gavins通过调用显式elmah记录器,然后默认的服务堆错误处理...本身已经做了日志记录,导致双重记录到elmah。

所以真的所有你需要的是这样的(以下假设你想包装NLOG与ELMAH)

public class YourAppHost : AppHostBase 
{ 
    public YourAppHost() //Tell ServiceStack the name and where to find your web services 
     : base("YourAppName", typeof(YourService).Assembly) 
    { 
     LogManager.LogFactory = new ElmahLogFactory(new NLogFactory()); 
    } 

    //...just normal stuff... 
} 

你可能只是有这个上面:

ElmahLogFactory factory = new ElmahLogFactory(); 

...但你可能应该为非错误日志记录封装另一种类型的记录器,如“调试”和“警告”。

0

这一节关于configuring ElmahLogging.Elmah UseCase的ServiceStack和Elmah的一个工作示例配置在一起。

ElmahLogFactory可以在Global.asax进行配置,如前初始化ServiceStack APPHOST:

public class Global : System.Web.HttpApplication 
{ 
    protected void Application_Start(object sender, EventArgs e) 
    { 
     var debugMessagesLog = new ConsoleLogFactory(); 
     LogManager.LogFactory = new ElmahLogFactory(debugMessagesLog, this); 
     new AppHost().Init(); 
    } 
}