using Bootstrap.Admin.Query;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Bootstrap.Admin.Controllers.Api
{
///
///
///
[Route("api/[controller]")]
[ApiController]
public class ExceptionsController : ControllerBase
{
///
/// 显示所有异常
///
///
///
[HttpGet]
public QueryData Get([FromQuery]QueryExceptionOption value)
{
return value.Retrieves();
}
///
/// 异常程序页面点击服务器日志按钮获取所有物理日志文件列表方法
///
///
[HttpPost]
[ButtonAuthorize(Url = "~/Admin/Exceptions", Auth = "log")]
public IEnumerable Post()
{
var filePath = Path.Combine(AppContext.BaseDirectory, "Error");
return Directory.Exists(filePath)
? Directory.GetFiles(filePath)
.Where(f => Path.GetExtension(f).Equals(".log", StringComparison.OrdinalIgnoreCase))
.Select(f => Path.GetFileNameWithoutExtension(f)).OrderByDescending(s => s)
: Enumerable.Empty();
}
///
/// 选中指定文件查看其内容方法
///
///
[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";
if (!System.IO.File.Exists(logName)) return new JsonResult("无此日志文件");
StringBuilder sb = new StringBuilder();
using (StreamReader reader = new StreamReader(logName))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Replace("<", "<").Replace(">", ">");
if (line == "General Information ") sb.AppendFormat("{0} ", line);
else if (line.StartsWith("TimeStamp:")) sb.AppendFormat("{0}
", line);
else if (line.EndsWith("Exception Information")) sb.AppendFormat("{0}
", line);
else if (line.StartsWith("Message:")) sb.AppendFormat("{0}
", line);
else if (line.StartsWith("ErrorSql:")) sb.AppendFormat("{0}
", line);
else if (line.StartsWith("Exception Type: Longbow.Data.DBAccessException")) sb.AppendFormat("{0}
", line);
else if (line.StartsWith("StackTrace Information")) sb.AppendFormat("{0} ", line);
else sb.AppendFormat("{0} ", line);
}
}
return new JsonResult(sb.ToString());
}
///
///
///
public class ExceptionFileQuery
{
///
///
///
public string FileName { get; set; }
}
}
}