我是 servicestack 和 elman 日志记录的新手。
任何人都可以建议我们如何将 elmah 集成到服务堆栈应用程序中。
谢谢...
如果您有现有的日志记录解决方案,那么您可以使用ServiceStack.Logging.Elmah项目。它可通过 NuGet 获得。
除了最初的记录器之外,异常、错误和致命调用将被记录到 Elmah。对于所有其他日志类型,仅使用原始记录器。
因此,如果您已经在使用 Log4Net,那么您可以像这样配置 Elmah
ElmahLogFactory factory = new ElmahLogFactory(new Log4NetFactory());
如果您不想覆盖现有日志,那么您可以研究将 Elmah 添加到任何 ASP.NET 网站。没有理由仅仅因为您使用的是 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
}
}
现在您的 ServiceStack 错误出现在 Elmah 中(假设您已经设置了 web.config 等)。
实际上,kampsj 的答案比 Gavin 的要好,因为 Gavins 通过调用显式 elmah 记录器和默认的 servicestack 错误处理来导致对 elmah 的双重日志记录……它本身已经进行了日志记录。
所以你真正需要的就是这个(下面假设你想用 Elmah包装 NLog )
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();
...但是您可能应该为非错误日志记录包装另一种类型的记录器,例如调试和警告。
本节介绍如何配置 Elmah和Logging.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();
}
}