欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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# Onnx實現(xiàn)DIS高精度圖像二類分割

    C# Onnx實現(xiàn)DIS高精度圖像二類分割

    這篇文章主要為大家詳細(xì)介紹了C# Onnx實現(xiàn)DIS高精度圖像二類分割的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • C#中string用法實例詳解

    C#中string用法實例詳解

    這篇文章主要介紹了C#中string用法,非常詳細(xì)的總結(jié)了比較常見的關(guān)于C#中string的幾個常用方法,需要的朋友可以參考下
    2014-08-08
  • 一文掌握C# JSON(2023最新整理)

    一文掌握C# JSON(2023最新整理)

    JSON的全稱是JavaScript Object Notation,意思是JavaScript對象表示法,它是一種基于文本,獨立于語言的輕量級數(shù)據(jù)交換格式,這篇文章主要介紹了C#中的JSON(2023最新整理),需要的朋友可以參考下
    2023-05-05
  • C#微信分享代碼

    C#微信分享代碼

    這篇文章主要為大家詳細(xì)介紹了C#微信分享的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟

    C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟

    這篇文章主要介紹了C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟,文中通過圖文結(jié)合的方式介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • C#中的let字句應(yīng)用示例

    C#中的let字句應(yīng)用示例

    這篇文章主要給大家介紹了C#中的let字句,文中通過應(yīng)用實例介紹的很詳細(xì),相信對大家具有一定的參考價值,有需要的朋友們下面來一起看看吧。
    2017-02-02
  • C#?使用PrintDocument類打印標(biāo)簽的方法

    C#?使用PrintDocument類打印標(biāo)簽的方法

    本文介紹打印機初步配置,以及實現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對C#?使用PrintDocument類打印標(biāo)簽的詳細(xì)過程感興趣的朋友一起看看吧
    2022-04-04
  • C#之CLR內(nèi)存字符串常量池(string)

    C#之CLR內(nèi)存字符串常量池(string)

    這篇文章主要介紹了C#之CLR內(nèi)存字符串常量池(string),對于學(xué)習(xí)和理解C#內(nèi)存原理很有幫助,需要的朋友可以參考下
    2014-08-08
  • C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例

    C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例

    下面小編就為大家分享一篇C# 字符串、數(shù)組和List的截取和轉(zhuǎn)換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • C#中Linq延遲查詢的例子

    C#中Linq延遲查詢的例子

    這篇文章主要介紹了C#中Linq延遲查詢的例子,本文用一個實例來講解延遲查詢的使用,需要的朋友可以參考下
    2015-06-06

最新評論