LoggingInterceptor.cs 878 B

123456789101112131415161718192021222324252627282930
  1. using Castle.DynamicProxy;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Hotline.Settings.SystemLogDomain;
  8. public class LoggingInterceptor : IInterceptor
  9. {
  10. public void Intercept(IInvocation invocation)
  11. {
  12. // 检查方法是否有 LogAttribute
  13. var logAttribute = invocation.Method.GetCustomAttributes(typeof(LogToDatabaseAttribute), true)
  14. .FirstOrDefault() as LogToDatabaseAttribute;
  15. if (logAttribute != null)
  16. {
  17. Console.WriteLine($"[LOG] {logAttribute.Message}: {invocation.Method.Name}");
  18. }
  19. // 执行实际方法
  20. invocation.Proceed();
  21. if (logAttribute != null)
  22. {
  23. Console.WriteLine($"[LOG] Finished: {invocation.Method.Name}");
  24. }
  25. }
  26. }