C#文本日誌類

C#文本日誌類

說明

本類適合C#項目在本地記錄文本文件,日誌文件已經按照年月/日/類型分文件夾存儲,因此不會產生單個大文件。固然,若是項目中有記錄大量日誌的邏輯,那麼產生單個大的日誌文件也是沒辦法的事情。本類是單線程日誌記錄類,正在計劃完善稱爲多線程而且更爲規範的日誌記錄方式。git

項目地址

點我打開多線程

代碼

基礎類SaveTextLog.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Common
{
    public class SaveTextLog
    {
        private static string lockString = "ab3cdef1ghi5jklmno4pqrstuvw23xyz123456ds789098e7654321zyxabc;;;asd0fasdfa";
        private static string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;   //存儲在本程序目錄下

        /// <summary>
        /// 記錄錯誤消息到文件日誌
        /// </summary>
        /// <param name="error">錯誤消息</param>
        public static void WriteErrorLog(string error)
        {
            WriteToLog(path, "Error", error);
        }
        /// <summary>
        /// 記錄操做日誌到文件日誌
        /// </summary>
        /// <param name="message">操做日誌</param>
        public static void WriteOperateLog(string message)
        {
            WriteToLog(path, "Operate", message);
        }

        /// <summary>
        /// 記錄系統操做
        /// </summary>
        /// <param name="message">系統操做消息</param>
        public static void WriteSystemLog(string message)
        {
            WriteToLog(path, "System", message);
        }

        public static void WriteException(string message)
        {
            WriteToLog(path, "Exception", message);
        }

        private static void WriteToLog(string filePath, string fileName, string message)
        {
            if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
            {
                filePath = System.AppDomain.CurrentDomain.BaseDirectory;
            }
            try
            {
                lock (lockString + fileName)
                {
                    if (!Directory.Exists(filePath + "log\\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString()))
                    {
                        Directory.CreateDirectory(filePath + "log\\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString());
                    }
                    using (StreamWriter sw = new StreamWriter(filePath + "log\\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + fileName + "_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt", true, System.Text.Encoding.Default))
                    {
                        sw.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "]\r\n" + message);
                        sw.Flush();
                        sw.Dispose();
                    }
                }
            }
            catch
            {
                //已經沒法再次處理了
            }
        }
    }
}

擴展類SaveTextLogExtend.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 日誌擴展類
    /// </summary>
    public class SaveTextLogExtend
    {
        /// <summary>
        /// 輸出異常到文本文件
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <param name="ex">異常</param>
        public static void WriteErrorLog(string methodName, Exception ex)
        {
            SaveTextLog.WriteErrorLog("{" + methodName + "}\r\n" + ex);
        }

        /// <summary>
        /// 輸出異常到文本文件
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <param name="errorDescription">異常</param>
        public static void WriteErrorLog(string methodName, string errorDescription)
        {
            SaveTextLog.WriteErrorLog("{" + methodName + "}\r\n" + errorDescription);
        }

        /// <summary>
        /// 寫日誌
        /// </summary>
        /// <param name="methodName">方法名</param>
        /// <param name="message">日誌消息</param>
        public static void WriteOperateLog(string methodName, string message)
        {
            SaveTextLog.WriteOperateLog("{" + methodName + "}\r\n" + message);
        }
        /// <summary>
        /// 寫系統操做日誌
        /// </summary>
        /// <param name="methodName">方法</param>
        /// <param name="message">消息</param>
        public static void WriteSystemLog(string methodName, string message)
        {
            SaveTextLog.WriteSystemLog("{" + methodName + "}\r\n" + message);
        }

        /// <summary>
        /// 記錄異常
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteException(Exception ex)
        {
            SaveTextLog.WriteException("{" + ex.TargetSite + "}\r\n" + ex);
        }
    }
}
相關文章
相關標籤/搜索