LogFactory.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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
  8. {
  9. public class LogFactory
  10. {
  11. /// <summary>
  12. /// "Log4Net" 或 "NLog"
  13. /// </summary>
  14. private static readonly string LoggerType = "Log4Net";
  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. }
  47. }