0

I'm using ServiceStack for some time and had a setup with some basic logging using ServiceStack.Logging package. It works well to log the exceptions that go up the call stack.

In some cases I may need to log an event further on the stack. My structure is something like this:

  • ServiceInterface - containing the services
  • ServiceModel - containing the DTOs
  • BLL - the logic layer (I need to log something here)

I have also a ServiceBase that setup the logging interface as following:

public abstract class MyServiceBase : Service
{
    public ILog log = LogManager.GetLogger(typeof(MyServiceBase));
}

What is the best approach to log information inside this BLL layer? Currently, I only see the option of passing the ILog instance to the BLL class and use it down there.

Is there any other option?

Thanks!

4

1 回答 1

2

您永远不需要传递 ILog 实例。LogFactory 负责这一点。当您的 Web 应用程序启动时,您需要通过以下命令设置 LogManager:

ServiceStack.Logging.LogManager.LogFactory = new Log4NetFactory();

一旦设置好,LogManager 将始终解析为正确的记录器。

public ILog log = LogManager.GetLogger(typeof(MyServiceBase));

您所要做的就是将日志记录属性添加到您要记录的每个类,并确保代码在设置 LogFactory 后始终运行。

对于其他项目/解决方案: 您可以将ServiceStack.Logging项目添加到需要记录的任何内容。它是一个轻量级的解决方案,将您与 Log4Net 分离。使更改日志记录实现变得微不足道。如果您使用 ServiceStack 的日志记录,那么您的所有日志记录和内部 ServiceStack 日志记录将一起记录。

于 2013-08-02T18:34:54.290 回答