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

73 lines
2.2 KiB

2 weeks ago
  1. using Microsoft.Extensions.Logging;
  2. using Microsoft.Extensions.Logging.Abstractions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Shentun.Peis.PlugIns.Extensions
  9. {
  10. public class PluginLogger
  11. {
  12. private string _logFilePath;
  13. private string _logFilePath2;
  14. public PluginLogger()
  15. {
  16. var logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PlugLogs");
  17. if (!Directory.Exists(logDir))
  18. Directory.CreateDirectory(logDir);
  19. _logFilePath = Path.Combine(logDir, $"plugin_{DateTime.Now:yyyyMMdd}.log");
  20. _logFilePath2 = Path.Combine(logDir, $"plugin_result_{DateTime.Now:yyyyMMdd}.log");
  21. }
  22. // 写日志方法
  23. public void Log(string logT, string message)
  24. {
  25. try
  26. {
  27. var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{logT}] {message}";
  28. File.AppendAllText(_logFilePath, logEntry + Environment.NewLine);
  29. }
  30. catch { /* 忽略日志错误 */ }
  31. }
  32. public void Log(string logT, Exception ex, string message)
  33. {
  34. try
  35. {
  36. var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{logT}] {message}\n{ex}";
  37. File.AppendAllText(_logFilePath, logEntry + Environment.NewLine);
  38. }
  39. catch { /* 忽略日志错误 */ }
  40. }
  41. // 写日志方法
  42. public void LogResult(string logT, string message)
  43. {
  44. try
  45. {
  46. var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{logT}] {message}";
  47. File.AppendAllText(_logFilePath2, logEntry + Environment.NewLine);
  48. }
  49. catch { /* 忽略日志错误 */ }
  50. }
  51. // 写日志方法
  52. public void LogResult(string logT, Exception ex, string message)
  53. {
  54. try
  55. {
  56. var logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{logT}] {message}\n{ex}";
  57. File.AppendAllText(_logFilePath2, logEntry + Environment.NewLine);
  58. }
  59. catch { /* 忽略日志错误 */ }
  60. }
  61. }
  62. }