C#寫日志類實(shí)例
本文實(shí)例講述了C#寫日志類,分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
using System.Configuration;
using System.IO;
using System.Threading;
namespace FQDService.Utils
{
/// <summary>
/// 寫日志類
/// </summary>
public class FileLogger
{
#region 字段
public static readonly object _lock = new object();
#endregion
#region 寫文件
/// <summary>
/// 寫文件
/// </summary>
public static void WriteFile(string log, string path)
{
Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
{
lock (_lock)
{
if (!File.Exists(path))
{
using (FileStream fs = new FileStream(path, FileMode.Create)) { }
}
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
#region 日志內(nèi)容
string value = string.Format(@"{0}
--------------------------------------------------------
{1}
", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), obj.ToString());
#endregion
sw.WriteLine(value);
sw.Flush();
}
}
}
}));
thread.Start(log);
}
#endregion
#region 寫日志
/// <summary>
/// 寫日志
/// </summary>
public static void WriteLog(string log)
{
string logPath = ConfigurationManager.AppSettings["LogPath"] + "\\FQDService_Log.txt";
WriteFile(log, logPath);
}
#endregion
#region 寫錯(cuò)誤日志
/// <summary>
/// 寫錯(cuò)誤日志
/// </summary>
public static void WriteErrorLog(string log)
{
string logPath = ConfigurationManager.AppSettings["LogPath"] + "\\FQDService_ErrorLog.txt";
WriteFile(log, logPath);
}
#endregion
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成
這篇文章主要為大家詳細(xì)介紹了C#如何使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10分享我在工作中遇到的多線程下導(dǎo)致RCW無(wú)法釋放的問題
最近在做項(xiàng)目中遇到一個(gè)問題,在調(diào)用一個(gè)類庫(kù)中的方法時(shí),出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動(dòng)線程或其他線程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會(huì)導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07c#編寫的高并發(fā)數(shù)據(jù)庫(kù)控制訪問代碼
往往大數(shù)據(jù)量,高并發(fā)時(shí), 瓶頸都在數(shù)據(jù)庫(kù)上, 好多人都說(shuō)用數(shù)據(jù)庫(kù)的復(fù)制,發(fā)布, 讀寫分離等技術(shù), 但主從數(shù)據(jù)庫(kù)之間同步時(shí)間有延遲.2015-03-03C# Socket網(wǎng)絡(luò)編程實(shí)例
這篇文章主要介紹了C# Socket網(wǎng)絡(luò)編程實(shí)例,分析了Socket網(wǎng)絡(luò)通信的原理與具體應(yīng)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01C#使用iTextSharp設(shè)置PDF所有頁(yè)面背景圖功能實(shí)例
這篇文章主要介紹了C#使用iTextSharp設(shè)置PDF所有頁(yè)面背景圖功能,實(shí)例分析了C#使用iTextSharp設(shè)置PDF頁(yè)面背景圖的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C#實(shí)現(xiàn)漢字轉(zhuǎn)漢語(yǔ)拼音的示例代碼
這篇文章主要介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)漢語(yǔ)拼音,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定幫助,感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-03-03