Ejemplo n.º 1
0
        /*------------------------------------- Constructors ------------------------------------*/
        /// <summary>
        /// FileLogger的构造器
        /// </summary>
        ///     <param name="filePath">文件完整路径,可以不填,默认生成在当前根目录/log/下</param>
        ///     <param name="level">logger记录的最低级别</param>
        public FileLogger(string filePath = null, LoggerLevel level = LoggerLevel.Debug) : base(level)
        {
            if (filePath == null)
            {
                // TODO: 10-A 获取程序运行的根目录
                string root = Environment.CurrentDirectory;

                // TODO: 10-B 创建Log文件夹
                if (!Directory.Exists(root + @"/log/"))
                {
                    Directory.CreateDirectory(root + @"/log/");
                }

                this.FilePath = root + @"/log/" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".log";
            }
            else
            {
                this.FilePath = filePath;
            }

            // TODO: 10-C 创建log文件
            using (StreamWriter writer = new StreamWriter(this.FilePath)) {
                writer.WriteLineAsync(BaseLogger.FormatMessage(MessageType.Infor, "Logger File is Created...", true, nameof(FileLogger), "Created by Constructor", 46));
            }
        }
Ejemplo n.º 2
0
        // TODO: 09-D 打印日志 WriteDebug, WriteInfo, WriteError, WriteFatal
        /// <summary>
        /// 打印一条新的日志消息
        /// </summary>
        ///     <param name="type">消息类型</param>
        ///     <param name="message">消息的具体内容</param>
        ///     <param name="isDetailMode">详细模式?</param>
        ///     <param name="callerName">调用的方法的名字</param>
        ///     <param name="fileName">调用方法所在的文件名</param>
        ///     <param name="line">调用代码所在行</param>
        /// <returns>[true]->打印成功</returns>
        private static bool WriteLine
        (
            MessageType type,
            string message,
            bool isDetailMode,
            string callerName,
            string fileName,
            int line
        )
        {
            string msg      = BaseLogger.FormatMessage(type, message, isDetailMode, callerName, fileName, line);
            bool   isWrited = false;

            if (Loggers.Any())
            {
                isWrited = true;
                Loggers.ForEach(logger => isWrited &= logger.WriteLine(msg, type));
            }
            return(isWrited);
        }