【C#通用類】日志記錄類


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace KTCommon.LOG
{
    public class TraceLog
    {
        /// <summary>
        /// 全局日志對象
        /// </summary>
        public static TraceLog m_Trace = new TraceLog((HttpRuntime.AppDomainAppId == null) ?
            "d://LOG" : HttpRuntime.AppDomainAppPath + "//", "KTGJ");

        public string m_LogFilePath = "";//當前日志文件路徑

        private string m_xmlPath = "";              //Log的目錄

        private string m_FileNamePrefix = "";
        StreamWriter SW;

        public TraceLog(string filePath, string fileNamePrefix)
        {
            m_xmlPath = filePath;
            m_FileNamePrefix = fileNamePrefix;
        }

        #region //將顯示的提示信息寫到Log文件
        public void Trace(string tipMsg)
        {
            string nodeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Trace(nodeTime, tipMsg);
            tipMsg = null;
        }
        public void Trace(string nodeTime, string tipMsg)
        {
            try
            {
                //一小時寫一個文件
                string strNowY = DateTime.Now.Year.ToString();
                string strNowM = DateTime.Now.Month.ToString();
                string strNowD = DateTime.Now.Day.ToString();
                string strNowH = DateTime.Now.Hour.ToString();
                string fileName = m_FileNamePrefix + "_" + strNowH + "0000.log";
                string filePath = m_xmlPath + "\\LOG\\" + strNowY + "\\" + strNowM + "\\" + strNowD + "\\";
                if (nodeTime != "")
                {
                    nodeTime = "[" + nodeTime + "] ";
                }

                //LOG目錄不存在,則創建

                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }

                m_LogFilePath = filePath + fileName;
                //日志文件不存在,則創建

                if (File.Exists(filePath + fileName) == false)
                {
                    if (SW != null)
                    {
                        SW.Flush();
                        SW.Close();
                    }
                    File.Create(filePath + fileName).Close();
                    SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8);
                }
                //創建實例
                if (SW == null)
                {
                    SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8);
                }
                //將內容寫到log文件中

                SW.WriteLine(nodeTime + tipMsg);
                //刷新,實時保存

                SW.Flush();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("TraceLog Error:" + ex.Message.ToString());
            }
        }
        #endregion   //將消息寫到Log文件
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM