loghelper.cs 代碼

唉,網上處處找一圈,真是麻煩,本身結合別人寫的,從新整理一個。 這個破玩意最大的做用就是寫微信那種無法順利斷點調試的程序的時候,在須要的地方寫日誌,而後去查看。真是回到當年用DW4寫php的年代了,惋惜一點懷舊的喜悅都沒有,有的只是慢慢的無奈php

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

/// <summary>
/// 日誌文件存放文件夾分類枚舉
/// </summary>
    public enum LogType
    {
        /// <summary>
        /// 普通訊息
        /// </summary>
        Info,
        /// <summary>
        /// 錯誤
        /// </summary>
        Error,
        /// <summary>
        /// 其餘所有信息
        /// </summary>
        Overall,
    }

    public class LogHelper
    {
        /// <summary>
        /// 日誌存放路徑
        /// </summary>
        public static string LogPath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + @"\log";
            }
        }

        /// <summary>
        /// 信息類型
        /// </summary>
        public enum LogLevel
        {
            /// <summary>
            /// 普通訊息
            /// </summary>
            Info,
            /// <summary>
            /// 錯誤
            /// </summary>
            Error
        }

        /// <summary>
        /// 普通訊息寫入日誌
        /// </summary>
        /// <param name="message"></param>
        /// <param name="logType"></param>
        public static void Info(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "", message);
        }
        /// <summary>
        /// 自定義錯誤信息寫入
        /// </summary>
        /// <param name="message">自定義消息</param>
        /// <param name="logType">存儲目錄類型</param>
        public static void Error(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", message);
        }
        /// <summary>
        /// 程序異常信息寫入
        /// </summary>
        /// <param name="e">異常</param>
        /// <param name="logType">存儲目錄類型</param>
        public static void Error(Exception e, LogType logType = LogType.Overall)
        {
            if (e == null)
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", e.Message);
        }
        /// <summary>
        /// 寫日誌的最終執行動做
        /// </summary>
        /// <param name="path">文件路徑</param>
        /// <param name="prefix">前綴</param>
        /// <param name="message">內容</param>
        public static void WriteLog(string path, string prefix, string message)
        {
            path = LogPath + path;
            var fileName = string.Format("{0}{1}.log", prefix, DateTime.Now.ToString("yyyyMMdd"));

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (FileStream fs = new FileStream(path + fileName, FileMode.Append, FileAccess.Write,
                                                  FileShare.Write, 1024, FileOptions.Asynchronous))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("HH:mm:ss") + " " + message + "\r\n");
                IAsyncResult writeResult = fs.BeginWrite(buffer, 0, buffer.Length,
                    (asyncResult) =>
                    {
                        var fStream = (FileStream)asyncResult.AsyncState;
                        fStream.EndWrite(asyncResult);
                    },

                    fs);
                fs.Close();
            }
        }
    }
相關文章
相關標籤/搜索