C 實現每天建立日誌檔案

2021-09-23 18:42:00 字數 1888 閱讀 2725

一、編寫日誌檔案類

/*

* 主題:日誌檔案

* * 功能:

* 1、每天建立日誌檔案

* 2、可以向日誌檔案追加內容,不被覆蓋

* 3、可以讀取日誌檔案的內容

* 4、可以清空日誌檔案內容

* 5、刪除指定的日誌檔案

*

* */

using system;

using system.io;

using system.text;

namespace kernal}}

else}}

return success;

}/// /// 讀取日誌檔案的資訊

///

/// 檔案路徑

/// 返回讀取的資訊

public static string read(string path)}}

return contents;

}/// /// 清除日誌檔案的資訊

///

/// 檔案的路徑

/// true:表示清空成功

public static bool clear(string path)

}return success;

}/// /// 刪除日誌檔案

///

/// 檔案的路徑

/// true:表示刪除成功

public static bool delete(string path)

return success;

}#endregion

#region 私有方法

/// /// 日誌檔案存放路徑

///

private static string logpath

return outpath + datetime.today.tostring("yyyy-mm-dd") + ".txt";}}

#endregion

}//class_end

}

二、測試

using kernal;

using system;

namespace xmlhelper

}string path = @"c:\users\coffeemilk\desktop\xmlhelper\xmlhelper\bin\debug\everydaylog\2019-05-27.txt";

//讀取檔案內容

string a = everydaylog.read(path);

console.writeline(a);

//清空檔案內容

bool success = everydaylog.clear(path);

console.writeline("清空情況=" + success);

刪除檔案

//bool success1 = everydaylog.delete(path);

"刪除情況=" + success1);

console.read();

}//class_end}}

三、效果

c#清空txt內容,並追加內容

c 實現的日誌檔案

include include include ifdef win32 include include else include include include define critical section pthread mutex t define vsnprintf vsnprintf en...

c 實現log日誌檔案

1 在專案的執行過程中需要記錄或現實 的執行流程和錯誤資訊,但在不同的階段需要的功能不同 除錯階段 需要在螢幕顯示執行過程和錯誤資訊 運營階段 需要在檔案中記錄下來執行過程和錯誤資訊 2 根據錯誤原因一般段錯誤分為收下四個等級 致命錯誤 log fatal 警告 log warning 錯誤 log...

異常處理 建立日誌檔案

procedure clogfile txt string var tf text sfile string begin sfile recod.log trytry assignfile tf,sfile if not fileexists sfile then rewrite tf else w...