寫入日誌到txt檔案

2022-04-29 13:09:06 字數 731 閱讀 6920

//寫入日誌(簡潔版)

//帶自動刪除版(大小 )

//寫入日誌

using (streamwriter writer = new streamwriter(path, true))

long size = 0;

//獲取檔案大小

using (filestream file = system.io.file.openread(path))

//判斷日誌檔案大於2m,自動刪除。

if (size > (1024 * 4 * 512))

}catch

}//帶自動刪除版(時間 )

//寫入日誌

using (streamwriter writer = new streamwriter(path, true))

datetime createtime = datetime.minvalue;

//獲取建立時間

fileinfo file = new fileinfo(path);

createtime= file.creationtime;

//判斷日誌檔案建立日期是否超過乙個月,自動刪除。

if (createtime > datetime.now.addmonths(-1))

}catch

}system.diagnostics.process.getcurrentprocess().mainmodule.filename // 獲取模組的完整路徑。

日誌寫入到檔案 多檔案寫入

在上篇文章的基礎上,修改配置 log4j.rootlogger info,fout 說明 rootlogger是可以多樣式定義的,如log4j.rootlogger info,myout,fout。myout,fout是自定義樣式。這樣就會在c盤下建立a.log檔案。有這樣一種需求,不同模組需要各自...

C 寫入資料到txt檔案

string readme readme.txt 檔名稱 string txtpath path readme 檔案存放路徑 directoryinfo directoryinfo new directoryinfo path if directoryinfo.exists 判斷 資料夾是否存在 i...

txt檔案寫入

txt檔案的讀取 public static void main string args system.out.println 一共行數 i 寫入txt檔案 方法一 file file new file g aaa bbb ceshi.txt 相對路徑,如果沒有則要建立乙個新的output。txt檔...