You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.md 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # The Logger in LLamaSharp
  2. LLamaSharp supports customized logger because it could be used in many kinds of applications, like Winform/WPF, WebAPI and Blazor, so that the preference of logger varies.
  3. ## Define customized logger
  4. What you need to do is to implement the `ILogger` interface.
  5. ```cs
  6. public interface ILLamaLogger
  7. {
  8. public enum LogLevel
  9. {
  10. Info,
  11. Debug,
  12. Warning,
  13. Error
  14. }
  15. void Log(string source, string message, LogLevel level);
  16. }
  17. ```
  18. The `source` specifies where the log message is from, which could be a function, a class, etc..
  19. The `message` is the log message itself.
  20. The `level` is the level of the information in the log. As shown above, there're four levels, which are `info`, `debug`, `warning` and `error` respectively.
  21. The following is a simple example of the logger implementation:
  22. ```cs
  23. public sealed class LLamaDefaultLogger : ILLamaLogger
  24. {
  25. private static readonly Lazy<LLamaDefaultLogger> _instance = new Lazy<LLamaDefaultLogger>(() => new LLamaDefaultLogger());
  26. private bool _toConsole = true;
  27. private bool _toFile = false;
  28. private FileStream? _fileStream = null;
  29. private StreamWriter _fileWriter = null;
  30. public static LLamaDefaultLogger Default => _instance.Value;
  31. private LLamaDefaultLogger()
  32. {
  33. }
  34. public LLamaDefaultLogger EnableConsole()
  35. {
  36. _toConsole = true;
  37. return this;
  38. }
  39. public LLamaDefaultLogger DisableConsole()
  40. {
  41. _toConsole = false;
  42. return this;
  43. }
  44. public LLamaDefaultLogger EnableFile(string filename, FileMode mode = FileMode.Append)
  45. {
  46. _fileStream = new FileStream(filename, mode, FileAccess.Write);
  47. _fileWriter = new StreamWriter(_fileStream);
  48. _toFile = true;
  49. return this;
  50. }
  51. public LLamaDefaultLogger DisableFile(string filename)
  52. {
  53. if (_fileWriter is not null)
  54. {
  55. _fileWriter.Close();
  56. _fileWriter = null;
  57. }
  58. if (_fileStream is not null)
  59. {
  60. _fileStream.Close();
  61. _fileStream = null;
  62. }
  63. _toFile = false;
  64. return this;
  65. }
  66. public void Log(string source, string message, LogLevel level)
  67. {
  68. if (level == LogLevel.Info)
  69. {
  70. Info(message);
  71. }
  72. else if (level == LogLevel.Debug)
  73. {
  74. }
  75. else if (level == LogLevel.Warning)
  76. {
  77. Warn(message);
  78. }
  79. else if (level == LogLevel.Error)
  80. {
  81. Error(message);
  82. }
  83. }
  84. public void Info(string message)
  85. {
  86. message = MessageFormat("info", message);
  87. if (_toConsole)
  88. {
  89. Console.ForegroundColor = ConsoleColor.White;
  90. Console.WriteLine(message);
  91. Console.ResetColor();
  92. }
  93. if (_toFile)
  94. {
  95. Debug.Assert(_fileStream is not null);
  96. Debug.Assert(_fileWriter is not null);
  97. _fileWriter.WriteLine(message);
  98. }
  99. }
  100. public void Warn(string message)
  101. {
  102. message = MessageFormat("warn", message);
  103. if (_toConsole)
  104. {
  105. Console.ForegroundColor = ConsoleColor.Yellow;
  106. Console.WriteLine(message);
  107. Console.ResetColor();
  108. }
  109. if (_toFile)
  110. {
  111. Debug.Assert(_fileStream is not null);
  112. Debug.Assert(_fileWriter is not null);
  113. _fileWriter.WriteLine(message);
  114. }
  115. }
  116. public void Error(string message)
  117. {
  118. message = MessageFormat("error", message);
  119. if (_toConsole)
  120. {
  121. Console.ForegroundColor = ConsoleColor.Red;
  122. Console.WriteLine(message);
  123. Console.ResetColor();
  124. }
  125. if (_toFile)
  126. {
  127. Debug.Assert(_fileStream is not null);
  128. Debug.Assert(_fileWriter is not null);
  129. _fileWriter.WriteLine(message);
  130. }
  131. }
  132. private string MessageFormat(string level, string message)
  133. {
  134. DateTime now = DateTime.Now;
  135. string formattedDate = now.ToString("yyyy.MM.dd HH:mm:ss");
  136. return $"[{formattedDate}][{level}]: {message}";
  137. }
  138. }
  139. ```