zhaolei
7 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
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
using Bootstrap.Admin.Query;
using Bootstrap.DataAccess;
using Prow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
 
namespace Bootstrap.Admin.Controllers.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class ExceptionsController : ControllerBase
    {
        /// <summary>
        /// 显示所有异常
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet]
        public QueryData<object> Get([FromQuery]QueryExceptionOption value)
        {
            return value.Retrieves();
        }
 
        /// <summary>
        /// 异常程序页面点击服务器日志按钮获取所有物理日志文件列表方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ButtonAuthorize(Url = "~/Admin/Exceptions", Auth = "log")]
        public IEnumerable<string> Post() => ExceptionsHelper.RetrieveLogFiles();
 
        /// <summary>
        /// 选中指定文件查看其内容方法
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ButtonAuthorize(Url = "~/Admin/Exceptions", Auth = "log")]
        public JsonResult Put([FromBody]ExceptionFileQuery exceptionFile)
        {
            var filePath = Path.Combine(AppContext.BaseDirectory, "Error");
            var logName = $"{Path.Combine(filePath, exceptionFile.FileName)}.log";
            var sb = new StringBuilder();
            if (System.IO.File.Exists(logName))
            {
                using var reader = new StreamReader(logName);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine()?.Replace("<", "&lt;").Replace(">", "&gt;");
                    if (!string.IsNullOrEmpty(line))
                    {
                        if (line == "General Information ") sb.AppendFormat("<h4><b>{0}</b></h4>", line);
                        else if (line.StartsWith("TimeStamp:")) sb.AppendFormat("<div class='logTs'>{0}</div>", line);
                        else if (line.EndsWith("Exception Information")) sb.AppendFormat("<div class='logExcep'>{0}</div>", line);
                        else if (line.StartsWith("Message:")) sb.AppendFormat("<div class='logMsg'>{0}</div>", line);
                        else if (line.StartsWith("ErrorSql:")) sb.AppendFormat("<div class='logSql'>{0}</div>", line);
                        else if (line.StartsWith("Exception Type: Prow.Data.DBAccessException")) sb.AppendFormat("<div class='logDbExcep'>{0}</div>", line);
                        else if (line.StartsWith("StackTrace Information")) sb.AppendFormat("<b>{0}</b><br>", line);
                        else sb.AppendFormat("{0}<br>", line);
                    }
                }
            }
            return new JsonResult(sb.ToString());
        }
 
        /// <summary>
        /// 查询服务器端日志文件参数类
        /// </summary>
        public class ExceptionFileQuery
        {
            /// <summary>
            /// 获取/设置 文件名称
            /// </summary>
            public string FileName { get; set; } = "";
        }
    }
}