jellyfin/MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs

58 lines
2.4 KiB
C#
Raw Normal View History

2021-04-26 09:02:26 -04:00
using System;
2021-10-26 20:42:17 -04:00
using System.IO;
using System.Threading.Tasks;
2021-10-27 21:20:14 -04:00
using MediaBrowser.Controller.Net;
2021-04-26 09:02:26 -04:00
using MediaBrowser.Model.ClientLog;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Controller.ClientEvent
{
/// <inheritdoc />
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<ClientEventLogger> _logger;
2021-10-26 20:42:17 -04:00
private readonly IServerApplicationPaths _applicationPaths;
2021-04-26 09:02:26 -04:00
/// <summary>
/// Initializes a new instance of the <see cref="ClientEventLogger"/> class.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger{ClientEventLogger}"/> interface.</param>
2021-10-26 20:42:17 -04:00
/// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
public ClientEventLogger(
ILogger<ClientEventLogger> logger,
IServerApplicationPaths applicationPaths)
2021-04-26 09:02:26 -04:00
{
_logger = logger;
2021-10-26 20:42:17 -04:00
_applicationPaths = applicationPaths;
2021-04-26 09:02:26 -04:00
}
/// <inheritdoc />
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);
}
2021-10-26 20:42:17 -04:00
/// <inheritdoc />
public async Task<string> WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents)
2021-10-26 20:42:17 -04:00
{
2021-10-27 21:40:35 -04:00
var fileName = $"upload_{authorizationInfo.Client}_{(authorizationInfo.IsApiKey ? "apikey" : authorizationInfo.Version)}_{DateTime.UtcNow:yyyyMMddHHmmss}.log";
2021-10-26 20:42:17 -04:00
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;
2021-10-26 20:42:17 -04:00
}
2021-04-26 09:02:26 -04:00
}
2021-10-26 20:42:17 -04:00
}