Skip to content

Commit 63948ca

Browse files
author
ozakboy
committed
新增 超過最大檔案大小自動切割LOG紀錄檔
1 parent 2d0da2a commit 63948ca

File tree

4 files changed

+95
-32
lines changed

4 files changed

+95
-32
lines changed

ozakboy.NLOG/ozakboy.NLOG.Test/Program.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
LOG.Error_Log("LOG_TEST");
88
LOG.Warn_Log("LOG_TEST");
99
LOG.Fatal_Log("LOG_TEST");
10-
LOG.CostomName_Log("ABC","LOG_TEST");
10+
LOG.CostomName_Log("ABC", "LOG_TEST");
11+
12+
for (int i = 0; i < 1000000; i++)
13+
{
14+
LOG.CostomName_Log("BigFile_1", "LOG_TEST");
15+
}
1116

1217
try
1318
{

ozakboy.NLOG/ozakboy.NLOG/LogText.cs

+75-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ static class LogText
1515
/// 請設定天數為負數
1616
/// </summary>
1717
public static int LogKeepDay = -3;
18-
public static int BigFilesByte = 1024;
18+
/// <summary>
19+
/// 預設最大檔案 50MB 超過自動分割檔案
20+
/// </summary>
21+
public static long BigFilesByte =50 * 1024 * 1024 ;
1922

2023
/// <summary>
2124
/// 建立或是新增LOG紀錄
@@ -32,30 +35,12 @@ internal static void Add_LogText(string Type, string Message, object[] arg)
3235
{
3336
string LogPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\logs\\LogFiles\\";
3437

35-
//判斷有無資料表 若沒有建立資料表
36-
if (!Directory.Exists(LogPath))
37-
{
38-
Directory.CreateDirectory(LogPath);
39-
}
38+
CheckDirectoryExistCreate(LogPath);
4039

41-
var s_File_Path = $"{LogPath}{DateTime.Now.ToString("yyyyMMdd")}_{Type}_LOG.txt";
40+
var LogFilePath = CheckFileExistCreate(LogPath, Type);
4241

43-
//判斷有無檔案,若沒有則建立檔案
44-
if (!File.Exists(s_File_Path))
45-
{
46-
using (FileStream fileStream = new FileStream(s_File_Path, FileMode.Create))
47-
{
48-
fileStream.Close();
49-
}
50-
}
42+
FIleWriteLine(arg, LogFilePath, Message);
5143

52-
53-
using (StreamWriter sw = new StreamWriter(s_File_Path, true, Encoding.UTF8))
54-
{
55-
sw.WriteLine(Message, arg);
56-
sw.Close();
57-
}
58-
5944
Remove_TimeOutLogText();
6045
}
6146
}
@@ -66,23 +51,82 @@ internal static void Add_LogText(string Type, string Message, object[] arg)
6651
}
6752

6853
/// <summary>
69-
/// 刪除過久紀錄檔
54+
/// 判斷有無資料表 若沒有建立資料表
7055
/// </summary>
71-
/// <param name="Type"></param>
72-
internal static void Remove_LogText(string Type)
56+
/// <param name="LogPath"></param>
57+
private static void CheckDirectoryExistCreate(string LogPath)
7358
{
74-
string LogPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\logs\\LogFiles\\";
59+
if (!Directory.Exists(LogPath))
60+
{
61+
Directory.CreateDirectory(LogPath);
62+
}
63+
}
64+
65+
/// <summary>
66+
/// 判斷有無檔案或檔案過大,若沒有或檔案過大則建立新檔案
67+
/// </summary>
68+
/// <param name="_LogPath">檔案路徑</param>
69+
/// <param name="_FileName">檔案名稱</param>
70+
private static string CheckFileExistCreate(string _LogPath , string _FileName)
71+
{
72+
var LogFIleName = $"{DateTime.Now.ToString("yyyyMMdd")}_{_FileName}_Log.txt";
73+
var SearchFIleName = $"{DateTime.Now.ToString("yyyyMMdd")}_{_FileName}*";
74+
FileExistCreate(_LogPath + LogFIleName);
75+
76+
DirectoryInfo di = new DirectoryInfo(_LogPath);
77+
var Files = di.GetFiles(SearchFIleName).OrderBy(x=>x.LastWriteTimeUtc).ToArray();
78+
var NowWriteFile = Files[Files.Length -1];
79+
if(NowWriteFile.Length > BigFilesByte)
80+
{
81+
var FileNameSplits = NowWriteFile.Name.Replace("_"+_FileName,"").Split("_");
82+
if (!FileNameSplits[1].Contains("part"))
83+
{
84+
LogFIleName = $"{DateTime.Now.ToString("yyyyMMdd")}_{_FileName}_part{1}_Log.txt";
85+
}
86+
else
87+
{
88+
var Part = Convert.ToInt32(FileNameSplits[1].Replace("part",""));
89+
LogFIleName = $"{DateTime.Now.ToString("yyyyMMdd")}_{_FileName}_part{Part + 1}_Log.txt";
90+
}
91+
FileExistCreate(_LogPath + LogFIleName);
92+
}
93+
else
94+
{
95+
LogFIleName = NowWriteFile.Name;
96+
}
97+
return _LogPath + LogFIleName;
98+
}
99+
100+
/// <summary>
101+
/// 判斷有無檔案,若沒有則建立新檔案
102+
/// </summary>
103+
/// <param name="_LogFilePath"></param>
104+
private static void FileExistCreate(string _LogFilePath)
105+
{
106+
if (!File.Exists(_LogFilePath))
107+
{
108+
using (FileStream fileStream = new FileStream(_LogFilePath, FileMode.Create))
109+
{
110+
fileStream.Close();
111+
}
112+
}
113+
}
75114

76-
var s_File_Path = $"{LogPath}{DateTime.Now.AddDays(LogKeepDay).ToString("yyyyMMdd")}_{Type}_LOG.txt";
77-
//判斷有無檔案,若有則刪除檔案
78-
if (File.Exists(s_File_Path))
115+
private static void FIleWriteLine(object[] arg , string _filePath ,string _Message)
116+
{
117+
using (StreamWriter sw = new StreamWriter(_filePath, true, Encoding.UTF8))
79118
{
80-
File.Delete(s_File_Path);
119+
sw.WriteLine(_Message, arg);
120+
sw.Close();
81121
}
82122
}
83123

84124

85-
internal static void Remove_TimeOutLogText()
125+
126+
/// <summary>
127+
/// 刪除超時紀錄檔
128+
/// </summary>
129+
private static void Remove_TimeOutLogText()
86130
{
87131
string LogPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\logs\\LogFiles\\";
88132
DirectoryInfo di = new DirectoryInfo(LogPath);

ozakboy.NLOG/ozakboy.NLOG/NLOG.cs

+14
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,19 @@ public static void SetLogKeepDay(int KeepDay)
275275

276276
#endregion
277277

278+
#region 設定預設最大檔案限制
279+
280+
/// <summary>
281+
/// 設定預設最大檔案限制
282+
/// 預設最大檔限制 50MB
283+
/// </summary>
284+
/// <param name="_BigFilesByte">最大檔案位元組限制</param>
285+
public static void SetBigFilesByte(long _BigFilesByte)
286+
{
287+
LogText.BigFilesByte = _BigFilesByte;
288+
}
289+
290+
#endregion
291+
278292
}
279293
}

0 commit comments

Comments
 (0)