使用类名记录器将log4net记录到多个定义的附加程序 [英] log4net logging to multiple defined appenders using class name loggers

查看:117
本文介绍了使用类名记录器将log4net记录到多个定义的附加程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想登录到多个文件(仅2个不同的文件),但是使记录器的名称与类名相同,就像通常所做的那样:

I want to log to multiple files (2 different files only) but leave the logger name the same as the class name like is typically done:

private static readonly log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, isReadOnly);

我想保留类名,以便将其输出到记录器.

I want to keep the class name so I can output it to the logger.

问题是,我需要根据被调用的wcf方法(这是一个wcf服务)在添加程序之间动态切换.我尝试了各种log4net配置设置,包括以下解决方案:

The problem is, I need to dynamically switch between appenders based on the wcf method being called (this is a wcf service). I tried various log4net config settings, including this solution:

配置Log4net以写入多个文件

但是许多解决方案都具有不需要的预定义记录器名称.我也不想在log4net配置中对所有不同的类名进行硬编码,并引用那些特定的记录器(维护噩梦).

But many solutions have pre-defined logger names which I don't want. I also don't want to hard code all my different class names in the log4net config and reference those specific loggers (maintenance nightmare).

我正在寻找的东西是这样的:

What I'm looking for is something like this:

public static ILog GetLogger(Type classType, bool shouldLogToFile2)
{
    if (shouldLogToFile2)
    {
        return LogManager.GetLogger(classType, file2Appender); // log to file2.log (this statement doesn't compile)
    }
    else
    {
        return LogManager.GetLogger(classType, file1Appender); // log to file1.log (this statement doesn't compile)
    }
}

并这样称呼:

ILog log = GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, true);
log.Info("This should log to file2.log");

推荐答案

如果要使用相同名称但使用不同附加程序的多个记录器,则必须设置多个存储库以包含它们.

If you want to have multiple loggers with the same name but using different appenders, then you must set up multiple repositories to contain them.

关于存储库的文档(略显不足)中所述 :

可以使用LogManager.CreateRepository方法创建命名日志存储库.可以使用LogManager.GetRepository方法检索其存储库.以这种方式创建的存储库将需要以编程方式进行配置.

Named logging repositories can be created using the LogManager.CreateRepository method. The repository for can be retrieved using the LogManager.GetRepository method. A repository created in this way will need to be configured programmatically.

但是,这并不意味着实际的配置必须在代码中,而是您应该执行以下操作:

However, this does not mean the actual config must be in code, but that you should do something like this:

// or wherever
string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 

var repository = log4net.LogManager.CreateRepository("file1repo");
log4net.XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(configPath));

repository = log4net.LogManager.CreateRepository("file2repo");
log4net.XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(configPath));

然后,您将编写代码来修改"file2"附加程序以写入到file2:

Then you would write code to amend the 'file2' appender to write to file2:

var appender = LogManager.GetRepository("file2repo").GetAppenders()
                         .OfType<RollingFileAppender>().Single();

appender.File = "file2.log";
appender.ActivateOptions(); 

您的代码如下:

public static ILog GetLogger(Type classType, bool shouldLogToFile2)
{
    return LogManager.GetLogger(shouldLogToFile2 ? "file2repo" : "file1repo", classType);
}

这篇关于使用类名记录器将log4net记录到多个定义的附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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