1- using Newtonsoft . Json ;
1+ using Blog . Core . Common . Helper ;
2+ using Newtonsoft . Json ;
23using System ;
34using System . Collections . Generic ;
45using System . IO ;
@@ -21,7 +22,7 @@ public LogLock(string contentPath)
2122 _contentRoot = contentPath ;
2223 }
2324
24- public static void OutSql2Log ( string filename , string [ ] dataParas , bool IsHeader = true )
25+ public static void OutSql2Log ( string prefix , string [ ] dataParas , bool IsHeader = true )
2526 {
2627 try
2728 {
@@ -31,12 +32,13 @@ public static void OutSql2Log(string filename, string[] dataParas, bool IsHeader
3132 // 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常
3233 LogWriteLock . EnterWriteLock ( ) ;
3334
34- var path = Path . Combine ( _contentRoot , "Log" ) ;
35- if ( ! Directory . Exists ( path ) )
35+ var folderPath = Path . Combine ( _contentRoot , "Log" ) ;
36+ if ( ! Directory . Exists ( folderPath ) )
3637 {
37- Directory . CreateDirectory ( path ) ;
38+ Directory . CreateDirectory ( folderPath ) ;
3839 }
39- string logFilePath = Path . Combine ( path , $@ "{ filename } .log") ;
40+ //string logFilePath = Path.Combine(path, $@"{filename}.log");
41+ var logFilePath = FileHelper . GetAvailableFileWithPrefixOrderSize ( folderPath , prefix ) ;
4042
4143 var now = DateTime . Now ;
4244 string logContent = String . Join ( "\r \n " , dataParas ) ;
@@ -72,23 +74,54 @@ public static void OutSql2Log(string filename, string[] dataParas, bool IsHeader
7274 }
7375 }
7476
75- public static string ReadLog ( string Path , Encoding encode )
77+ /// <summary>
78+ /// 读取文件内容
79+ /// </summary>
80+ /// <param name="folderPath">文件夹路径</param>
81+ /// <param name="fileName">文件名</param>
82+ /// <param name="encode">编码</param>
83+ /// <param name="readType">读取类型(0:精准,1:前缀模糊)</param>
84+ /// <returns></returns>
85+ public static string ReadLog ( string folderPath , string fileName , Encoding encode , ReadType readType = ReadType . Accurate )
7686 {
7787 string s = "" ;
7888 try
7989 {
8090 LogWriteLock . EnterReadLock ( ) ;
8191
82- if ( ! System . IO . File . Exists ( Path ) )
92+ // 根据文件名读取当前文件内容
93+ if ( readType == ReadType . Accurate )
8394 {
84- s = null ;
95+ var filePath = Path . Combine ( folderPath , fileName ) ;
96+ if ( ! File . Exists ( filePath ) )
97+ {
98+ s = null ;
99+ }
100+ else
101+ {
102+ StreamReader f2 = new StreamReader ( filePath , encode ) ;
103+ s = f2 . ReadToEnd ( ) ;
104+ f2 . Close ( ) ;
105+ f2 . Dispose ( ) ;
106+ }
85107 }
86- else
108+
109+ // 根据前缀读取所有文件内容
110+ if ( readType == ReadType . Prefix )
87111 {
88- StreamReader f2 = new StreamReader ( Path , encode ) ;
89- s = f2 . ReadToEnd ( ) ;
90- f2 . Close ( ) ;
91- f2 . Dispose ( ) ;
112+ var allFiles = new DirectoryInfo ( folderPath ) ;
113+ var selectFiles = allFiles . GetFiles ( ) . Where ( fi => fi . Name . ToLower ( ) . Contains ( fileName . ToLower ( ) ) ) . ToList ( ) ;
114+
115+ foreach ( var item in selectFiles )
116+ {
117+ if ( File . Exists ( item . FullName ) )
118+ {
119+ StreamReader f2 = new StreamReader ( item . FullName , encode ) ;
120+ s += f2 . ReadToEnd ( ) ;
121+ f2 . Close ( ) ;
122+ f2 . Dispose ( ) ;
123+ }
124+ }
92125 }
93126 }
94127 catch ( Exception )
@@ -112,7 +145,7 @@ public static List<LogInfo> GetLogData()
112145
113146 try
114147 {
115- var aoplogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" , "AOPLog.log" ) , Encoding . UTF8 ) ;
148+ var aoplogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "AOPLog_" , Encoding . UTF8 , ReadType . Prefix ) ;
116149
117150 if ( ! string . IsNullOrEmpty ( aoplogContent ) )
118151 {
@@ -130,7 +163,7 @@ public static List<LogInfo> GetLogData()
130163
131164 try
132165 {
133- var exclogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" , $ "GlobalExceptionLogs_{ DateTime . Now . ToString ( "yyyMMdd" ) } .log") , Encoding . UTF8 ) ;
166+ var exclogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" ) , $ "GlobalExceptionLogs_{ DateTime . Now . ToString ( "yyyMMdd" ) } .log", Encoding . UTF8 ) ;
134167
135168 if ( ! string . IsNullOrEmpty ( exclogContent ) )
136169 {
@@ -150,7 +183,7 @@ public static List<LogInfo> GetLogData()
150183
151184 try
152185 {
153- var sqllogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" , "SqlLog.log" ) , Encoding . UTF8 ) ;
186+ var sqllogContent = ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "SqlLog_" , Encoding . UTF8 , ReadType . Prefix ) ;
154187
155188 if ( ! string . IsNullOrEmpty ( sqllogContent ) )
156189 {
@@ -184,7 +217,7 @@ public static List<LogInfo> GetLogData()
184217
185218 try
186219 {
187- var Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" , "RequestIpInfoLog.log" ) , Encoding . UTF8 ) + "]" ) ;
220+ var Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "RequestIpInfoLog_" , Encoding . UTF8 , ReadType . Prefix ) + "]" ) ;
188221
189222 Logs = Logs . Where ( d => d . Datetime . ObjToDate ( ) >= DateTime . Today ) . ToList ( ) ;
190223
@@ -228,7 +261,7 @@ public static RequestApiWeekView RequestApiinfoByWeek()
228261
229262 try
230263 {
231- Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" , "RequestIpInfoLog.log" ) , Encoding . UTF8 ) + "]" ) ;
264+ Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "RequestIpInfoLog_" , Encoding . UTF8 , ReadType . Prefix ) + "]" ) ;
232265
233266 var ddd = Logs . Where ( d => d . Week == "周日" ) . ToList ( ) ;
234267
@@ -293,7 +326,7 @@ public static AccessApiDateView AccessApiByDate()
293326 List < ApiDate > apiDates = new List < ApiDate > ( ) ;
294327 try
295328 {
296- Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" , "RequestIpInfoLog.log" ) , Encoding . UTF8 ) + "]" ) ;
329+ Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "RequestIpInfoLog_" , Encoding . UTF8 , ReadType . Prefix ) + "]" ) ;
297330
298331 apiDates = ( from n in Logs
299332 group n by new { n . Date } into g
@@ -323,7 +356,7 @@ public static AccessApiDateView AccessApiByHour()
323356 List < ApiDate > apiDates = new List < ApiDate > ( ) ;
324357 try
325358 {
326- Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" , "RequestIpInfoLog.log" ) , Encoding . UTF8 ) + "]" ) ;
359+ Logs = JsonConvert . DeserializeObject < List < RequestInfo > > ( "[" + ReadLog ( Path . Combine ( _contentRoot , "Log" ) , "RequestIpInfoLog_" , Encoding . UTF8 , ReadType . Prefix ) + "]" ) ;
327360
328361 apiDates = ( from n in Logs
329362 where n . Datetime . ObjToDate ( ) >= DateTime . Today
@@ -349,5 +382,10 @@ where n.Datetime.ObjToDate() >= DateTime.Today
349382 }
350383 }
351384
385+ public enum ReadType
386+ {
387+ Accurate ,
388+ Prefix
389+ }
352390
353391}
0 commit comments