using Bootstrap.Admin.Query; using Bootstrap.DataAccess; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Bootstrap.Admin.Controllers.Api { /// /// /// [Route("api/[controller]")] public class ExceptionsController : Controller { /// /// 显示所有异常 /// /// /// [HttpGet] public QueryData Get(QueryExceptionOption value) { return value.RetrieveData(); } /// /// /// /// [HttpPost] public IEnumerable Post() { var filePath = Path.Combine(AppContext.BaseDirectory, "Error"); return Directory.GetFiles(filePath) .Where(f => Path.GetExtension(f).Equals(".log", System.StringComparison.OrdinalIgnoreCase)) .Select(f => Path.GetFileNameWithoutExtension(f)).OrderByDescending(s => s); } /// /// /// /// [HttpPut] 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; } } } }