Log4Net Wrapper类是什么样的? [英] What would a Log4Net Wrapper class look like?

查看:161
本文介绍了Log4Net Wrapper类是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找.net(c#)的日志框架,并决定在读取stackoverflow上的几个问题/回答线程后,给log4net一下。我看到人们一再提到他们使用的是一个包装类的log4net,我不知道这将是什么样子。

I have been looking for a logging framework for .net (c#) and decided to give log4net a go after reading up on a few question/answer threads here on stackoverflow. I see people mentioning over and over that they use a wrapper class for log4net and I am wonder what that would look like.

我有我的代码分成不同的项目数据访问/业务/ webservice / ..)。
log4net包装器类如何?包装类是否需要包含在所有项目中?我应该把它作为一个单独的项目一起构建吗?

I have my code split up into different projects (data access/business/webservice/..). How would a log4net wrapper class look like? Would the wrapper class need to be included in all of the projects? Should I build it as a separate project all together?

包装器应该是单例类吗?

Should the wrapper be a singleton class?

推荐答案

本质上您创建一个接口,然后直接包装Log4net的类和方法的该接口的具体实现。可以通过创建更具体的类来包装其他类和这些系统的方法来包装其他日志记录系统。最后,使用工厂根据配置设置或代码行更改来创建包装器的实例。 (注意:您可以使用反转控制容器来获得更灵活和复杂的功能,例如< a href =http://structuremap.sourceforge.net/Default.htm =noreferrer> StructureMap 。)

Essentially you create an interface and then a concrete implementation of that interface that wraps the classes and methods of Log4net directly. Additional logging systems can be wrapped by creating more concrete classes which wrap other classes and methods of those systems. Finally use a factory to create instances of your wrappers based on a configuration setting or line of code change. (Note: you can get more flexible - and complex - using an Inversion of Control container such as StructureMap.)

public interface ILogger
{
    void Debug(object message);
    bool IsDebugEnabled { get; }

    // continue for all methods like Error, Fatal ...
}

public class Log4NetWrapper : ILogger
{
    private readonly log4net.ILog _logger;

    public Log4NetWrapper(Type type)
    {
        _logger = log4net.LogManager.GetLogger(type);
    }

    public void Debug(object message)
    {
        _logger.Debug(message);
    }

    public bool IsDebugEnabled
    {
        get { return _logger.IsDebugEnabled; }
    }

    // complete ILogger interface implementation
}

public static class LogManager
{
    public static ILogger GetLogger(Type type)
    {
        // if configuration file says log4net...
        return new Log4NetWrapper(type);
        // if it says Joe's Logger...
        // return new JoesLoggerWrapper(type);
    }
}

还有一个在你的类中使用这个代码的例子(声明为静态只读字段):

And an example of using this code in your classes (declared as a static readonly field):

private static readonly ILogger _logger =
    LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

您可以使用以下方式获得相同的稍微更好的效果:

You can get the same slightly more performance friendly effect using:

private static readonly ILogger _logger = 
    LogManager.GetLogger(typeof(YourTypeName));

前一个例子被认为更易于维护。

The former example is considered more maintainable.

您不想创建一个Singleton来处理所有日志记录,因为Log4Net记录调用类型;它更清洁和有用,让每种类型都使用自己的记录器,而不仅仅是在记录所有消息的日志文件中看到一个类型。

You would not want to create a Singleton to handle all logging because Log4Net logs for the invoking type; its much cleaner and useful to have each type use its own logger rather than just seeing a single type in the log file reporting all messages.

因为你的实现应该是相当可重用的(您组织中的其他项目),您可以使其成为自己的组件,或者理想地将其与您自己的个人/组织的框架/实用程序集合一起包含。不要在您的每个业务/数据/ UI程序集中重新声明类,这是不可维护的。

Because your implementation should be fairly reusable (other projects in your organization) you could make it its own assembly or ideally include it with your own personal/organization's framework/utility assembly. Do not re-declare the classes separately in each of your business/data/UI assemblies, that's not maintainable.

这篇关于Log4Net Wrapper类是什么样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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