Log4net记录器包装程序的实现和使用 [英] Implementation and usage of logger wrapper for log4net

查看:70
本文介绍了Log4net记录器包装程序的实现和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与史蒂文的答案-

This question is related to Steven’s answer - here. He proposed a very good logger wrapper. I will paste his code below:

public interface ILogger
{
    void Log(LogEntry entry);
}

public static class LoggerExtensions
{
    public static void Log(this ILogger logger, string message)
    {
        logger.Log(new LogEntry(LoggingEventType.Information,
            message, null));
    }

    public static void Log(this ILogger logger, Exception exception)
    {
        logger.Log(new LogEntry(LoggingEventType.Error, 
            exception.Message, exception));
    }

    // More methods here.
}

所以,我的问题是创建代理到log4net的实现的正确方法是什么??我应该只添加另一个带有类型参数的Log扩展方法,然后在其中创建一个开关吗?在LoggingEventType?

So, my question is what is the proper way to create implementation that proxies to log4net? Should I just add another Log extension method with type parameter and then create a switch inside? Use different log4net method in case of LoggingEventType ?

第二个问题,稍后在代码中使用它的最佳方法是什么?

因为他写道:

(...),您可以轻松创建ILogger实现(...)并进行配置 您的DI容器将其注入到具有ILogger的类中 构造函数.

(…) you can easily create an ILogger implementation (…) and configure your DI container to inject it in classes that have a ILogger in their constructor.

这是否意味着每个要登录的类(基本上是每个类)的构造函数中都应包含ILogger?

Does that mean that every class that will log sth (so basically every), should have ILogger in its constructor?

推荐答案

所以,我的问题是创建代理到log4net的实现的正确方法是什么?

So, my question is what is the proper way to create implementation that proxies to log4net?

您应该创建类似以下内容的

you should create something like:

public class Log4netAdapter : ILogger
{
    private readonly log4net.ILog m_Adaptee;

    public Log4netAdapter(log4net.ILog adaptee)
    {
        m_Adaptee = adaptee;
    }

    public void Log(LogEntry entry)
    {
        //Here invoke m_Adaptee
        if(entry.Severity == LoggingEventType.Debug)
            m_Adaptee.Debug(entry.Message, entry.Exception);
        else if(entry.Severity == LoggingEventType.Information)
            m_Adaptee.Info(entry.Message, entry.Exception);
        else if(entry.Severity == LoggingEventType.Warning)
            m_Adaptee.Warn(entry.Message, entry.Exception);
        else if(entry.Severity == LoggingEventType.Error)
            m_Adaptee.Error(entry.Message, entry.Exception);
        else
            m_Adaptee.Fatal(entry.Message, entry.Exception);
    }
}

这是否意味着每个要登录的类(基本上是每个类)都应在其构造函数中包含ILogger?

Does that mean that every class that will log sth (so basically every), should have ILogger in its constructor?

据我从史蒂文斯(Stevens)的答案中了解到:是的,您应该这样做.

As I understand from Stevens answer: Yes, you should do this.

稍后在代码中使用它的最佳方法是什么?

what is the best way to use it later in the code?

如果使用的是DI容器,则只需使用DI容器将ILogger映射到Log4netAdapter.您还需要注册log4net.ILog,或仅将log4net logger的实例提供给DI容器,以将其注入Log4netAdapter构造函数.

If you are using a DI container, then just use the DI container to map ILogger to Log4netAdapter. You also need to register log4net.ILog, or just give an instance of log4net logger to the DI container to inject it to the Log4netAdapter constructor.

如果您不使用DI容器,即使用纯DI ,然后您将执行以下操作:

If you don't use a DI container, i.e., you use Pure DI, then you do something like this:

ILog log = log4net.LogManager.GetLogger("MyClass");

ILogger logging_adapter = new Log4netAdapter(log);

var myobject = new MyClass(other_dependencies_here, logging_adapter);

这篇关于Log4net记录器包装程序的实现和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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