aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs
blob: 2a7e6be0fdbe6949aa20691c4b39690ea3138b0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.IO;
using System.Threading.Tasks;

namespace MediaBrowser.Controller.ClientEvent
{
    /// <inheritdoc />
    public class ClientEventLogger : IClientEventLogger
    {
        private readonly IServerApplicationPaths _applicationPaths;

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientEventLogger"/> class.
        /// </summary>
        /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
        public ClientEventLogger(IServerApplicationPaths applicationPaths)
        {
            _applicationPaths = applicationPaths;
        }

        /// <inheritdoc />
        public async Task<string> WriteDocumentAsync(string clientName, string clientVersion, Stream fileContents)
        {
            var fileName = $"upload_{clientName}_{clientVersion}_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid():N}.log";
            var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName);
            var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
            await using (fileStream.ConfigureAwait(false))
            {
                await fileContents.CopyToAsync(fileStream).ConfigureAwait(false);
                return fileName;
            }
        }
    }
}