C#寫(xiě)日志類(lèi)實(shí)例
本文實(shí)例講述了C#寫(xiě)日志類(lèi),分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
using System.Configuration;
using System.IO;
using System.Threading;
namespace FQDService.Utils
{
/// <summary>
/// 寫(xiě)日志類(lèi)
/// </summary>
public class FileLogger
{
#region 字段
public static readonly object _lock = new object();
#endregion
#region 寫(xiě)文件
/// <summary>
/// 寫(xiě)文件
/// </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 寫(xiě)日志
/// <summary>
/// 寫(xiě)日志
/// </summary>
public static void WriteLog(string log)
{
string logPath = ConfigurationManager.AppSettings["LogPath"] + "\\FQDService_Log.txt";
WriteFile(log, logPath);
}
#endregion
#region 寫(xiě)錯(cuò)誤日志
/// <summary>
/// 寫(xiě)錯(cuò)誤日志
/// </summary>
public static void WriteErrorLog(string log)
{
string logPath = ConfigurationManager.AppSettings["LogPath"] + "\\FQDService_ErrorLog.txt";
WriteFile(log, logPath);
}
#endregion
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#處理文本文件TXT實(shí)例詳解
- C#中把日志導(dǎo)出到txt文本的簡(jiǎn)單實(shí)例
- C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中
- C#實(shí)現(xiàn)將日志寫(xiě)入文本文件的方法
- c#實(shí)現(xiàn)把異常寫(xiě)入日志示例(異常日志)
- C#實(shí)現(xiàn)將程序運(yùn)行信息寫(xiě)入日志的方法
- C#實(shí)現(xiàn)寫(xiě)系統(tǒng)日志的方法
- C#記錄消息到日志文件的方法
- C#實(shí)現(xiàn)一個(gè)簡(jiǎn)單實(shí)用的TXT文本操作及日志框架詳解
相關(guān)文章
C#使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成
這篇文章主要為大家詳細(xì)介紹了C#如何使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10分享我在工作中遇到的多線(xiàn)程下導(dǎo)致RCW無(wú)法釋放的問(wèn)題
最近在做項(xiàng)目中遇到一個(gè)問(wèn)題,在調(diào)用一個(gè)類(lèi)庫(kù)中的方法時(shí),出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動(dòng)線(xiàn)程或其他線(xiàn)程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會(huì)導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12C#運(yùn)用FileInfo類(lèi)實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類(lèi)實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07c#編寫(xiě)的高并發(fā)數(shù)據(jù)庫(kù)控制訪(fǎng)問(wèn)代碼
往往大數(shù)據(jù)量,高并發(fā)時(shí), 瓶頸都在數(shù)據(jù)庫(kù)上, 好多人都說(shuō)用數(shù)據(jù)庫(kù)的復(fù)制,發(fā)布, 讀寫(xiě)分離等技術(shù), 但主從數(shù)據(jù)庫(kù)之間同步時(shí)間有延遲.2015-03-03c#.net多線(xiàn)程編程教學(xué)——線(xiàn)程同步
c#.net多線(xiàn)程編程教學(xué)——線(xiàn)程同步...2007-04-04C# 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