FileOutput.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using BestHTTP.Extensions;
  3. using BestHTTP.PlatformSupport.Memory;
  4. namespace BestHTTP.Logger
  5. {
  6. public sealed class FileOutput : ILogOutput
  7. {
  8. private System.IO.Stream fileStream;
  9. public FileOutput(string fileName)
  10. {
  11. this.fileStream = HTTPManager.IOService.CreateFileStream(fileName, PlatformSupport.FileSystem.FileStreamModes.Create);
  12. }
  13. public void Write(Loglevels level, string logEntry)
  14. {
  15. if (this.fileStream != null && !string.IsNullOrEmpty(logEntry))
  16. {
  17. int count = System.Text.Encoding.UTF8.GetByteCount(logEntry);
  18. var buffer = BufferPool.Get(count, true);
  19. try
  20. {
  21. System.Text.Encoding.UTF8.GetBytes(logEntry, 0, logEntry.Length, buffer, 0);
  22. this.fileStream.Write(buffer, 0, count);
  23. this.fileStream.WriteLine();
  24. }
  25. finally
  26. {
  27. BufferPool.Release(buffer);
  28. }
  29. this.fileStream.Flush();
  30. }
  31. }
  32. public void Dispose()
  33. {
  34. if (this.fileStream != null)
  35. {
  36. this.fileStream.Close();
  37. this.fileStream = null;
  38. }
  39. GC.SuppressFinalize(this);
  40. }
  41. }
  42. }