OctopusKing

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 
    /// <summary>
    /// 日志服务
    /// </summary>
    public interface ILog
    {

        void Write(LogType logType,
            string message = "",
            [CallerFilePath] string filePath = "",
            [CallerMemberName] string methodName = "",
            [CallerLineNumber] int lineNum = 0);
    }

 

其中,LogType是日志的级别,message是日志的具体信息,可以调用的时候载入。

[CallerFilePath]特性,当某个方法A调用此方法(Write方法)时,自动将A方法所在的文件路径赋值给filePath参数,然后便可以将此路径写入到日志文件中

[CallerMemberName]特性,与CallerFilePath类似,自动将调用Write方法的方法名赋值给methodName参数

[CallerLineNumber]特性,同上,拿到调用方法在该文件中的行数

 

  public class Log : ILog
    {
        /// <summary>
        /// 向今天的Log日志中,填写Log信息
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="methodName"></param>
        /// <param name="filePath"></param>
        /// <param name="lineNum"></param>
        public void Write(LogType logType,
            string message = "",
            [CallerFilePath] string filePath = "",
            [CallerMemberName] string methodName = "",
            [CallerLineNumber] int lineNum = 0)
        {
            //  是否写日志
            if (Fields._isWriteLog)
            {
                string directoryPath = GetCurrentLogDirectoryPath();
                string path = GetCurrentLogFileFullPath();

                if (!DirectoryHelper.IsExistDirectory(directoryPath))
                {
                    DirectoryHelper.CreateDirectory(directoryPath);
                }

                switch (logType)
                {
                    case LogType.Info:
                        //  ToDo: 1. 添加往日志文件中写的方法
                        FileHelper.AppendLineToFile(path, "[ Info ] : " + DateTime.Now.ToString("T") +" " + message + ",方法名称:" + methodName);
                        break;
                    case LogType.Warning:
                        FileHelper.AppendLineToFile(path, "[ Warning ] : " + DateTime.Now.ToString("T") + " " + message + ",方法名称:" + methodName);
                        break;
                    case LogType.Error:
                        FileHelper.AppendLineToFile(path, "[ Error ] : " + DateTime.Now.ToString("T") + " " + message + "\n" +
                            "\t位置信息:" + filePath +
                            ",引发行数:" + lineNum +
                            ",方法名称:" + methodName);
                        break;
                    case LogType.Fatal:
                        FileHelper.AppendLineToFile(path, "[ Fatal ] : " + DateTime.Now.ToString("T") + " " + message + "\n" +
                            "\t位置信息:" + filePath +
                            ",引发行数:" + lineNum +
                            ",方法名称:" + methodName);
                        break;
                    default:
                        break;
                }
            }
        }

        private string GetCurrentLogDirectoryPath()
        {
            string fileFullPath = GetCurrentLogFileFullPath();

            string[] strList = fileFullPath.Split('\\');
            strList[strList.Length - 1] = "";

            string path = string.Join("\\", strList);

            return path.Trim();
        }

        /// <summary>
        /// 获取今天的Log日志文件路径及文件名称
        /// </summary>
        /// <returns></returns>
        private string GetCurrentLogFileFullPath()
        {
            return Fields._logDataDirectory + "\\" + DateTime.Now.ToString("D") + "\\" + DateTime.Now.ToString("D") + ".txt";
        }
    }
 

 

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogType
    {
        Info = 1,
        Warning = 2,
        Error = 3,
        Fatal = 4,
    }

 

定义一个日志的实现类,该类提供一个写日志的方法。然后在需要打日志的地方调用该方法,例如:

        public static void CreateFile(string filePath)
        {
            try
            {
                FileLower.CreateFile(filePath);
            }
            catch (FileException fe)
            {
                _log.Write(LogType.Error, fe.Message);
            }
            catch (Exception ex)
            {
                _log.Write(LogType.Error, ex.Message);
            }
        }

 

如果创建目标文件失败的时候,可以调用打日志的方法,将失败的信息记录下来。

注:如果在创建文件的方法中打日志,并且日志写的时候发现文件不存在,然后又需要创建日志文件,此时会进入死循环,然后抛出异常:StackOverFlow,爆栈。这个时候需要处理一下,防止进入死循环。

 

另外,

1.可以再记录一下,日志总共写了多少行,比如1W行,重新生成一个日志文件(日志文件名以当前时间为名称作为区分)。

2.也可以将记录的天数做一个判定,比如只保存一个月内的记录,到时间后就将其删除掉。

posted on 2023-03-09 21:47  八爪鱼~  阅读(18)  评论(0编辑  收藏  举报