huangxiaoqiang
5 天以前 f814747cb35a71dfa8349fde66a173af4b18236e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Microsoft.AspNetCore.Mvc;
using System.Text;
using System.IO;
 
namespace WIDESEA_WMSServer.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class Sys_LogController : Controller
    {
        [HttpPost, Route("GetLogName"), AllowAnonymous]
        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 });
                    }
                }
 
                return WebResponseContent.Instance.OK(data: data);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        [HttpPost, Route("GetLog"), AllowAnonymous]
        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);
                }
                if (files.Count > 0)
                {
                    FileInfo file = files.Where(x => x.Name == fileName).FirstOrDefault();
                    using StreamReader stream = new StreamReader(file.FullName);
                    StringBuilder text = new StringBuilder();
                    List<string> lines = new List<string>();
                    int i = 0;
                    while (stream.Peek() >= 0)
                    {
                        var line = stream.ReadLine();
                        lines.Add(line);
                    }
 
                    content = WebResponseContent.Instance.OK(data: lines);
                }
                else
                {
                    content = WebResponseContent.Instance.Error($"未找到日志文件,【{fileName}】");
                }
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"打开日志文件错误,{ex.Message}");
            }
            return content;
        }
 
        
    }
}