Using Elmah with ServiceStack.Mvc

2019-04-11 21:52发布

问题:

I could not see unhandled exceptions ( MyService:RestServiceBase) on /elmah.axd path.

I've added http handlers for seeing errors.

 <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />

I've installed ServiceStack.Logging.Elmah

UPDATE

I've installed Elmah.Mvc package also

I can get errors in Controller's Action but I could not get service errors!

UPDATE

I'm not alone :) https://groups.google.com/forum/#!topic/servicestack/WSrM5CLL120

回答1:

You need subscribe to AppHostBase.ServiceExceptionHandler events in Configure method which normally resides in AppHost class:

public class AppHost : AppHostBase
{
    ....

    public override void Configure(Funq.Container container)
    {
        ServiceExceptionHandler += (request, exception) =>
        {
            //pass the exception over to Elmah
            var context = HttpContext.Current;
            Elmah.ErrorLog.GetDefault(context).Log(new Error(exception, context));

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

    ....
}