我应该直接在域模型对象中使用 log4net 吗?

发布于 2024-07-18 02:29:03 字数 1687 浏览 11 评论 0原文

我想知道直接在域对象上使用 log4net 是否是不好的做法...我将使用 ELMAH 来处理 ASP.NET MVC 应用程序端的异常,但出于某些信息目的,我想记录一些数据关于领域模型本身。

给定以下域对象:

   public class Buyer
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

    private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
    public IList<SupportTicket> SupportTickets
    {
        get
        {
            return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
        }
    }

    public void AddSupportTicket(SupportTicket ticket)
    {
        if (!SupportTickets.Contains(ticket))
        {
            _SupportTickets.Add(ticket);
        }
    }
}

在 AddSupportTicketMethod 中添加日志记录行为是一个坏主意...所以本质上它看起来像这样:

       public class Buyer
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(SupportTicket));

    public Buyer()
    {
       log4net.Config.XmlConfigurator.Configure();
    }


    private int _ID;
    public int ID
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

    private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
    public IList<SupportTicket> SupportTickets
    {
        get
        {
            return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
        }
    }

    public void AddSupportTicket(SupportTicket ticket)
    {
        if (!SupportTickets.Contains(ticket))
        {
            _SupportTickets.Add(ticket);
        } else {
           log.Warn("Duplicate Ticket Not Added.");
        }
    }
}

I'm wondering if it's bad practice to use log4net directly on my domain object... I'll be using ELMAH for my exceptions on the ASP.NET MVC application side, but for some informational purposes I'd like to log some data about the domain model itself.

Given the following domain object:

   public class Buyer
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

    private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
    public IList<SupportTicket> SupportTickets
    {
        get
        {
            return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
        }
    }

    public void AddSupportTicket(SupportTicket ticket)
    {
        if (!SupportTickets.Contains(ticket))
        {
            _SupportTickets.Add(ticket);
        }
    }
}

Is adding logging behavior in the AddSupportTicketMethod a bad idea...so essentialy it'd look like this:

       public class Buyer
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(SupportTicket));

    public Buyer()
    {
       log4net.Config.XmlConfigurator.Configure();
    }


    private int _ID;
    public int ID
    {
        get { return _ID; }
        set
        {
            _ID = value;
        }
    }

    private IList<SupportTicket> _SupportTickets=new List<SupportTicket>();
    public IList<SupportTicket> SupportTickets
    {
        get
        {
            return _SupportTickets.ToList<SupportTicket>().AsReadOnly();
        }
    }

    public void AddSupportTicket(SupportTicket ticket)
    {
        if (!SupportTickets.Contains(ticket))
        {
            _SupportTickets.Add(ticket);
        } else {
           log.Warn("Duplicate Ticket Not Added.");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的十字路口 2024-07-25 02:29:03

我直接在域对象中使用了 log4net 和 log4J。 这有好的副作用,也有坏的副作用。

  • +:登录域对象的代码简单明了,您知道可以利用 log4net 功能。
  • --:这意味着使用域对象的程序需要注意 log4net 配置,这可能是也可能不是问题
  • --:您无法链接您的域对象与调用程序正在使用的 log4net 版本不同。 我已经看到很多冲突,其中一项与 log4net 1.2.0.10 链接,另一项与早期版本链接。

不记录您的域对象是一个坏主意。 另一种方法是像其他人建议的那样,依赖注入或外部框架(例如 log4J 的 commons-logging),允许插入不同的日志框架或创建一个接口来执行日志记录并针对该接口进行日志记录。 (使用域对象的代码需要提供该接口的适当实例以用于日志记录目的。)

I have used log4net and log4J directly in domain objects. This has good side effects and bad ones.

  • +: Logging in the domain object is simple and straightforward to code and you know you can take advantage of log4net features.
  • --: It means the program making use of the domain objects needs to pay attention to log4net configuration, which may or may not be a problem
  • --: You cannot link your domain object to a different log4net version than the calling program is using. I've seen a lot of conflicts with one item linked against log4net 1.2.0.10 and another linked against an earlier release.

Not logging in your domain object is a bad idea. The alternative is as others have suggested, dependency injection or an external framework (such as commons-logging for log4J) that allows plugging different logging frameworks or creating an interface that does the logging and logging against that interface. (The code using your domain object would need to then supply an appropriate instance of that interface for logging purposes.)

牵强ㄟ 2024-07-25 02:29:03

如果您要从域对象登录并且使用可能想要换出的 IOC 容器,我建议您使用服务定位器模式(您可以查看 Sharp# 架构,以获取 SafeServiceLocator 的良好实现用更多信息性错误消息包装 msoft 的 ServiceLocator)。

我还想建议您考虑是否要记录示例中显示的错误类型。 在这种情况下,我倾向于让域对象抛出异常,并让调用者决定这是否是应用程序所期望的(因此不应被记录),或者是否代表调用者想要的情况以某种方式处理。

If you are going to log from your domain objects and you use an IOC container which you might want to swap out, I would recommend you use the Service Locator pattern (you could look at the Sharp# architecture for a nice implementation of a SafeServiceLocator that wraps msoft's ServiceLocator with more informative error messages).

I would also like to suggest that you consider whether you want to log the type of error you show in your example. I would tend to want to have the domain object throw an exception in that case and let the caller decide whether that was something that was expected by the application (and hence shouldn't be logged) or whether that represents a situation that the caller wants to deal with in some way.

夜吻♂芭芘 2024-07-25 02:29:03

这是一个经典问题!

执行此操作的好方法是引入 ILogger 类型的类成员并将日志记录抽象到此接口中。 在你的类中,无论你在哪里调用 logg ,都可以通过这个接口来完成。 然后在运行时通过使用可用 IoC 容器之一或依赖项注入 farmeworks 的实现之一注入此依赖项。 默认情况下,您可以使用该接口的 log4net 实现。

以下是可用依赖注入框架的一长串:
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

This is a classic question!

The good way of doing this would be to introduce a class member of ILogger type and abstract the logging into this interface. In your class wherever you do a call to logg something do it through this interface. Then inject this dependency at the run-time with one of the implementation using one of the available IoC container or dependency injection farmeworks. By default you can use log4net implementation of this interface.

Here is a long list of available dependency injection frameworks:
http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

月下客 2024-07-25 02:29:03

我认为日志记录是一个横切问题,因此最好以面向方面的方式完成。 如果您使用的是 Spring.NET 这样的框架,那么您就可以使用它。

I think logging is a cross cutting concern, so it's best done in an aspect-oriented fashion. If you're using a framework like Spring.NET it's available to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文