log4net动态创建日志文件

时间:2016-11-29 20:08:05

标签: c# log4net

我找不到关于我要做什么的帖子。基本上,要求是能够动态创建日志文件。详细地说,我们希望能够根据某些事件更改日志文件的位置。

实施例: 所以我从C:\ Type1Logs \ Log.txt开始并正常写入文件。接下来会发生一个事件,它需要在C:\ Type2Logs \ Logs.dat创建一个新文件,而所有新日志消息都会发送到该文件。

这是我用作原型的一些简单代码。我添加了hier来调试,看看我是否可以使用该路由更改它。它确实让我发现我当前的代码创建了另一个appender而不是改变现有代码。

using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApp
{
    class Program
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            string Input = "";

            Logger.ConfigureFileAppender("log.txt"); // This only has to be called once.

            while (Input.ToUpper() != "QUIT")
                {
                Console.WriteLine("Enter text to write to the log");
                Input = Console.ReadLine();

                if (Input.Length > 7 && Input.ToUpper().Substring(0,6) == "NEWLOG")
                {
                    Logger.ConfigureFileAppender(Input.Substring(6));
                    Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;


                }

                Log.Info(Input);
            }

        }
    }


class Logger
    {
        public static void ConfigureFileAppender(string logFile)
        {
            var fileAppender = GetFileAppender(logFile);
            BasicConfigurator.Configure(fileAppender);
            ((Hierarchy)LogManager.GetRepository()).Root.Level = Level.Debug;
        }

        private static IAppender GetFileAppender(string logFile)
        {
            var layout = new PatternLayout("%date %-5level %logger - %message%newline");
            layout.ActivateOptions(); // According to the docs this must be called as soon as any properties have been changed.

            var appender = new FileAppender
            {
                File = logFile,
                Encoding = Encoding.UTF8,
                Threshold = Level.Debug,
                Layout = layout
            };

            appender.ActivateOptions();

            return appender;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这段代码是我最终得到我需要的东西。我用以下文章指出了正确的方向 - > http://blog.gfader.com/2010/05/log4net-how-to-change-settings-of.html

// Get the Hierarchy object that organizes the loggers
                    Hierarchy hier = log4net.LogManager.GetRepository() as Hierarchy;
                    if (hier != null)
                    {
                        // Get Appender
                        FileAppender fileAppender =
                            (FileAppender)hier.GetAppenders().Where(
                                appender => appender.Name.Equals("MyLogger", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

                        if (fileAppender != null)
                        {
                            fileAppender.File = Input.Substring(6);

                            //refresh settings of appender
                            fileAppender.ActivateOptions();                         
                        }
                    }