@@ -15,7 +15,10 @@ static class LogText
15
15
/// 請設定天數為負數
16
16
/// </summary>
17
17
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 ;
19
22
20
23
/// <summary>
21
24
/// 建立或是新增LOG紀錄
@@ -32,30 +35,12 @@ internal static void Add_LogText(string Type, string Message, object[] arg)
32
35
{
33
36
string LogPath = $ "{ AppDomain . CurrentDomain . BaseDirectory } \\ logs\\ LogFiles\\ ";
34
37
35
- //判斷有無資料表 若沒有建立資料表
36
- if ( ! Directory . Exists ( LogPath ) )
37
- {
38
- Directory . CreateDirectory ( LogPath ) ;
39
- }
38
+ CheckDirectoryExistCreate ( LogPath ) ;
40
39
41
- var s_File_Path = $ " { LogPath } { DateTime . Now . ToString ( "yyyyMMdd" ) } _ { Type } _LOG.txt" ;
40
+ var LogFilePath = CheckFileExistCreate ( LogPath , Type ) ;
42
41
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 ) ;
51
43
52
-
53
- using ( StreamWriter sw = new StreamWriter ( s_File_Path , true , Encoding . UTF8 ) )
54
- {
55
- sw . WriteLine ( Message , arg ) ;
56
- sw . Close ( ) ;
57
- }
58
-
59
44
Remove_TimeOutLogText ( ) ;
60
45
}
61
46
}
@@ -66,23 +51,82 @@ internal static void Add_LogText(string Type, string Message, object[] arg)
66
51
}
67
52
68
53
/// <summary>
69
- /// 刪除過久紀錄檔
54
+ /// 判斷有無資料表 若沒有建立資料表
70
55
/// </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 )
73
58
{
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
+ }
75
114
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 ) )
79
118
{
80
- File . Delete ( s_File_Path ) ;
119
+ sw . WriteLine ( _Message , arg ) ;
120
+ sw . Close ( ) ;
81
121
}
82
122
}
83
123
84
124
85
- internal static void Remove_TimeOutLogText ( )
125
+
126
+ /// <summary>
127
+ /// 刪除超時紀錄檔
128
+ /// </summary>
129
+ private static void Remove_TimeOutLogText ( )
86
130
{
87
131
string LogPath = $ "{ AppDomain . CurrentDomain . BaseDirectory } \\ logs\\ LogFiles\\ ";
88
132
DirectoryInfo di = new DirectoryInfo ( LogPath ) ;
0 commit comments