using System; using System.Text; namespace IMCS.CCS.Common { using NLog; /// ///日志 /// public static class LogUtil { private static Logger Logger { get; set; } public static void Init(Logger logger) { Logger = logger; } /// /// 日志对象 /// /// public static Logger GetLogger() => Logger; /// /// 写入日志 /// /// public static void Write(string text) { Logger.Info(text); } /// /// Debug /// /// public static void WriteDebug(string text) { Logger.Debug(text); } /// /// 写入日志 /// /// /// public static void Write(string text, Exception exception) { WriteError(exception, null, sb => { sb.Append($"{text}\r\n"); }); } /// /// 写入日志 /// /// /// /// public static void WriteError(Exception exception, string userHostAddress, Action callBack = null) { var sb = new StringBuilder(); var message = "异常信息: " + exception.Message; var source = "错误源:" + exception.Source; var stackTrace = "堆栈信息:" + exception.StackTrace; sb.Append($"\r\n"); callBack?.Invoke(sb); if (!string.IsNullOrWhiteSpace(userHostAddress)) { sb.Append($"{userHostAddress}\r\n"); } if (!string.IsNullOrWhiteSpace(message)) { sb.Append($"{message}\r\n"); } if (!string.IsNullOrWhiteSpace(source)) { sb.Append($"{source}\r\n"); } if (!string.IsNullOrWhiteSpace(stackTrace)) { sb.Append($"{stackTrace}\r\n"); } Logger.Error(exception, sb.ToString()); } } }