• C# 异步日志记录类,方便下次使用,不用重复造轮子


    先定义接口类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 异常
    {
        internal interface ILog
        {
            Task WriteErrorLog(string message);
    
            Task WriteInfoLog(string message);
    
            Task WriteLog(string logType, string message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在去实现:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 异常
    {
        internal class Log : ILog
        {
            public string LogDirectory { get; set; }
            public string TimeDirName { get; set; }
    
            private  string LogFileName { get; set; }
    
            private const long maxLogSize = 2*1024*1024; // 2 MB
    
            private string FirstLogFileName { get; set; }
            public Log() 
            {
                //在根目录创建Log文件夹
                IOTools.CreatLogDir("Log");
                //初始化
                LogDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Log");
                TimeDirName = DateTime.Now.ToString("yyyyMMdd");
                FirstLogFileName = $"log_{DateTime.Now:yyyyMMddHHmmss}.txt";
            }
            public async Task WriteErrorLog(string message)
            {
                 await WriteLog("Error",message);
            }
    
            public async Task WriteInfoLog(string message)
            {
                await WriteLog("Info", message);
            }
    
            public async Task WriteLog(string logType ,string message)
            {
                //创建文件夹
                string dirType = TimeDirName + "\\" + logType;
                IOTools.CreatDir(dirType, LogDirectory);
                LogFileName=Path.Combine(LogDirectory, dirType, FirstLogFileName);
                if (!File.Exists(LogFileName))
                {
                    using (StreamWriter sw = File.CreateText(LogFileName))
                    {
                       await sw.WriteLineAsync($"【{logType}{DateTime.Now} \r\n {message}  \r\n \r\n");
                    }
                }
                else
                {
                    FileInfo fileInfo = new FileInfo(LogFileName);
                    if (fileInfo.Length > maxLogSize)
                    {
                        string newFileName = $"log_{DateTime.Now:yyyyMMddHHmmss}.txt";
                        LogFileName = Path.Combine(LogDirectory, dirType, newFileName);
                        using (StreamWriter sw = File.CreateText(LogFileName))
                        {
                            await sw.WriteLineAsync($"【{logType}{DateTime.Now} \r\n {message}  \r\n \r\n");
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(LogFileName))
                        {
                            await sw.WriteLineAsync($"【{logType}{DateTime.Now} \r\n {message}  \r\n \r\n");
                        }
                    }
                }
            }
    
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    工具类:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    
    namespace 异常
    {
        internal  class IOTools
        {
            public static void CreatLogDir(string name)
            {
                string rootDirectory = Directory.GetCurrentDirectory();
                CreatDir(name, rootDirectory);
            }
    
            public static void CreatDir(string name , string path)
            {
                if(string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
                if(string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
                string logPath = Path.Combine(path, name);
                // 判断文件夹是否存在
                if (!Directory.Exists(logPath))
                {
                    // 在当前项目根目录创建一个新的文件夹
                    Directory.CreateDirectory(logPath);
                }
    
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    最后实现的效果,简洁明了
    如图:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    pytorch安装记录
    验收测试的内容和流程有哪些?
    pyTorch——基础学习笔记
    JAVASE(复习)——static
    接口测试利器——APIFox的调研报告
    Mysql相关操作命令合集
    java计算机毕业设计springboot+vue郑州经贸学院迎新系统
    未来属于 Firefly:通过最新的生成式 AI 创新解锁新的创造力水平
    刚开始做斗音掌握这5点至少让你少走半年弯路
    Camera1 源码解析系列(三)—— Camera1 hw_get_module() 解析
  • 原文地址:https://blog.csdn.net/csdn2990/article/details/134333350