使用log4net创建具有动态名称的多个日志文件 [英] Create multiple Logfiles with dynamic Names with log4net

查看:557
本文介绍了使用log4net创建具有动态名称的多个日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows服务中使用log4net.该服务处理一些RFID读取器.当前,我们将所有Reader的所有任务记录在一个日志文件中.这很好. 但是现在我想将每个阅读器的任务记录在一个单独的文件中.读取器由其IP地址标识.因此,我想将IP地址作为文件名的一部分.

I'm using log4net in a Windows Service. This Service processes some RFID Reader. Currently we are logging all tasks of all Reader in one Logfile. This works fine. But now I want to log the tasks of each Reader in a separate File. The Readers are identified by their IP Address. So I want to take the IP Address as part of the Filename.

log4net中用于创建动态文件追加器的选项似乎不适合我,因为每次写日志时,我都必须管理从Reader到日志文件的分配.

The option in log4net to create dynamic file appenders seems not to fit for me, because I would have to manage the assignment from Reader to log file, each time I write a log.

在log4net中是否有适当的方法可以做到这一点,或者不可能吗?

Is there an appropriate way to do this in log4net, or is it not possible?

推荐答案

在我的Logclass中,我为Logger使用了Dictionary<string, ILog>.我已经重载了方法,要么使用Default-Logger,要么获取Dictionary的密钥以使用指定的Logger.

In my Logclass I used a Dictionary<string, ILog> for my Loggers. I've overloaded methods, either they use the Default-Logger or they get the Key for the Dictionary to use the specified Logger.

public static class Log
{
    private static readonly Dictionary<string, ILog> loggers = new Dictionary<string, ILog>();

    static Log()
    {
        XmlConfigurator.Configure();
    }

    public static void Debug(string message)
    {
        Debug(Logger.Default, message);
    }

    public static void Debug(string readerIp, string message)
    {
        GetLoggerInternal(readerIp).Debug(message);
    }

    private static ILog GetLoggerInternal(string logger)
    {
        if (!loggers.ContainsKey(logger))
        {
            var appender = CreateRollingFileAppender(logger);
            appender.ActivateOptions();
            loggers.Add(logger, LogManager.GetLogger(logger));
            ((log4net.Repository.Hierarchy.Logger)loggers[logger].Logger).AddAppender(appender);
        }
        return loggers[logger];
    }

    private static RollingFileAppender CreateRollingFileAppender(string readingPointIp)
    {
        var layout = new PatternLayout
        {
            ConversionPattern = "%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"
        };
        layout.ActivateOptions();

        return new RollingFileAppender
        {
            Name = readingPointIp,
            AppendToFile = true,
            DatePattern = "yyyyMMdd",
            MaximumFileSize = "1MB",
            MaxSizeRollBackups = 10,
            RollingStyle = RollingFileAppender.RollingMode.Composite,
            File = $"..\\Log\\{readingPointIp}_log.txt",
            Layout = layout
        };
    }
}

调用.ActivateOptions();方法很重要,它们实例化了Appender和Layout类.我使用LogManager.GetLogger创建一个新的Logger.要添加添加程序,必须使用AddAppender.

It is important to call the .ActivateOptions(); methods, they instantiate the Appender and Layout Classes. I use LogManager.GetLogger to create a new Logger. To add the appender I've to cast the logger, to use AddAppender.

现在,我只需要调用Log.Debug(readingPoint.IpAddress, "Some readingpoint specific log message.");,并将此消息保存在文件中,并在其名称中输入IP地址.

Now I just have to call Log.Debug(readingPoint.IpAddress, "Some readingpoint specific log message."); and I've this message in a file, with the IP Address in it's name.

这篇关于使用log4net创建具有动态名称的多个日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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