using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web.Http; using Serilog; using XdCxRhDW.Dto; using XdCxRhDW.Repostory; using System.IO; using XdCxRhDW.Entity; using XdCxRhDW.Api; using XdCxRhDW.WebApi; using DPP_YH_Core.Extensions; using XdCxRhDW.App.Model; using System.Diagnostics; using System.Configuration; namespace XdCxRhDW.App.Controllers { /// /// 日志相关接口 /// public class LogController : BaseController { /// /// 将日志写入到平台 /// /// 日志信息 /// [HttpPost] public async Task Add(LogInfoDto dto) { try { using (RHDWContext db = new RHDWContext()) { LogRes res = new LogRes() { LogTime = dto.LogTime, Module = dto.Module, LogType = (Entity.EnumLogType)(int)dto.LogType, Msg = dto.Msg, }; db.LogRes.Add(res); await db.SaveChangesAsync(); } return Success(); } catch (TaskCanceledException) { Serilog.Log.Warning("日志信息写入超时"); return Error("日志信息写入超时"); } catch (Exception ex) { Serilog.Log.Error(ex, "日志信息写入异常"); return Error("日志信息写入异常"); } } /// /// 查询日志信息 /// /// 日志查询参数 /// [HttpPost] public async Task>> Query(LogQueryDto dto) { try { using (RHDWContext db = new RHDWContext()) { var query = db.LogRes.AsQueryable(); if (!string.IsNullOrWhiteSpace(dto.Module) && dto.Module != "全部") query = query.Where(p => p.Module == dto.Module); if (dto.LogTimeBegin != null && dto.LogTimeBegin != DateTime.MinValue) query = query.Where(p => p.LogTime >= dto.LogTimeBegin); if (dto.LogTimeEnd != null && dto.LogTimeEnd != DateTime.MinValue) query = query.Where(p => p.LogTime <= dto.LogTimeEnd); var res = await query.OrderBy(p => p.Module).ThenByDescending(t => t.LogTime).ToListAsync(); var dtoRes = res.Select(t => new LogInfoDto { ID = t.ID, LogTime = t.LogTime, LogType = (EnumLogTypeDto)(int)t.LogType, Module = t.Module, Msg = t.Msg, }).ToList(); return Success(dtoRes); } } catch (TaskCanceledException) { Serilog.Log.Warning($"日志信息查询超时.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}"); return Error>("日志信息查询超时"); } catch (Exception ex) { Serilog.Log.Error(ex, "日志信息查询异常.Mudule={dto.Module},TimeStart={dto.LogTimeBegin},TimeEnd={dto.LogTimeEnd}"); return Error>("日志信息查询异常"); } } /// /// 获取所有日志模块 /// /// [HttpPost] public async Task>> GetModules() { try { using (RHDWContext db = new RHDWContext()) { var res = await db.LogRes.Select(p => p.Module).Distinct().ToListAsync(); var dtoRes = res.Select(p => new LogModulesResDto() { Module = p }).OrderBy(p => p.Module).ToList(); return Success(dtoRes); } } catch (TaskCanceledException) { Serilog.Log.Warning($"日志模块查询超时"); return Error>("日志模块查询超时"); } catch (Exception ex) { Serilog.Log.Error(ex, "日志模块查询异常"); return Error>("日志模块查询异常"); } } /// /// 删除日志 /// /// /// [HttpPost] public async Task Delete(IEnumerable dto) { if (dto == null || !dto.Any()) return Success(); try { var ids = dto.Select(p => p.ID); using (RHDWContext db = new RHDWContext()) { var delItems = await db.LogRes.Where(p => ids.Contains(p.ID)).ToListAsync(); var delItemsReal = db.LogRes.RemoveRange(delItems); db.SaveChanges(); await Add(new LogInfoDto() { LogTime = DateTime.Now, LogType = EnumLogTypeDto.Info, Module = "平台", Msg = $"用户删除了{delItemsReal.LongCount()}行日志" }); return Success(); } } catch (TaskCanceledException) { Serilog.Log.Warning($"日志信息删除超时.共{dto.Count()}条记录"); return Error("日志信息删除超时"); } catch (Exception ex) { Serilog.Log.Error(ex, "日志信息删除异常.共{dto.Count()}条记录"); return Error("日志信息删除异常"); } } } }