123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Text;
- namespace IMCS.CCS.Common
- {
- using NLog;
- /// <summary>
- ///日志
- /// </summary>
- public static class LogUtil
- {
- private static Logger Logger { get; set; }
- public static void Init(Logger logger)
- {
- Logger = logger;
- }
- /// <summary>
- /// 日志对象
- /// </summary>
- /// <returns></returns>
- public static Logger GetLogger() => Logger;
- /// <summary>
- /// 写入日志
- /// </summary>
- /// <param name="text"></param>
- public static void Write(string text)
- {
- Logger.Info(text);
- }
- /// <summary>
- /// Debug
- /// </summary>
- /// <param name="text"></param>
- public static void WriteDebug(string text)
- {
- Logger.Debug(text);
- }
- /// <summary>
- /// 写入日志
- /// </summary>
- /// <param name="text"></param>
- /// <param name="exception"></param>
- public static void Write(string text, Exception exception)
- {
- WriteError(exception, null, sb => { sb.Append($"{text}\r\n"); });
- }
- /// <summary>
- /// 写入日志
- /// </summary>
- /// <param name="exception"></param>
- /// <param name="userHostAddress"></param>
- /// <param name="callBack"></param>
- public static void WriteError(Exception exception, string userHostAddress,
- Action<StringBuilder> 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());
- }
- }
- }
|