C#實現(xiàn)異步日志記錄類的示例代碼
更新時間:2023年11月10日 16:16:48 作者:愛吃奶酪的松鼠丶
這篇文章主要為大家詳細(xì)介紹了C#如何實現(xiàn)異步日志記錄類,從而方便下次使用,不用重復(fù)造輪子,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
先定義接口類:
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); } }
在去實現(xiàn):
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() { //在根目錄創(chuàng)建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) { //創(chuàng)建文件夾 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"); } } } } } }
工具類:
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)) { // 在當(dāng)前項目根目錄創(chuàng)建一個新的文件夾 Directory.CreateDirectory(logPath); } } } }
最后實現(xiàn)的效果,簡潔明了
如圖:
以上就是C#實現(xiàn)異步日志記錄類的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#異步日志記錄類的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟
這篇文章主要介紹了C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟,文中通過圖文結(jié)合的方式介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03C#?使用PrintDocument類打印標(biāo)簽的方法
本文介紹打印機初步配置,以及實現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對C#?使用PrintDocument類打印標(biāo)簽的詳細(xì)過程感興趣的朋友一起看看吧2022-04-04C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例
下面小編就為大家分享一篇C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11