ClientEventLogger.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. namespace MediaBrowser.Controller.ClientEvent
  5. {
  6. /// <inheritdoc />
  7. public class ClientEventLogger : IClientEventLogger
  8. {
  9. private readonly IServerApplicationPaths _applicationPaths;
  10. /// <summary>
  11. /// Initializes a new instance of the <see cref="ClientEventLogger"/> class.
  12. /// </summary>
  13. /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
  14. public ClientEventLogger(IServerApplicationPaths applicationPaths)
  15. {
  16. _applicationPaths = applicationPaths;
  17. }
  18. /// <inheritdoc />
  19. public async Task<string> WriteDocumentAsync(string clientName, string clientVersion, Stream fileContents)
  20. {
  21. var fileName = $"upload_{clientName}_{clientVersion}_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid():N}.log";
  22. var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName);
  23. var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
  24. await using (fileStream.ConfigureAwait(false))
  25. {
  26. await fileContents.CopyToAsync(fileStream).ConfigureAwait(false);
  27. return fileName;
  28. }
  29. }
  30. }
  31. }