LOG.cs 3.7 KB

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