FileLogController.cs
2.18 KB
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
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
namespace WebMvc.Areas.Planned.Controllers
{
/// <summary>
/// 日志文件
/// </summary>
[Area("Planned")]
public class FileLogController : BaseController
{
private readonly DirectoryInfo _directory;
public FileLogController(IAuth authUtil) : base(authUtil)
{
_directory = new DirectoryInfo($"{Directory.GetCurrentDirectory()}//Logs");
}
public IActionResult Index()
{
var vm = _directory.GetDirectories().Select(x => new FileLogVM
{
DirectoryName = x.Name,
FileNames = new DirectoryInfo(x.FullName).GetFiles().Select(f => new FileItems
{
FileName = f.Name,
FileSize = $"{f.Length / 1024:N1}Kb"
}).OrderByDescending(f => f.FileName).ToList(),
}).OrderByDescending(x => x.DirectoryName).Take(3).ToList();
return View(vm);
}
/// <summary>
/// 获取日志文件文本
/// </summary>
/// <param name="directoryName">目录</param>
/// <param name="fileLogName">文件名称</param>
/// <returns></returns>
public IActionResult GetFileText(string directoryName, string fileLogName)
{
try
{
var logFilePath = $"{Directory.GetCurrentDirectory()}//Logs//{directoryName}//{fileLogName}";
if (!System.IO.File.Exists(logFilePath))
{
return Content($"文件不存在!");
}
FileStream fs = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
var log = sr.ReadToEnd();
sr.Close();
fs.Close();
return Content(log);
}
catch (Exception ex)
{
return Content($"日志读取异常:{ex.Message}");
}
}
}
}