using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using XdCxRhDW.Dto; namespace XdCxRhDW.UI.Lib { public class LogInfo { public LogInfo() { this.LogTime = DateTime.Now; this.LogType = EnumLogType.Info; } [Display(Name = "日志类型")] public EnumLogType LogType { get; set; } [Display(Name = "时间")] public DateTime LogTime { get; private set; } [Display(Name = "内容")] public string Msg { get; set; } } public enum EnumLogType { [Display(Name = "消息")] Info, [Display(Name = "警告")] Warning, [Display(Name = "错误")] Error } public static class LogUI { public static string BaseUrl; public static Action Logger; public static async Task Info(string msg) { var pro = Process.GetCurrentProcess(); try { await HttpHelper.PostRequestAsync>(BaseUrl + "Log/Add", new LogInfoDto() { LogTime = DateTime.Now, LogType = EnumLogTypeDto.Info, Module = pro.ProcessName, Msg = msg }); } catch { } XdCxRhDW.Framework.LogHelper.Info(msg); if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle)) Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Info, Msg = msg }); } public static async Task Warning(string msg, Exception ex = null) { var pro = Process.GetCurrentProcess(); try { await HttpHelper.PostRequestAsync>(BaseUrl + "Log/Add", new LogInfoDto() { LogTime = DateTime.Now, LogType = EnumLogTypeDto.Warning, Module = pro.ProcessName, Msg = msg }); } catch { } XdCxRhDW.Framework.LogHelper.Warning(msg,ex); if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle)) Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Warning, Msg = msg }); } public static async Task Error(string msg, Exception ex = null) { if (ex != null && ex.GetType() == typeof(AggregateException)) { var iex = (ex as AggregateException).InnerExceptions.FirstOrDefault(); if (iex != null) ex = iex; } var pro = Process.GetCurrentProcess(); try { await HttpHelper.PostRequestAsync>(BaseUrl + "Log/Add", new LogInfoDto() { LogTime = DateTime.Now, LogType = EnumLogTypeDto.Error, Module = pro.ProcessName, Msg = msg }); } catch { } XdCxRhDW.Framework.LogHelper.Error(msg, ex); if (!string.IsNullOrWhiteSpace(pro.MainWindowTitle)) Logger?.Invoke(new LogInfo() { LogType = EnumLogType.Error, Msg = msg }); } } }