| | |
| | | using System; |
| | | using Microsoft.AspNetCore.Mvc; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.BaseServices; |
| | | using WIDESEAWCS_Model.Models; |
| | | |
| | | namespace WIDESEAWCS_ISystemServices |
| | | { |
| | | #region å®ä½ç±» |
| | | public class DirInfo |
| | | { |
| | | /// <summary> |
| | | /// å½åè·¯å¾ |
| | | /// </summary> |
| | | public string dirPath { get; set; } |
| | | |
| | | /// <summary> |
| | | /// å½ååç§° |
| | | /// </summary> |
| | | public string dirName { get; set; } |
| | | |
| | | /// <summary> |
| | | /// åæä»¶ |
| | | /// </summary> |
| | | public List<FileDataInfo> files { get; set; } |
| | | |
| | | /// <summary> |
| | | /// åæä»¶å¤¹ |
| | | /// </summary> |
| | | public List<DirInfo> dirs { get; set; } |
| | | } |
| | | |
| | | public class FileDataInfo |
| | | { |
| | | public string filePath { get; set; } |
| | | |
| | | public string fileName { get; set; } |
| | | |
| | | public long fileSize { get; set; } |
| | | } |
| | | |
| | | public class GetLogParm |
| | | { |
| | | /// <summary> |
| | | /// æä»¶å°å |
| | | /// </summary> |
| | | public string path; |
| | | |
| | | /// <summary> |
| | | /// 读åè¿åº¦ |
| | | /// </summary> |
| | | public float percent; |
| | | |
| | | /// <summary> |
| | | /// æå¤§è¯»åå¤§å° |
| | | /// </summary> |
| | | public double maxsize_KB = 20; |
| | | |
| | | /// <summary> |
| | | /// å½åèµ·å§ä½ç½® |
| | | /// </summary> |
| | | public long topStartPos = 0; |
| | | } |
| | | #endregion |
| | | public interface ISys_LogService : IService<Sys_Log> |
| | | { |
| | | /// <summary> |
| | | /// éå½è·åæä»¶ä¿¡æ¯ |
| | | /// </summary> |
| | | /// <param name="dirPath"></param> |
| | | /// <returns></returns> |
| | | private DirInfo GetDirInfo(string dirPath) |
| | | { |
| | | //å½åæä»¶å¤¹ |
| | | var dirInfo = new DirInfo(); |
| | | dirInfo.dirName = Path.GetFileName(dirPath); |
| | | |
| | | //åæä»¶ |
| | | List<FileDataInfo> files = new List<FileDataInfo>(); |
| | | foreach (var file in Directory.GetFiles(dirPath)) |
| | | { |
| | | files.Add(new FileDataInfo() |
| | | { |
| | | filePath = file, |
| | | fileName = Path.GetFileName(file) |
| | | }); |
| | | } |
| | | |
| | | //åæä»¶å¤¹ |
| | | var dirs = Directory.GetDirectories(dirPath); |
| | | dirInfo.dirs = new List<DirInfo>(); |
| | | foreach (var dir in dirs) |
| | | { |
| | | dirInfo.dirs.Add(GetDirInfo(dir)); |
| | | } |
| | | |
| | | //åæä»¶å¤¹ï¼ä¸åç®å½åå¹¶ |
| | | foreach (var file in files) |
| | | { |
| | | dirInfo.dirs.Add(new DirInfo() { dirPath = file.filePath, dirName = file.fileName }); |
| | | } |
| | | return dirInfo; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è·åæ¥å¿æä»¶å表 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public WebResponseContent GetLogList() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | string path = System.Environment.CurrentDirectory + "/log"; |
| | | if (!System.IO.Directory.Exists(path)) |
| | | { |
| | | return content.Error("ææ æ¥å¿æä»¶"); |
| | | } |
| | | |
| | | content.Data = GetDirInfo(path); |
| | | content.OK(); |
| | | return content; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è·åæä»¶å
容 |
| | | /// </summary> |
| | | /// <param name="parm"></param> |
| | | /// <returns></returns> |
| | | public WebResponseContent GetLogData(GetLogParm parm) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | string res = ""; |
| | | //æ¯å¦è¯»åå°æå |
| | | bool isEnd = false; |
| | | long startIndex = 0; |
| | | //æä»¶å¤§å° |
| | | long len = 0; |
| | | try |
| | | { |
| | | if (!System.IO.File.Exists(parm.path)) |
| | | { |
| | | throw new Exception($"æä»¶{parm.path}ä¸åå¨ï¼"); |
| | | } |
| | | using (FileStream fs = new FileStream(parm.path, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| | | { |
| | | //æå¤§è¯»åå¤§å° |
| | | int maxsize = (int)(1024 * parm.maxsize_KB); |
| | | len = fs.Length; |
| | | long startPos = (long)(len * (parm.percent / 100));//èµ·å§ä½ç½® |
| | | long readLen = len - startPos;//读åé¿åº¦ |
| | | |
| | | //ååå è½½ |
| | | if (parm.topStartPos != 0) |
| | | { |
| | | startPos = parm.topStartPos - maxsize; |
| | | if (startPos < 0) |
| | | { |
| | | //已读å°èµ·å§ä½ |
| | | startPos = 0; |
| | | readLen = parm.topStartPos; |
| | | } |
| | | else |
| | | { |
| | | readLen = maxsize; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | //读å大尿¯å¦è¶
åºæå¤§é¿åº¦ |
| | | if (readLen > maxsize) |
| | | { |
| | | readLen = maxsize; |
| | | } |
| | | else |
| | | { |
| | | isEnd = true; |
| | | } |
| | | } |
| | | |
| | | //å è½½100%ï¼ææå¤§å
容读å |
| | | if (parm.percent == 100) |
| | | { |
| | | if (len < maxsize) |
| | | { |
| | | startPos = 0; |
| | | readLen = len; |
| | | } |
| | | else |
| | | { |
| | | startPos = len - maxsize; |
| | | readLen = maxsize; |
| | | } |
| | | } |
| | | |
| | | fs.Seek(startPos, SeekOrigin.Begin); |
| | | var buffer = new byte[readLen]; |
| | | fs.Read(buffer, 0, (int)readLen); |
| | | |
| | | startIndex = startPos; |
| | | if (startPos != 0 && (parm.percent != 0 || parm.topStartPos != 0)) |
| | | { |
| | | //䏿¯ä»å¤´å è½½ï¼å é¤å¯è½ä¸å®æ´ç第ä¸è¡ |
| | | int skipCount = 0; |
| | | for (int i = 0; i < buffer.Length; i++) |
| | | { |
| | | if (buffer[i] == 10) |
| | | { |
| | | skipCount = i; |
| | | break; |
| | | } |
| | | } |
| | | if (skipCount != 0) |
| | | { |
| | | //廿æ¢è¡ |
| | | skipCount++; |
| | | //䏿¬¡è¯»ååå»¶ |
| | | startIndex += skipCount; |
| | | } |
| | | res = Encoding.UTF8.GetString(buffer.Skip(skipCount).ToArray()); |
| | | } |
| | | else |
| | | { |
| | | res = Encoding.UTF8.GetString(buffer); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return content.Error(ex.Message); |
| | | } |
| | | return content.OK(data: new { content = res, isEnd, startIndex, len }); |
| | | } |
| | | |
| | | } |
| | | } |