diff options
| author | Claus Vium <cvium@users.noreply.github.com> | 2021-11-08 07:46:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-08 07:46:51 +0100 |
| commit | 0ee43f897b4c06c23cb741e85908527a46687769 (patch) | |
| tree | 20762ea3a72ff6ae8bdd65a501685216ee279f2b /MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs | |
| parent | 195831ad4a538e0a44714dfdc31bd007ab5c3e66 (diff) | |
| parent | 666e95e27f92eabb15d6ab7ae399bdbb95eeb318 (diff) | |
Merge pull request #5918 from crobibero/client-logger
Add endpoint to log client events
Diffstat (limited to 'MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs')
| -rw-r--r-- | MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs b/MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs new file mode 100644 index 000000000..82b5b4593 --- /dev/null +++ b/MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs @@ -0,0 +1,55 @@ +using System; +using System.IO; +using System.Threading.Tasks; +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; + private readonly IServerApplicationPaths _applicationPaths; + + /// <summary> + /// Initializes a new instance of the <see cref="ClientEventLogger"/> class. + /// </summary> + /// <param name="logger">Instance of the <see cref="ILogger{ClientEventLogger}"/> interface.</param> + /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param> + public ClientEventLogger( + ILogger<ClientEventLogger> logger, + IServerApplicationPaths applicationPaths) + { + _logger = logger; + _applicationPaths = applicationPaths; + } + + /// <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); + } + + /// <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); + await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None); + await fileContents.CopyToAsync(fileStream).ConfigureAwait(false); + return fileName; + } + } +} |
