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#通過Win32API設(shè)置客戶端系統(tǒng)時間的方法詳解
在日常工作中,有時可能會需要獲取或修改客戶端電腦的系統(tǒng)時間,比如軟件設(shè)置了Licence有效期,本文以一個簡單的小例子,簡述如何通過C#獲取和設(shè)置客戶端電腦的系統(tǒng)時間,僅供學(xué)習(xí)分享使用,如有不足之處,還請指正,需要的朋友可以參考下2024-06-06
C#中DataTable的創(chuàng)建與遍歷實現(xiàn)
這篇文章主要介紹了C#中DataTable的創(chuàng)建與遍歷實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C# Winform 調(diào)用系統(tǒng)接口操作 INI 配置文件的代碼
封裝了一小段代碼, 調(diào)用系統(tǒng)接口, 操作配置文件. 一般用于 .ini 文件, 或者其它鍵值對格式的配置文件2011-05-05

