Нема описа

MonLogger.cs 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Logger.Services.Interfaces;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading;
  7. namespace Logger.Services
  8. {
  9. public class MonLogger : IMonLogger
  10. {
  11. private Queue<string> _queueAction;
  12. public MonLogger()
  13. {
  14. _queueAction = new Queue<string>();
  15. }
  16. public async void Write()
  17. {
  18. if (Monitor.TryEnter(_queueAction))
  19. {
  20. try
  21. {
  22. while (_queueAction.Count > 0)
  23. {
  24. string docPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
  25. using StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "Log_aoao.txt"), true);
  26. await outputFile.WriteAsync(_queueAction.Dequeue() + Environment.NewLine);
  27. }
  28. }
  29. finally
  30. {
  31. Monitor.Exit(_queueAction);
  32. }
  33. }
  34. }
  35. public void AddAction(string action)
  36. {
  37. if (Monitor.TryEnter(_queueAction))
  38. {
  39. try
  40. {
  41. _queueAction.Enqueue(action);
  42. }
  43. finally
  44. {
  45. Monitor.Exit(_queueAction);
  46. }
  47. }
  48. }
  49. }
  50. }

Powered by TurnKey Linux.