将日志文件写入两个不同的文件中

发布于 2024-07-26 15:24:26 字数 2972 浏览 5 评论 0原文

我的应用程序运行在自定义的客户端框架上,客户端框架使用 log4net 来记录自己的日志文件。 我们(我们的应用程序)必须使用相同的 log4net 在我们自己的路径(比如我们的自定义路径)中记录我们的日志文件。 目前我们的日志文件已创建,但日志未写入该文件。 它正在写入客户端框架日志文件。

我搜索了很多网站的链接 Log4Net:以编程方式指定多个记录器(具有多个文件附加程序) 帮助我以编程方式配置 log4net 配置, 我的日志语句仍然没有写入我的日志文件中。使用的代码如下

 public class TraceLog
    {
        private string message = string.Empty;        
        private static ILog ILogger = null; 
        private static TraceLog instance = new TraceLog();
        private TraceLog()
        {
            SetLevel("Log4net.MainForm", "ALL");
            AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
        }        
        public static TraceLog Instance
        {
            get
            {
                return instance;
            }
        }      
        public void Debug(string logMessage)
        {
            message = PrepareLog(logMessage);
            ILogger.Debug(message);
        }
        protected string PrepareLog(string logMessage)
        {
            string message = GetFileMethodLineNumberInfo();
            message += logMessage;
            return message;
        }      
        protected string GetFileMethodLineNumberInfo()
        {
            StackTrace stackTrace = new StackTrace(true);
            // The position 3 is relative to the index of the specified method
            StackFrame stackFrame = stackTrace.GetFrame(3);
            return (stackFrame.GetMethod().DeclaringType.Name
                + "/" + stackFrame.GetMethod().Name
                + "/" + stackFrame.GetFileLineNumber()
                + ":");
        }
        private static void SetLevel(string loggerName, string levelName)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.Level = l.Hierarchy.LevelMap[levelName];
        }        
        private static void AddAppender(string loggerName, IAppender appender)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.AddAppender(appender);
        }
        private static IAppender CreateFileAppender(string name, string fileName)
        {
            FileAppender appender = new FileAppender();
            appender.Name = name;
            appender.File = fileName;
            appender.AppendToFile = true;
            //PatternLayout layout = new PatternLayout();

            //layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
            //layout.ActivateOptions();
            //appender.Layout = layout;
            appender.ActivateOptions();
            return appender;
        }

    }
}

my application runs on a customized client framework, the client framework uses log4net to log their own log files. We are(our application) has to use the same log4net to log our log files in our own path(say our customized path). currently the our log files are created but log are not writing in that file. It is writing in the client framework log file.

I searched lot of sites the link
Log4Net: Programmatically specify multiple loggers (with multiple file appenders)
helped me to configure the log4net config programmatically,
still my log statements are not written in my log file.the code used as below

 public class TraceLog
    {
        private string message = string.Empty;        
        private static ILog ILogger = null; 
        private static TraceLog instance = new TraceLog();
        private TraceLog()
        {
            SetLevel("Log4net.MainForm", "ALL");
            AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
        }        
        public static TraceLog Instance
        {
            get
            {
                return instance;
            }
        }      
        public void Debug(string logMessage)
        {
            message = PrepareLog(logMessage);
            ILogger.Debug(message);
        }
        protected string PrepareLog(string logMessage)
        {
            string message = GetFileMethodLineNumberInfo();
            message += logMessage;
            return message;
        }      
        protected string GetFileMethodLineNumberInfo()
        {
            StackTrace stackTrace = new StackTrace(true);
            // The position 3 is relative to the index of the specified method
            StackFrame stackFrame = stackTrace.GetFrame(3);
            return (stackFrame.GetMethod().DeclaringType.Name
                + "/" + stackFrame.GetMethod().Name
                + "/" + stackFrame.GetFileLineNumber()
                + ":");
        }
        private static void SetLevel(string loggerName, string levelName)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.Level = l.Hierarchy.LevelMap[levelName];
        }        
        private static void AddAppender(string loggerName, IAppender appender)
        {
            ILogger = LogManager.GetLogger(loggerName);
            log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
            l.AddAppender(appender);
        }
        private static IAppender CreateFileAppender(string name, string fileName)
        {
            FileAppender appender = new FileAppender();
            appender.Name = name;
            appender.File = fileName;
            appender.AppendToFile = true;
            //PatternLayout layout = new PatternLayout();

            //layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
            //layout.ActivateOptions();
            //appender.Layout = layout;
            appender.ActivateOptions();
            return appender;
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-08-02 15:24:26

我对这个问题的解决方案:

public static void AddAppenderToLogger(string loggerName, string fileName) {
    FileAppender appender = new FileAppender();
    appender.Name = string.Format("appender_{0}", loggerName);
    appender.File = fileName;
    appender.AppendToFile = true;
    appender.Layout = new PatternLayout("%date - %message%newline");
    appender.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
    appender.ActivateOptions();
    ILoggerRepository repository = LogManager.CreateRepository(string.Format("repository_{0}",loggerName));
    log4net.Config.BasicConfigurator.Configure(repository, appender);
}
public static ILog GetLogger(string loggerName){
    return LogManager.GetLogger(string.Format("repository_{0}", loggerName), loggerName);
}

//Using in application code:
    static void Main(string[] args) {
        AddAppenderToLogger("test", @"c:\testLog.txt");
        ILog log = GetLogger("test");
        log.Info("TestRecord");           
    }

My resolve for this problem:

public static void AddAppenderToLogger(string loggerName, string fileName) {
    FileAppender appender = new FileAppender();
    appender.Name = string.Format("appender_{0}", loggerName);
    appender.File = fileName;
    appender.AppendToFile = true;
    appender.Layout = new PatternLayout("%date - %message%newline");
    appender.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
    appender.ActivateOptions();
    ILoggerRepository repository = LogManager.CreateRepository(string.Format("repository_{0}",loggerName));
    log4net.Config.BasicConfigurator.Configure(repository, appender);
}
public static ILog GetLogger(string loggerName){
    return LogManager.GetLogger(string.Format("repository_{0}", loggerName), loggerName);
}

//Using in application code:
    static void Main(string[] args) {
        AddAppenderToLogger("test", @"c:\testLog.txt");
        ILog log = GetLogger("test");
        log.Info("TestRecord");           
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文