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

查看:23
本文介绍了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.

我将代码拆分为不同的项目(数据访问/业务/网络服务/..).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" rel="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.

您不希望创建一个单例来处理所有日志记录,因为 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天全站免登陆