log4net的:动态地创建一个记录器,我应该担心什么? [英] log4net: Creating a logger dynamically, should I worry about anything?

查看:169
本文介绍了log4net的:动态地创建一个记录器,我应该担心什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态地创建记录器,所以用后<一个href="http://stackoverflow.com/questions/571876/best-way-to-dynamically-set-an-appender-file-path">from这里和反光镜的帮助下,我已经成功地动态地创建记录器,但我想知道我是否应该担心别的东西......我不知道至极的影响可以做到这一点。

I need to create loggers dynamically, so with a post from here and the help of reflector I have managed to create loggers dynamically, but I'd like to know if I should worry about something else ... I don't know wich implications can have do it.

    public static ILog GetDyamicLogger(Guid applicationId)
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
        RollingFileAppender roller = new RollingFileAppender();
        roller.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
        roller.AppendToFile = true;
        roller.RollingStyle = RollingFileAppender.RollingMode.Composite;
        roller.MaxSizeRollBackups = 14;
        roller.MaximumFileSize = "15000KB";
        roller.DatePattern = "yyyyMMdd";
        roller.Layout = new log4net.Layout.PatternLayout();
        roller.File = "App_Data\\Logs\\"+applicationId.ToString()+"\\debug.log";
        roller.StaticLogFileName = true;

        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date [%thread] %-5level %logger [%property{NDC}] - %message%newline";
        patternLayout.ActivateOptions();

        roller.Layout = patternLayout;
        roller.ActivateOptions();
        hierarchy.Root.AddAppender(roller);

        hierarchy.Root.Level = Level.All;
        hierarchy.Configured = true;

        DummyLogger dummyILogger = new DummyLogger(applicationId.ToString());
        dummyILogger.Hierarchy = hierarchy;
        dummyILogger.Level = log4net.Core.Level.All;
        dummyILogger.AddAppender(roller);

        return new LogImpl(dummyILogger);
    }

    internal sealed class DummyLogger : Logger
    {
        // Methods
        internal DummyLogger(string name)
            : base(name)
        {
        }
    }

干杯。

推荐答案

我要说的是,你不必担心创建于code记录仪。这是他们创造的一种支持的方法。你做的应用程序运行时失去改变事物的能力(除非你写的code吧)。这只是利用配置文件的优点之一。

I would say that you don't have to worry about creating loggers in code. It's one of the supported methods of creating them. You do lose the ability to change things while the application is running (unless you write the code for it). That's just one of the benefits of using config files.

这篇关于log4net的:动态地创建一个记录器,我应该担心什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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