LogEventSubscriber.cs 1.6 KB

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