LOG.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.RT.Log
  8. {
  9. public static class LOG
  10. {
  11. public static ICommonLog InnerLogger { set; private get; }
  12. public static void Info(string message, bool isTraceOn=false, int traceLevel=2)
  13. {
  14. if (InnerLogger != null)
  15. InnerLogger.Info(message, isTraceOn, GetFormatStackFrameInfo(traceLevel));
  16. }
  17. static string GetFormatStackFrameInfo(int traceLevel)
  18. {
  19. StackFrame sf = new StackTrace(true).GetFrame(traceLevel+1);
  20. string pathFile = sf.GetFileName();
  21. string file = string.IsNullOrEmpty(pathFile) ? "" : pathFile.Substring(pathFile.LastIndexOf('\\') + 1);
  22. return $"{file}\tLine {sf.GetFileLineNumber()}\t{sf.GetMethod().Name}()";
  23. }
  24. public static void Warning(string message, params object[] args)
  25. {
  26. if (InnerLogger != null)
  27. InnerLogger.Warning(string.Format(message, args));
  28. }
  29. public static void Warning(string message, int traceLevel = 2)
  30. {
  31. if (InnerLogger != null)
  32. InnerLogger.Warning(message, GetFormatStackFrameInfo(traceLevel));
  33. }
  34. public static void Warning(string message, int traceLevel = 2, params object[] args)
  35. {
  36. if (InnerLogger != null)
  37. InnerLogger.Warning(string.Format(message, args), GetFormatStackFrameInfo(traceLevel));
  38. }
  39. public static void Error(string message, int traceLevel = 2)
  40. {
  41. if (InnerLogger != null)
  42. InnerLogger.Error(message, GetFormatStackFrameInfo(traceLevel));
  43. }
  44. public static void Warning(string message, Exception ex, int traceLevel = 2)
  45. {
  46. if (InnerLogger != null)
  47. InnerLogger.Warning(message, ex, GetFormatStackFrameInfo(traceLevel));
  48. }
  49. public static void Error(string message, Exception ex, int traceLevel = 2)
  50. {
  51. if (InnerLogger != null)
  52. InnerLogger.Error(message, ex, GetFormatStackFrameInfo(traceLevel));
  53. }
  54. public static void Write(Exception ex, int traceLevel = 2)
  55. {
  56. Error("", ex, traceLevel+1);
  57. }
  58. public static void Write(Exception ex, string message, int traceLevel = 2)
  59. {
  60. Error(message, ex, traceLevel + 1);
  61. }
  62. public static void Write(string message, int traceLevel = 2)
  63. {
  64. Info(message, false, traceLevel + 1);
  65. }
  66. }
  67. }