LoggingEventTarget.cs 797 B

123456789101112131415161718192021222324252627282930
  1. using NLog;
  2. using NLog.Targets;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SKMC.Api.Common
  9. {
  10. /// <summary>
  11. /// NLog 扩展
  12. /// </summary>
  13. public class LoggingEventTarget : TargetWithLayout
  14. {
  15. static readonly int LOGVIEW_MAXLINE = 1000;
  16. private readonly LogDataService logDataService = LogDataService.Instance();
  17. protected override void Write(LogEventInfo loggingEvent)
  18. {
  19. logDataService.LogEventModels.Insert(0, new LoggingEventModel(loggingEvent));
  20. if (logDataService.LogEventModels.Count == LOGVIEW_MAXLINE)
  21. {
  22. logDataService.LogEventModels.RemoveAt(LOGVIEW_MAXLINE - 1);
  23. }
  24. }
  25. }
  26. }