单例记录器,静态记录器,工厂记录器...如何记录? [英] Singleton logger, static logger, factory logger... how to log?

查看:68
本文介绍了单例记录器,静态记录器,工厂记录器...如何记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在包装图案&针对.NET编写的应用程序,实践了Enterprise Library Logging Application Block.

I am wrapping the patterns & practices Enterprise Library Logging Application Block for an application written in .NET.

我希望能够将记录器归类(即提供特定于域的记录).

I want to be able to subclass a logger (i.e to provide domain specific logging).

做到这一点的最佳方法是什么?

What is the best way to do this?

例如,目前我有一个静态Logger类,但这不允许我专门针对特定于域的日志记录.

For e.g, I have a static Logger class at the moment, but this does not allow me to specialize it for domain specific logging.

例如,

Log(MyDomainObj obj, string msg)

推荐答案

查看 NLog .他们使用这种模式:

Check out NLog. They use this sort of pattern:

private static Logger myDomainLogger = LogManager.GetCurrentClassLogger();

然后您可以基于myDomainLogger所属的类来专门化输出.

You can then specialize the output based on the class that myDomainLogger belongs to.

更多细节:

class MyDomain
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    private void SomeFunc()
    {
        _logger.Trace("this is a test");
    }
}

然后在输出中,可以将其输出"MyDomain.SomeFunc"作为这是测试"消息的一部分.

Then in your output you can have it output "MyDomain.SomeFunc" as part of the "this is a test" message.

这篇关于单例记录器,静态记录器,工厂记录器...如何记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆