C#按照日期輸出程式日誌
namespace LogData
{
public class Log
{
private static LogManager logManager;
static Log()
{
logManager = new LogManager();
}
public static void WriteLog(LogFile logFile, string msg)
{
try
{
logManager.WriteLog(logFile, msg);
}
catch
{
}
}
public static void WriteLog(string msg)
{
try
{
logManager.WriteLog(LogFile.Info, msg);
}
catch
{
}
}
public static void WriteLog(string logFile, string msg)
{
try
{
logManager.WriteLog(logFile, msg);
}
catch
{
}
}
}
public class LogManager
{
private string logFileName = string.Empty;
private string logPath = "Log";
private string logFileExtName = "log";
private bool writeLogTime = true;
private bool logFileNameEndWithDate = true;
private Encoding logFileEncoding = Encoding.UTF8;
private object obj = new object();
#region 建構函式
public LogManager()
{
this.LogPath = "Log";
this.LogFileExtName = "log";
this.WriteLogTime = true;
this.logFileNameEndWithDate = true;
this.logFileEncoding = Encoding.UTF8;
}
public LogManager(string logPath, string logFileExtName, bool writeLogTime)
{
this.LogPath = logPath;
this.LogFileExtName = logFileExtName;
this.WriteLogTime = writeLogTime;
this.logFileNameEndWithDate = true;
this.logFileEncoding = Encoding.UTF8;
}
#endregion
#region 屬性
/// <summary>
/// Log 檔案路徑
/// </summary>
public string LogPath
{
get
{
if (this.logPath == null || this.logPath == string.Empty)
{
//Application.StartupPath
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
return this.logPath;
}
set
{
this.logPath = value;
if (this.logPath == null || this.logPath == string.Empty)
{
//Application.StartupPath
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
else
{
try
{
// 判斷是否不是絕對路徑(絕對路徑裡還有":")
if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
{ /* 絕對路徑 */}
else
{
// 相對路徑
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath, DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!Directory.Exists(this.logPath))
Directory.CreateDirectory(this.logPath);
}
catch
{
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!this.logPath.EndsWith(@"\"))
this.logPath += @"\";
}
}
}
/// <summary>
/// Log 副檔名
/// </summary>
public string LogFileExtName
{
get { return this.logFileExtName; }
set { this.logFileExtName = value; }
}
/// <summary>
/// 是否在每個Log行前面新增當前時間
/// </summary>
public bool WriteLogTime
{
get { return this.writeLogTime; }
set { this.writeLogTime = value; }
}
/// <summary>
/// 日誌檔名是否帶日期
/// </summary>
public bool LogFileNameEndWithDate
{
get { return logFileNameEndWithDate; }
set { logFileNameEndWithDate = value; }
}
/// <summary>
/// 日誌檔案的字元編碼
/// </summary>
public Encoding LogFileEncoding
{
get { return logFileEncoding; }
set { logFileEncoding = value; }
}
#endregion
#region 公有方法
public void WriteLog(string logFile, string msg)
{
lock (obj)
{
try
{
string dateString = string.Empty;
if (this.logFileNameEndWithDate || logFile.Length == 0)
{
dateString = DateTime.Now.ToString("yyyyMMdd");
}
logFileName = string.Format("{0}{1}{2}.{3}",
this.LogPath,
logFile,
dateString,
this.logFileExtName);
using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
{
if (writeLogTime)
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
}
else
{
sw.WriteLine(msg);
}
}
}
catch
{
}
}
}
public void WriteLog(LogFile logFile, string msg)
{
this.WriteLog(logFile.ToString(), msg);
}
public void WriteLog(string msg)
{
this.WriteLog(string.Empty, msg);
}
#endregion
}
public enum LogFile
{
Trace,
Error,
SQL,
SQLError,
Login,
Info,
WeChat
}
}
使用時直接呼叫公用方法WriteLog,裡面設定了錯誤型別.
每天的日誌檔案輸出成一個資料夾,方便查閱
相關文章
- reportbuilder 日誌輸出UI
- Python輸出日誌Python
- python如何輸出日誌?Python
- Java日誌輸出問題Java
- awk多行日誌排序輸出排序
- python日誌重複輸出Python
- nginx日誌按照天進行分割Nginx
- 如何使用 loguru 接管程式的所有日誌輸出?
- Java 如何正確地輸出日誌Java
- 指令碼日誌的標準輸出指令碼
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- 按日期分割nginx日誌Nginx
- 失敗登入行為輸出至alert日誌
- 【RMAN】rman的日誌輸出功能演示
- grep搜尋伺服器日誌(搜尋指定關鍵字的行,按照日期group by count)伺服器
- 使用 C# 捕獲程式輸出C#
- log4j日誌輸出級別(轉)
- logrus hook輸出日誌到本地磁碟Hook
- log4j2分層輸出日誌
- Robotframework-ride 日誌輸出中文亂碼FrameworkIDE
- (定時)任務輸出重定向到日誌
- 另一種列印輸出日誌資訊的方式
- spark指令碼日誌輸出級別設定Spark指令碼
- Hibernate 使用log4j輸出日誌
- 使用Log4j2輸出日誌演示
- MapReduce 按照Value值進行排序輸出排序
- hexdump如何按照單位元組格式輸出
- Laravel 自定義日誌驅動 -支援按類+日期分割日誌Laravel
- nginx自帶的日誌日期分割Nginx
- logback下日誌輸出前處理操作——以日誌脫敏為例
- Web實時日誌輸出檢視管理系統Web
- python怎麼將列印輸出日誌檔案Python
- linux自動清理Docker標準輸出日誌LinuxDocker
- Spring AOP實現統一日誌輸出Spring
- 實時檢視模擬器的日誌輸出
- MySQL 使用tee記錄語句和輸出日誌MySql
- Laravel 中輸出 SQL 語句的到 log 日誌LaravelSQL
- win8 學習筆記二 輸出日誌筆記