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,裡面設定了錯誤型別.
每天的日誌檔案輸出成一個資料夾,方便查閱
相關文章
- python如何輸出日誌?Python
- python日誌重複輸出Python
- 如何使用 loguru 接管程式的所有日誌輸出?
- 按日期分割nginx日誌Nginx
- Java 如何正確地輸出日誌Java
- 指令碼日誌的標準輸出指令碼
- 操作日誌記錄(包括輸出至自定義日誌檔案)
- grep搜尋伺服器日誌(搜尋指定關鍵字的行,按照日期group by count)伺服器
- 使用 C# 捕獲程式輸出C#
- logrus hook輸出日誌到本地磁碟Hook
- Robotframework-ride 日誌輸出中文亂碼FrameworkIDE
- logback下日誌輸出前處理操作——以日誌脫敏為例
- Laravel 自定義日誌驅動 -支援按類+日期分割日誌Laravel
- Spring AOP實現統一日誌輸出Spring
- 使用Log4j2輸出日誌演示
- log4j2分層輸出日誌
- Linux nohup 啟動服務且不輸出日誌Linux
- log4j日誌輸出級別(轉)
- python怎麼將列印輸出日誌檔案Python
- linux自動清理Docker標準輸出日誌LinuxDocker
- (定時)任務輸出重定向到日誌
- 失敗登入行為輸出至alert日誌
- 如何利用NLog輸出結構化日誌,並在Kibana優雅分析日誌?
- nginx自帶的日誌日期分割Nginx
- 在C程式中按照你想要的格式輸出時間:strftime()C程式
- hexdump如何按照單位元組格式輸出
- 啟動tomcat時,日誌裡大量輸出建立資料來源dataSource的日誌Tomcat
- Web實時日誌輸出檢視管理系統Web
- 另一種列印輸出日誌資訊的方式
- hyperf 如何實現按日期分割日誌
- logback 日誌輸出格式
- SpringBoot通過yml和xml檔案配置日誌輸出Spring BootXML
- C++簡單日誌/debug除錯資訊輸出C++除錯
- .NET Core整合CorrelationId實現全鏈路日誌輸出
- 簡單的C#日誌類C#
- [Java/日誌] 日誌框架列印應用程式日誌程式碼的執行情況Java框架
- nohup不輸出nohup.out日誌資訊,已解決。
- JVM 輸出 GC 日誌導致 JVM 卡住,我 TM 人傻了JVMGC