using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Core;
using log4net.Appender;

namespace ChuPiao.Common.Utils
{
    public class Log4netUtil
    {
        /// <summary>
        /// 根据loggername找到对应的FileAppend的路径
        /// </summary>
        /// <param name="loggername">loggername</param>
        /// <returns>路径，如：D:\data\logs\taskserver\</returns>
        public static string FileAppendPath(string loggername)
        {
            string logpath = "";

            ILog ilog = LogManager.GetLogger(loggername);
            LogImpl logimpl = ilog as LogImpl;
            if (null != logimpl)
            {
                AppenderCollection ac = ((log4net.Repository.Hierarchy.Logger)logimpl.Logger).Appenders;
                foreach (IAppender appender in ac)
                {
                    if (appender is log4net.Appender.FileAppender)
                    {
                        FileAppender fa = appender as FileAppender;
                        logpath = fa.File;  //D:\data\logs\taskserver\2011-07-07/taskserver-20110707-12.txt
                        if (-1 != logpath.IndexOf("/"))
                        {
                            logpath = logpath.Remove(logpath.LastIndexOf("\\") + 1);
                        }
                        else
                        {
                            logpath = logpath.EndsWith("\\") ? logpath : logpath + "\\";
                        }
                        break;
                    }
                }
            }
            return logpath;
        }
    }
}
