一個(gè)簡單的自定義程序日志小樣例
更新時(shí)間:2009年07月30日 18:48:09 作者:
前面一篇文章大概說了下自己對(duì)日志的一點(diǎn)理解,可能不太直觀,這里再附上一個(gè)簡單的使用例子,以作為對(duì)之前的補(bǔ)充,例子比較簡單,所以直接看注釋即可。
復(fù)制代碼 代碼如下:
using System;
using System.IO;
using System.Text;
public class LogInfo
{
private string ErrorInfo_User = ""; // 記錄用戶自定義錯(cuò)誤信息
private string ErrorPosition = ""; // 記錄錯(cuò)誤的位置信息,可包括類、函數(shù)等
private string ErrorInfo_Sys = ""; // 記錄系統(tǒng)產(chǎn)生的異常錯(cuò)誤信息
// 記錄日志信息
public void RecordErrorInfo(string Position, string Error_Sys, string Error_User)
{
ErrorPosition = Position;
ErrorInfo_Sys = Error_Sys;
ErrorInfo_User = Error_User;
}
// 自定義日志信息格式
private string GetLogContent()
{
string LogContent = "\r\n--------------------------------------------------------------------------\r\n";
LogContent += "[自定義異常日志][" + DateTime.Now.ToString() + "]\r\n";
LogContent += "=>[Position]=>[" + ErrorPosition + "]\r\n";
LogContent += "=>[UserInfo]=>[" + ErrorInfo_User + "]\r\n";
LogContent += "=>[SysInfo]=>[" + ErrorInfo_Sys + "]\r\n";
LogContent += "--------------------------------------------------------------------------\r\n";
return LogContent;
}
// 保存日志信息到文件
public void SaveLogToFile()
{
try
{
// get the file path of the log.
string FileName = @"log/LogInfo_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
// get the content of the log.
string LogContent = GetLogContent();
// create the stream of the log file and save the log.
FileStream smLog = new FileStream(FileName, FileMode.Append, FileAccess.Write);
// if the stream is correct.
if (smLog != null)
{
long lFileContentLen = smLog.Length;
smLog.Lock(0, lFileContentLen);
byte[] buffer = Encoding.GetEncoding("gb2312").GetBytes(LogContent);
smLog.Write(buffer, 0, buffer.Length);
smLog.Unlock(0, lFileContentLen);
smLog.Flush();
smLog.Close();
}
}
catch
{ }
}
}
public class LogExample
{
private LogInfo _Log = new LogInfo();
// 某處理函數(shù)一
public void Function_First()
{
try
{
// do something which could be occur exception.
}
catch (Exception error)
{
_Log.RecordErrorInfo("函數(shù)Function_First", error.Message.ToString(), "執(zhí)行函數(shù)Function_First時(shí),發(fā)生異常!");
_Log.SaveLogToFile();
}
}
// 某處理函數(shù)二
public void Function_Second()
{
try
{
// do something which could be occur exception.
}
catch (Exception error)
{
_Log.RecordErrorInfo("函數(shù)Function_Second", error.Message.ToString(), "執(zhí)行函數(shù)Function_Second時(shí),發(fā)生異常!");
_Log.SaveLogToFile();
}
}
}
class Program
{
static void Main(string[] args)
{
// 創(chuàng)建內(nèi)建了日志的對(duì)象
LogExample Le = new LogExample();
// 執(zhí)行處理操作一
Le.Function_First();
// 執(zhí)行處理操作二
Le.Function_Second();
}
}
相關(guān)文章
Community Server專題三:HttpModule
Community Server專題三:HttpModule...2007-03-03
.NET?Core配置TLS?Cipher(套件)的詳細(xì)過程
本文以.NET?5為例,只不過針對(duì).NET?Core?3或3.1通過工具掃描出的協(xié)議套件結(jié)果略有所差異,但不影響我們對(duì)安全套件的配置,我們使用OpenSSL生成自簽名證書,對(duì).NET?Core配置TLS?Cipher相關(guān)知識(shí)感興趣的朋友一起看看吧2021-12-12
.NET Core使用HttpClient進(jìn)行表單提交時(shí)遇到的問題
這篇文章主要介紹了.NET Core使用HttpClient進(jìn)行表單提交時(shí)遇到的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
理解ASP.NET Core 中間件(Middleware)
這篇文章主要介紹了ASP.NET Core 中間件(Middleware),中間件是一種裝配到應(yīng)用管道以處理請(qǐng)求和響應(yīng)的軟件。文中講解相關(guān)知識(shí)非常詳細(xì),感興趣的朋友可以一起來看一看2021-09-09
CentOS上運(yùn)行ZKEACMS的詳細(xì)過程
這篇文章主要為大家介紹了CentOS上運(yùn)行ZKEACMS的詳細(xì)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
asp.net mvc中Forms身份驗(yàn)證身份驗(yàn)證流程
本篇文章主要介紹了asp.net MVC中Forms身份驗(yàn)證身份驗(yàn)證流程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

