日志工具类—LogUtil

作者:追风剑情 发布于:2020-9-3 10:42 分类:C#

using System;
using System.Text;
using System.IO;

namespace UNetwork
{
    /// <summary>
    /// 日志工具类
    /// </summary>
    public sealed class LogUtil
    {
        private static object lockObj = new object();

        //日志分类
        public enum LogType
        {
            Info,   //信息
            Trace,  //追踪
            Warn,   //警告
            Error,  //错误
            Fatal   //严重错误
        }

        //输出设备
        public enum LogDevice
        {
            Console, //输出到控制台
            File,    //输出到日志文件
            ConsoleAndFile //输出到控制台和日志文件
        }

        private static string LogFileName
        {
            get
            {
                string timeFormat = "yyyyMMddHH";
                string timeStr = DateTime.Now.ToString(timeFormat);
                return timeStr;
            }
        }

        private static string CurrentTimeStr
        {
            get
            {
                string timeFormat = "yyyy-MM-dd HH:mm:ss";
                string timeStr = DateTime.Now.ToString(timeFormat);
                return timeStr;
            }
        }

        private static string LogFilePath(LogType logType)
        {
            string logDirectory = "./Log";
            string logClassify = string.Format("{0}/{1}", logDirectory, logType.ToString());
            string logFile = string.Format("{0}/{1}.txt", logClassify, LogFileName);
            try
            {
                if (!Directory.Exists(logDirectory))
                    Directory.CreateDirectory(logDirectory);
                if (!Directory.Exists(logClassify))
                    Directory.CreateDirectory(logClassify);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Create Directory Failured\n{0}\n{1}", ex.Message, ex.StackTrace);
            }
            return logFile;
        }

        private static void Write(string log, LogType logType=LogType.Info, LogDevice logDevice=LogDevice.Console)
        {
            lock (lockObj)
            {
                //文字颜色
                switch (logType)
                {
                    case LogType.Fatal:
                    case LogType.Error:
                        Console.ForegroundColor = ConsoleColor.Red;
                        break;
                    case LogType.Trace:
                        Console.ForegroundColor = ConsoleColor.Green;
                        break;
                    case LogType.Warn:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.White;
                        break;
                }

                //是否需要输出到控制台
                string content = string.Format("[{0}] {1}\r\n", CurrentTimeStr, log);
                switch (logDevice)
                {
                    case LogDevice.Console:
                        Console.Write(content);
                        break;
                    case LogDevice.File:

                        break;
                    case LogDevice.ConsoleAndFile:
                        Console.Write(content);
                        break;
                }
                Console.ForegroundColor = ConsoleColor.White;

                //是否需要输出到日志文件
                if (logDevice == LogDevice.File || logDevice == LogDevice.ConsoleAndFile)
                {
                    string logFilePath = LogFilePath(logType);
                    try
                    {
                        File.AppendAllText(logFilePath, content, Encoding.UTF8);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Write Log Failured\n{0}\n{1}", ex.Message, ex.StackTrace);
                    }
                }
            }
        }

        /// <summary>
        /// 写入一般信息
        /// </summary>
        /// <param name="log"></param>
        public static void Info(string log, LogDevice logDevice = LogDevice.Console)
        {
            Write(log, LogType.Info, logDevice);
        }

        /// <summary>
        /// 写入追踪日志 (记录程序执行流程)
        /// </summary>
        /// <param name="log"></param>
        public static void Trace(string log, LogDevice logDevice = LogDevice.Console)
        {
            Write(log, LogType.Trace, logDevice);
        }

        /// <summary>
        /// 写入出错日志 (指会导致程序无法按预期执行的错误)
        /// </summary>
        /// <param name="log"></param>
        public static void Error(string log, LogDevice logDevice = LogDevice.Console)
        {
            Write(log, LogType.Error, logDevice);
        }

        /// <summary>
        /// 致命错误 (需要结束应用程序的错误)
        /// </summary>
        /// <param name="log"></param>
        public static void Fatal(string log, LogDevice logDevice = LogDevice.Console)
        {
            Write(log, LogType.Fatal, logDevice);
        }

        /// <summary>
        /// 写入警告日志
        /// </summary>
        /// <param name="log"></param>
        /// <param name="logDevice"></param>
        public static void Warn(string log, LogDevice logDevice = LogDevice.Console)
        {
            Write(log, LogType.Warn, logDevice);
        }
    }
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号