LogHelper类

纯属个人记录,不供大家学习!

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace DPForAD2BPMTel

{

    public class LogHelper

    {

        #region 写文本日志

 

        /// <summary>

        /// 向日志文件中输出一个空行

        /// </summary>

        public void WriteSpaceLine()

        {

            WriteLog(string.Empty);

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="message">日志信息</param>

        /// /// <param name="logPath">日志路径</param>

        /// <param name="logName">日志文件名</param>

        public void WriteLog(string message, string logPath, string logName)

        {

            string logMsg = DateTime.Now.ToString("HH:mm:ss \t");

            string logFile = Path.Combine(logPath, logName); // logPath + "\\" + logName; //2008-09-05 修改

            //如果路径不存在,建立目录

            if (!Directory.Exists(logPath))

            {

                Directory.CreateDirectory(logPath);

            }

            //如果文件不存在,建立文件

            if (!File.Exists(logFile))

            {

                File.Create(logFile).Close();

            }

 

            logMsg += message;

 

            try

            {

                WriteToFile(logFile, logMsg);

            }

            catch

            {

                // do nothing

            }

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="message">日志信息</param>

        /// /// <param name="logPath">日志路径</param>

        public void WriteLog(string message, string logPath)

        {

            WriteLog(message, logPath, DateTime.Now.ToString("yyyy-MM-dd.LOG"));

        }

 

        /// <summary>

        /// 写日志

        /// </summary>

        /// <param name="sMessage">日志信息</param>

        public void WriteLog(string message)

        {

            WriteLog(message, AppDomain.CurrentDomain.BaseDirectory + "\\LOG");

        }

 

        /// <summary>

        /// 把信息写到指定文件的尾部

        /// </summary>

        /// <param name="fileName">文件完整路径</param>

        /// <param name="msg">信息文本</param>

        public void WriteToFile(string fileName, string msg)

        {

            StreamWriter swWriter = new StreamWriter(new FileStream(fileName, FileMode.Append));

            swWriter.WriteLine(msg);

            swWriter.Close();

        }

        #endregion

    }

}

 

posted on 2013-06-04 19:03  秋天  阅读(467)  评论(0编辑  收藏  举报

导航