2013-04-14 38 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(); 
    } 
} 
相關問題