LogEventSubscriber.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Furion.EventBus;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace Admin.NET.Core
  6. {
  7. /// <summary>
  8. /// 日志事件订阅
  9. /// </summary>
  10. public class LogEventSubscriber : IEventSubscriber
  11. {
  12. public IServiceProvider Services { get; }
  13. public LogEventSubscriber(IServiceProvider services)
  14. {
  15. Services = services;
  16. }
  17. /// <summary>
  18. /// 增加操作日志
  19. /// </summary>
  20. /// <param name="context"></param>
  21. /// <returns></returns>
  22. [EventSubscribe("Add:OpLog")]
  23. public async Task CreateOpLog(EventHandlerExecutingContext context)
  24. {
  25. using var scope = Services.CreateScope();
  26. var _rep = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogOp>>();
  27. await _rep.InsertAsync((SysLogOp)context.Source.Payload);
  28. }
  29. /// <summary>
  30. /// 增加异常日志
  31. /// </summary>
  32. /// <param name="context"></param>
  33. /// <returns></returns>
  34. [EventSubscribe("Add:ExLog")]
  35. public async Task CreateExLog(EventHandlerExecutingContext context)
  36. {
  37. using var scope = Services.CreateScope();
  38. var _rep = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogEx>>();
  39. await _rep.InsertAsync((SysLogEx)context.Source.Payload);
  40. }
  41. /// <summary>
  42. /// 增加访问日志
  43. /// </summary>
  44. /// <param name="context"></param>
  45. /// <returns></returns>
  46. [EventSubscribe("Add:VisLog")]
  47. public async Task CreateVisLog(EventHandlerExecutingContext context)
  48. {
  49. using var scope = Services.CreateScope();
  50. var _rep = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysLogVis>>();
  51. await _rep.InsertAsync((SysLogVis)context.Source.Payload);
  52. }
  53. }
  54. }