| | |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core; |
| | | using WIDESEA_Core.BaseServices; |
| | | using WIDESEAWCS_IRepository; |
| | | using WIDESEAWCS_ISystemServices; |
| | |
| | | public Sys_LogService(ISys_LogRepository BaseDal) : base(BaseDal) |
| | | { |
| | | } |
| | | public WebResponseContent GetLogName() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | List<object> data = new List<object>(); |
| | | DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\Logs\\"); |
| | | DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories().OrderByDescending(x => x.CreationTime).ToArray(); |
| | | int k = 2020; |
| | | for (int i = 0; i < firstDirectoryInfos.Length; i++) |
| | | { |
| | | if (firstDirectoryInfos[i].Name != "Info") |
| | | { |
| | | FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles(); |
| | | List<object> values = new List<object>(); |
| | | for (int j = 0; j < nextFileInfos.Length; j++) |
| | | { |
| | | values.Add(new { label = nextFileInfos[j].Name, id = k, hidden = true, fatherNode = firstDirectoryInfos[i].Name }); |
| | | k++; |
| | | } |
| | | data.Add(new { label = firstDirectoryInfos[i].Name, children = values, id = i, hidden = false }); |
| | | } |
| | | } |
| | | |
| | | FileInfo[] nextFileInfo = folder.GetFiles(); |
| | | List<object> value = new List<object>(); |
| | | for (int j = 0; j < nextFileInfo.Length; j++) |
| | | { |
| | | value.Add(new { label = nextFileInfo[j].Name, id = k, hidden = true, fatherNode = folder.Name }); |
| | | k++; |
| | | } |
| | | data.Add(new { label = folder.Name, children = value, id = 1, hidden = false }); |
| | | |
| | | return WebResponseContent.Instance.OK(data: data); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent GetLog(string fileName) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | List<FileInfo> files = new List<FileInfo>(); |
| | | DirectoryInfo folder = new DirectoryInfo(AppContext.BaseDirectory + "\\Logs\\"); |
| | | DirectoryInfo[] firstDirectoryInfos = folder.GetDirectories(); |
| | | for (int i = 0; i < firstDirectoryInfos.Length; i++) |
| | | { |
| | | FileInfo[] nextFileInfos = firstDirectoryInfos[i].GetFiles(); |
| | | files.AddRange(nextFileInfos); |
| | | } |
| | | |
| | | FileInfo[] nextFileInfo = folder.GetFiles(); |
| | | files.AddRange(nextFileInfo); |
| | | if (files.Count > 0) |
| | | { |
| | | FileInfo file = files.Where(x => x.Name == fileName).FirstOrDefault(); |
| | | if (file == null) |
| | | { |
| | | return WebResponseContent.Instance.Error($"未找到日志文件: {fileName}"); |
| | | } |
| | | |
| | | // 使用共享读取模式 |
| | | using (FileStream stream = new FileStream( |
| | | file.FullName, |
| | | FileMode.Open, |
| | | FileAccess.Read, |
| | | FileShare.ReadWrite)) |
| | | using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) |
| | | { |
| | | StringBuilder text = new StringBuilder(); |
| | | List<string> lines = new List<string>(); |
| | | while (!reader.EndOfStream) |
| | | { |
| | | var line = reader.ReadLine(); |
| | | lines.Add(line); |
| | | } |
| | | |
| | | content = WebResponseContent.Instance.OK(data: lines); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | content = WebResponseContent.Instance.Error($"未找到日志文件,【{fileName}】"); |
| | | } |
| | | } |
| | | catch (IOException ex) |
| | | { |
| | | if (IsFileLockedException(ex)) |
| | | { |
| | | content = WebResponseContent.Instance.Error($"日志文件正在被系统写入,请稍后再试"); |
| | | } |
| | | else |
| | | { |
| | | content = WebResponseContent.Instance.Error($"打开日志文件错误,{ex.Message}"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | content = WebResponseContent.Instance.Error($"打开日志文件错误,{ex.Message}"); |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | private bool IsFileLockedException(IOException ex) |
| | | { |
| | | int errorCode = ex.HResult & 0xFFFF; |
| | | return errorCode == 32 || errorCode == 33; // ERROR_SHARING_VIOLATION or ERROR_LOCK_VIOLATION |
| | | } |
| | | } |
| | | } |