How do we integrate elmah logging in servicestack

跟風遠走 提交于 2019-12-05 08:32:58

If you have an existing logging solution then you can use the ServiceStack.Logging.Elmah project. It is available via NuGet.

Exceptions, errors and fatal calls will be logged to Elmah in addition to the originally intended logger. For all other log types, only the original logger is used.

So if you are already using Log4Net then you can just configure Elmah like this

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

If you don't want to wrap in over an existing log then you can just research adding Elmah to any ASP.NET website. There is no reason it wouldn't work just because you are using ServiceStack.

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
    }
}

Now your ServiceStack error's appear in Elmah (assuming you've setup web.config etc).

Actually kampsj answer is better than Gavin's as Gavins causes double-logging to elmah by calling explicit elmah logger and then the default servicestack error handling...which itself already does the logging.

So really all you need is this (below assuming you want to wrap NLog with 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...
}

You could just have this above:

ElmahLogFactory factory = new ElmahLogFactory();

...but you probably should wrap another type of logger for non-error logging, like Debug and Warn.

This section on configuring Elmah and the Logging.Elmah UseCase for a working example of ServiceStack and Elmah configured together.

The ElmahLogFactory can be configured in your Global.asax before initializing the ServiceStack AppHost, e.g:

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!