Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c# - log4net: Creating a logger dynamically, should I worry about anything?

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 which 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();

    // this will add this appender to all logger created by LogManager.GetLogger(...)
    //hierarchy.Root.AddAppender(roller);

    // this will change all Loggers level to Level.All
    //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)
    {
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...