記錄日誌檔案
class CommonFuc
{
private static readonly string strLogPath = System.Configuration.ConfigurationSettings.AppSettings["logPath"];//日誌路徑
///
/// 用於記錄日誌
///
/// 日誌path
/// 日誌內容
public static void WriteLog(string logType, string logInfo)
{
string filename = string.Empty;
switch (logType)
{
case "MailLog":
filename = strLogPath + @"\MailLogs\" + DateTime.Now.ToString("yyyyMMdd") + "-Notes.log"; ;
break;
case "RuningLog":
filename = strLogPath + @"\ServiceLogs\" + DateTime.Now.ToString("yyyyMMdd") + "--B2CService.log";
break;
}
FileInfo f = new FileInfo(filename);
FileStream fs = null;
StreamWriter sw = null;
Monitor.Enter(typeof(CommonFuc));
try
{
if (f.Exists)
{
fs = new FileStream(filename,FileMode.Append, FileAccess.Write, FileShare.Write);
}
else
{
fs = new FileStream(filename,FileMode.Create, FileAccess.Write, FileShare.Write);
}
sw = new StreamWriter(fs);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss :") + logInfo);
sw.Flush();
}
catch (IOException e)
{
}
finally
{
sw.Close();
fs.Close();
Monitor.Exit(typeof(CommonFuc));
}
}
///
/// DELETE LOG
///
///
public static void DeleHistoryLog(double days)
{
string del = "ServiceLogs,MailContent1,MailLogs";
foreach (string item in del.Split(','))
{
DirectoryInfo dir = new DirectoryInfo(strLogPath + @"\" + item);
FileInfo[] files = dir.GetFiles();
foreach (FileInfo f in files)
{
if (DateTime.Now.AddDays(days).CompareTo(f.CreationTime) > 0)
{
f.Delete();
}
}
}
}
}
//在config中新增路徑
<!--日誌路徑-->
//呼叫
catch (Exception ex)
{
CommonFuc.WriteLog("RuningLog", "異常======>>" + ex.Message + " SP======>>" + "pk_automial.p_show_mail_info");
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-772685/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Laravel 指定日誌檔案記錄任意日誌Laravel
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- 日誌模組(一標頭檔案就實現了日誌記錄)
- 使apache的日誌檔案裡不記錄圖片檔案Apache
- nginx日誌檔案按天記錄定時清理迴圈記錄Nginx
- 記錄一則clear重做日誌檔案的案例
- 使用配置檔案方式記錄Python程式日誌Python
- php日誌,記錄日誌PHP
- PHP設定時區,記錄日誌檔案的方法PHP
- Nginx訪問日誌、Nginx日誌切割、靜態檔案不記錄日誌和過期時間Nginx
- SLF4J記錄日誌&&日誌檔案的滾動策略__SpringBootSpring Boot
- springboot使用logback記錄日誌,配置檔案Spring Boot
- django 專案日誌記錄設定Django
- 日誌記錄器
- 【Oracle日誌】- 日誌檔案重建Oracle
- 日誌檔案
- 如何在專案中記錄日誌資訊?
- 日誌檔案和歸檔日誌檔案的關係以及如何切換日誌
- Laravel sql 日誌記錄LaravelSQL
- secureCRT記錄操作日誌Securecrt
- PHP日誌記錄方法PHP
- oracle日誌操作記錄Oracle
- 監聽器日誌檔案(listener.log)到底記錄了些啥
- 刪除日誌檔案組與日誌檔案成員
- 管理日誌檔案
- dump日誌檔案
- APACHE日誌檔案Apache
- redo日誌檔案學習筆記(一)筆記
- 日誌記錄 PHP下往linux目錄下寫日誌PHPLinux
- larave 日誌自定義配置格式記錄呼叫檔案路徑與行號
- 記:”$ORACLE_HOME/.../sysman/recv/errors“目錄下產生大量日誌檔案OracleError
- Oracle補充日誌及日誌記錄規則Oracle
- 控制檔案/歸檔日誌
- db2不記錄日誌插入記錄DB2
- 增加日誌檔案,修改當前日誌檔案大小
- 記錄騰訊雲使用日誌
- 記錄 | 實習日誌 9
- mysql開啟日誌記錄MySql