LogFactory.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SKMC.Api.Common.Logger
  8. {
  9. public class LogFactory
  10. {
  11. /// <summary>
  12. /// "Log4Net" 或 "NLog"
  13. /// </summary>
  14. private static readonly string LoggerType = "NLog";
  15. public static ILogger Get(Type type)
  16. {
  17. if (LoggerType == "Log4Net")
  18. {
  19. return new Log4netLogger(type);
  20. }
  21. else if (LoggerType == "NLog")
  22. {
  23. return new NLogLogger(type.FullName);
  24. }
  25. else
  26. {
  27. throw new NotSupportedException($"Logger type {LoggerType} is not supported.");
  28. }
  29. }
  30. public static ILogger Get([CallerFilePath] string callerFilePath = "")
  31. {
  32. var callerType = System.IO.Path.GetFileNameWithoutExtension(callerFilePath);
  33. if (LoggerType == "Log4Net")
  34. {
  35. return new Log4netLogger(callerType);
  36. }
  37. else if (LoggerType == "NLog")
  38. {
  39. return new NLogLogger(callerType);
  40. }
  41. else
  42. {
  43. throw new NotSupportedException($"Logger type {LoggerType} is not supported.");
  44. }
  45. }
  46. public static ILogger Get(LogCategory logCategory)
  47. {
  48. if (logCategory == LogCategory.ProductionLogger)
  49. {
  50. return new NLogLogger("ProductionLogger");
  51. }
  52. else if(logCategory == LogCategory.ParameterLogger)
  53. {
  54. return new NLogLogger("ParameterLogger");
  55. }
  56. else if (logCategory == LogCategory.ProcessLogger)
  57. {
  58. return new NLogLogger("ProcessLogger");
  59. }
  60. else if(logCategory == LogCategory.ActionLogger)
  61. {
  62. return new NLogLogger("ActionLogger");
  63. }
  64. else
  65. {
  66. throw new NotSupportedException($"Logger category {logCategory.ToString()} is not supported.");
  67. }
  68. }
  69. public enum LogCategory
  70. {
  71. ProductionLogger,
  72. ParameterLogger,
  73. ProcessLogger,
  74. ActionLogger
  75. }
  76. }
  77. }