using System; using System.IO; using System.Threading.Tasks; using MediaBrowser.Controller.Net; using MediaBrowser.Model.ClientLog; using Microsoft.Extensions.Logging; namespace MediaBrowser.Controller.ClientEvent { /// public class ClientEventLogger : IClientEventLogger { private const string LogString = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level}] [{ClientName}:{ClientVersion}]: UserId: {UserId} DeviceId: {DeviceId}{NewLine}{Message}"; private readonly ILogger _logger; private readonly IServerApplicationPaths _applicationPaths; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of the interface. public ClientEventLogger( ILogger logger, IServerApplicationPaths applicationPaths) { _logger = logger; _applicationPaths = applicationPaths; } /// public void Log(ClientLogEvent clientLogEvent) { _logger.Log( LogLevel.Critical, LogString, clientLogEvent.Timestamp, clientLogEvent.Level.ToString(), clientLogEvent.ClientName, clientLogEvent.ClientVersion, clientLogEvent.UserId ?? Guid.Empty, clientLogEvent.DeviceId, Environment.NewLine, clientLogEvent.Message); } /// public async Task WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents) { var fileName = $"upload_{authorizationInfo.Client}_{(authorizationInfo.IsApiKey ? "apikey" : authorizationInfo.Version)}_{DateTime.UtcNow:yyyyMMddHHmmss}.log"; var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName); await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None); await fileContents.CopyToAsync(fileStream).ConfigureAwait(false); await fileStream.FlushAsync().ConfigureAwait(false); return fileName; } } }