1. 程式人生 > 其它 >C# 寫日誌幫助類

C# 寫日誌幫助類

/*-----------------------------------------------------------------
 * 作  者(Author):             Dennis
 * 日  期(Create Date):        2021/9/3 16:11:39
 * 公  司(Copyright):          www.dennisdong.top
 * 檔名(File Name):          LogFileHelper
 * ----------------------------------------------------------------
 * 描  述(Description):		  
 *----------------------------------------------------------------*/

using System;
using System.IO;

namespace Anta.Common.Helpers
{
    /// <summary>
    /// 自定義檔案寫入
    /// </summary>
    public class LogFileHelper
    {
        private static readonly object LogLock = new object();

        /// <summary>
        /// 日誌預設存放路徑
        /// </summary>
        private static readonly string LogPath = $@"{AppDomain.CurrentDomain.BaseDirectory}\LogFiles\";

        #region WriteLog

        /// <summary>
        /// 只記錄資訊
        /// </summary>
        /// <param name="msg">內容</param>
        public static void WriteLog(string msg)
        {
            WriteLog(null, null, msg);
        }

        /// <summary>
        /// 路徑加資訊
        /// </summary>
        /// <param name="filePath">相對路徑</param>
        /// <param name="msg">內容</param>
        public static void WriteLog(string filePath, string msg)
        {
            WriteLog(filePath, null, msg);
        }

        /// <summary>
        /// 寫入日誌
        /// </summary>
        /// <param name="filePath">相對路徑</param>
        /// <param name="fileName">檔名</param>
        /// <param name="msg">內容</param>
        public static void WriteLog(string filePath, string fileName, string msg)
        {
            lock (LogLock)
            {
                try
                {
                    if (string.IsNullOrEmpty(filePath))
                    {
                        filePath = "LogDefault";
                    }

                    filePath = LogPath + filePath;
                    if (!Directory.Exists(filePath))//判斷是否有該檔案  
                        Directory.CreateDirectory(filePath);

                    if (string.IsNullOrEmpty(fileName))
                    {
                        fileName = DateTime.Now.ToString("yyyyMMdd");
                    }
                    var logFileName = filePath + "/" + fileName + ".log";//生成日誌檔案  

                    var fs = !File.Exists(logFileName) ? new FileStream(logFileName, FileMode.Create) : new FileStream(logFileName, FileMode.Append);

                    var sw = new StreamWriter(fs);
                    sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} \n{msg}");
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception e)
                {
                    var path = LogPath + "/LogError";

                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    var logFileName = path + "/" + DateTime.Now.ToString("yyyyMMdd") + ".log";

                    var fs = !File.Exists(logFileName) ? new FileStream(logFileName, FileMode.Create) : new FileStream(logFileName, FileMode.Append);

                    var sw = new StreamWriter(fs);
                    sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} \n{msg} \n {e.Message}");
                    sw.Flush();
                    sw.Close();
                }
            }
        }

        #endregion
    }
}