using Bootstrap.Admin.Models; using Bootstrap.DataAccess; using Longbow.Web.Mvc; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.Http; namespace Bootstrap.Admin.Controllers { public class ExceptionsController : ApiController { /// /// 显示所有异常 /// /// /// [HttpGet] public QueryData Get([FromUri]QueryExceptionOption value) { return value.RetrieveData(); } /// /// /// /// [HttpPost] public IEnumerable Post() { var filePath = HttpContext.Current.Server.MapPath("~/App_Data/ErrorLog"); return Directory.GetFiles(filePath) .Where(f => Path.GetExtension(f).Equals(".log", System.StringComparison.OrdinalIgnoreCase)) .Select(f => Path.GetFileNameWithoutExtension(f)).OrderByDescending(s => s); } /// /// /// /// [HttpPut] public string Put([FromBody]string fileName) { var logName = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/ErrorLog/{0}.log", fileName)); if (!File.Exists(logName)) return "无此日志文件"; 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 sb.ToString(); } } }