读取日志文件

2 篇文章 0 订阅
 /// <summary>
    /// Represents the log level, level of value 4 is the most important
    /// </summary>
    public enum LogLevel : int
    {
        LvlConfig   = 6,    //登录,修改配置操作的记录
        LvlFolow    = 5,    //流程中的记录
        LvlMax      = 4,
        LvlHigh     = 3,
        LvlMedium   = 2,
        LvlLow      = 1,
        LvlMin      = 0,
    } 

//读取日志文件

 public string ReadLogFile(bool message)
        {
            String pathDirLog = ApplicationStructureManager.Instance().GetApplicationLogFolder();
           
            //LogFile Path
            //String logFileName = ApplicationStructureManager.Instance().GetApplicationLogName();
            String logFileName = String.Format(ApplicationStructureManager.Instance().GetApplicationLogName(), DateTime.Now.DayOfWeek.ToString());

            FileManager.Instance().CreateDirectoryFromPath(pathDirLog);
            String pathLog = pathDirLog + logFileName;
            FileManager.Instance().CreateFileFromPath(pathLog, false);
            string readLogFile=File.ReadAllText(pathLog);
         
            this.logFile = FileManager.Instance().GetFileInfoFromPath(pathLog);
            //Default mode
            //TMP should be modified form the settings after
            this.LogLevel = LogLevel.LvlMin;//TMP

            //TMP DEV
            this.printToConsole = true;
            return readLogFile;
        }

 

/// <summary>
        /// Return the log folder path for the application
        /// </summary>
        /// <returns>the path of the logs</returns>
        public String GetApplicationLogFolder()
        {
            String logPath = null;

            XmlNode root = this.xmlAppStructure.GetElementsByTagName(XmlTypes.ROOT_ELE)[0];
            logPath = root.Attributes[XmlTypes.ROOT_ATT_LOG_PAT].Value;
           
            //TMP LOG If not found ?!
            return logPath;
        }

 /// <summary>
        /// Return the name of the logfile
        /// </summary>
        /// <returns>the name of the log file</returns>
        public String GetApplicationLogName()
        {
            String logName = null;

            XmlNode root = this.xmlAppStructure.GetElementsByTagName(XmlTypes.ROOT_ELE)[0];
            logName = root.Attributes[XmlTypes.ROOT_ATT_LOG_NAM].Value;

            //TMP LOG If not found ?!
            return logName;
        }

 /// <summary>
        /// Create a Directory from the given path
        /// </summary>
        /// <param name="pPath">Directory complete path</param>
        /// <returns>The DirectoryInfo corresponding to the directory, or null otherwise</returns>
        public DirectoryInfo CreateDirectoryFromPath(String pPath)
        {
            DirectoryInfo di = null;

            if (!String.IsNullOrEmpty(pPath))
            {
                if (!Directory.Exists(pPath))
                {
                    Directory.CreateDirectory(pPath);
                }
                di = new DirectoryInfo(pPath);
            }

            return di;
        }

   /// <summary>
        /// Create a File from the given path
        /// </summary>
        /// <param name="pPath">File complete path</param>
        /// <param name="overwrite">overwrite if existing file found</param>
        /// <returns>The FileInfo corresponding to the path, or null otherwise</returns>
        public FileInfo CreateFileFromPath(String pPath, Boolean pOverwrite)
        {
            FileInfo fi = null;

            if (!String.IsNullOrEmpty(pPath))
            {
                // if the file doesn't exist or must overwrite
                if (!File.Exists(pPath) || pOverwrite)
                {
                    // close the stream else the file is locked
                    FileStream fs = File.Create(pPath);
                    fs.Close();
                }

                fi = new FileInfo(pPath);
            }

            return fi;
        }

     /// <summary>
        /// Return the File information object for a given path
        /// </summary>
        /// <param name="pPath">The path of the file</param>
        /// <returns>The FileInfo of the corresponding file if found, null otherwise</returns>
        public FileInfo GetFileInfoFromPath(String pPath)
        {
            FileInfo fi = null;

            if (!String.IsNullOrEmpty(pPath))
            {
                if (File.Exists(pPath))
                {
                    fi = new FileInfo(pPath);
                }
            }

            return fi;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值