Skip to content

Commit dc41767

Browse files
committed
Fixed anjoy8#96 Bug
修复 anjoy8#96 文件
1 parent 26d1fd6 commit dc41767

4 files changed

Lines changed: 104 additions & 30 deletions

File tree

Blog.Core.Api/Controllers/MonitorController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public MessageModel<AccessApiDateView> GetAccessApiByHour()
115115
[HttpGet]
116116
public MessageModel<List<UserAccessModel>> GetAccessLogs([FromServices]IWebHostEnvironment environment)
117117
{
118-
var Logs = JsonConvert.DeserializeObject<List<UserAccessModel>>("[" + LogLock.ReadLog(Path.Combine(environment.ContentRootPath, "Log", "RecordAccessLogs.log"), Encoding.UTF8) + "]");
118+
var Logs = JsonConvert.DeserializeObject<List<UserAccessModel>>("[" + LogLock.ReadLog(Path.Combine(environment.ContentRootPath, "Log"), "RecordAccessLogs_", Encoding.UTF8, ReadType.Prefix) + "]");
119119

120120
Logs = Logs.Where(d => d.BeginTime.ObjToDate() >= DateTime.Today).OrderByDescending(d => d.BeginTime).Take(50).ToList();
121121
return new MessageModel<List<UserAccessModel>>()

Blog.Core.Common/Helper/FileHelper.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45

56
namespace Blog.Core.Common.Helper
@@ -61,6 +62,29 @@ public static string GetPostfixStr(string filename)
6162
}
6263
#endregion
6364

65+
#region 根据文件大小获取指定前缀的可用文件名
66+
/// <summary>
67+
/// 根据文件大小获取指定前缀的可用文件名
68+
/// </summary>
69+
/// <param name="folderPath">文件夹</param>
70+
/// <param name="prefix">文件前缀</param>
71+
/// <param name="size">文件大小(1m)</param>
72+
/// <param name="ext">文件后缀(.log)</param>
73+
/// <returns>可用文件名</returns>
74+
public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
75+
{
76+
var allFiles = new DirectoryInfo(folderPath);
77+
var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).ToList();
78+
79+
if (selectFiles.Count > 0)
80+
{
81+
return selectFiles.FirstOrDefault().FullName;
82+
}
83+
84+
return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.DateToTimeStamp()}.log");
85+
}
86+
#endregion
87+
6488
#region 写文件
6589
/****************************************
6690
* 函数名称:WriteFile
@@ -78,12 +102,12 @@ public static string GetPostfixStr(string filename)
78102
/// <param name="Strings">文件内容</param>
79103
public static void WriteFile(string Path, string Strings)
80104
{
81-
if (!System.IO.File.Exists(Path))
105+
if (!File.Exists(Path))
82106
{
83-
System.IO.FileStream f = System.IO.File.Create(Path);
107+
FileStream f = File.Create(Path);
84108
f.Close();
85109
}
86-
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.GetEncoding("gb2312"));
110+
StreamWriter f2 = new StreamWriter(Path, false, System.Text.Encoding.GetEncoding("gb2312"));
87111
f2.Write(Strings);
88112
f2.Close();
89113
f2.Dispose();
@@ -97,12 +121,12 @@ public static void WriteFile(string Path, string Strings)
97121
/// <param name="encode">编码格式</param>
98122
public static void WriteFile(string Path, string Strings, Encoding encode)
99123
{
100-
if (!System.IO.File.Exists(Path))
124+
if (!File.Exists(Path))
101125
{
102-
System.IO.FileStream f = System.IO.File.Create(Path);
126+
FileStream f = File.Create(Path);
103127
f.Close();
104128
}
105-
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, encode);
129+
StreamWriter f2 = new StreamWriter(Path, false, encode);
106130
f2.Write(Strings);
107131
f2.Close();
108132
f2.Dispose();
@@ -126,7 +150,7 @@ public static void WriteFile(string Path, string Strings, Encoding encode)
126150
public static string ReadFile(string Path)
127151
{
128152
string s = "";
129-
if (!System.IO.File.Exists(Path))
153+
if (!File.Exists(Path))
130154
s = "不存在相应的目录";
131155
else
132156
{
@@ -148,7 +172,7 @@ public static string ReadFile(string Path)
148172
public static string ReadFile(string Path, Encoding encode)
149173
{
150174
string s = "";
151-
if (!System.IO.File.Exists(Path))
175+
if (!File.Exists(Path))
152176
s = "不存在相应的目录";
153177
else
154178
{

Blog.Core.Common/Helper/UtilConvert.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,17 @@ public static bool ObjToBool(this object thisValue)
167167
}
168168
return reval;
169169
}
170+
171+
172+
/// <summary>
173+
/// 获取当前时间的时间戳
174+
/// </summary>
175+
/// <param name="thisValue"></param>
176+
/// <returns></returns>
177+
public static string DateToTimeStamp(this DateTime thisValue)
178+
{
179+
TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
180+
return Convert.ToInt64(ts.TotalSeconds).ToString();
181+
}
170182
}
171183
}

Blog.Core.Common/LogHelper/LogLock.cs

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Blog.Core.Common.Helper;
2+
using Newtonsoft.Json;
23
using System;
34
using System.Collections.Generic;
45
using 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

Comments
 (0)