Merge pull request #5918 from crobibero/client-logger
Add endpoint to log client events
This commit is contained in:
commit
0ee43f897b
|
@ -56,6 +56,7 @@ using MediaBrowser.Common.Updates;
|
|||
using MediaBrowser.Controller;
|
||||
using MediaBrowser.Controller.Channels;
|
||||
using MediaBrowser.Controller.Chapters;
|
||||
using MediaBrowser.Controller.ClientEvent;
|
||||
using MediaBrowser.Controller.Collections;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Dlna;
|
||||
|
@ -665,7 +666,7 @@ namespace Emby.Server.Implementations
|
|||
serviceCollection.AddScoped<MediaInfoHelper>();
|
||||
serviceCollection.AddScoped<AudioHelper>();
|
||||
serviceCollection.AddScoped<DynamicHlsHelper>();
|
||||
|
||||
serviceCollection.AddScoped<IClientEventLogger, ClientEventLogger>();
|
||||
serviceCollection.AddSingleton<IDirectoryService, DirectoryService>();
|
||||
}
|
||||
|
||||
|
|
149
Jellyfin.Api/Controllers/ClientLogController.cs
Normal file
149
Jellyfin.Api/Controllers/ClientLogController.cs
Normal file
|
@ -0,0 +1,149 @@
|
|||
using System;
|
||||
using System.Net.Mime;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Api.Attributes;
|
||||
using Jellyfin.Api.Constants;
|
||||
using Jellyfin.Api.Helpers;
|
||||
using Jellyfin.Api.Models.ClientLogDtos;
|
||||
using MediaBrowser.Controller.ClientEvent;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Model.ClientLog;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Jellyfin.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Client log controller.
|
||||
/// </summary>
|
||||
[Authorize(Policy = Policies.DefaultAuthorization)]
|
||||
public class ClientLogController : BaseJellyfinApiController
|
||||
{
|
||||
private const int MaxDocumentSize = 1_000_000;
|
||||
private readonly IClientEventLogger _clientEventLogger;
|
||||
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ClientLogController"/> class.
|
||||
/// </summary>
|
||||
/// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
|
||||
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
||||
public ClientLogController(
|
||||
IClientEventLogger clientEventLogger,
|
||||
IServerConfigurationManager serverConfigurationManager)
|
||||
{
|
||||
_clientEventLogger = clientEventLogger;
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Post event from client.
|
||||
/// </summary>
|
||||
/// <param name="clientLogEventDto">The client log dto.</param>
|
||||
/// <response code="204">Event logged.</response>
|
||||
/// <response code="403">Event logging disabled.</response>
|
||||
/// <returns>Submission status.</returns>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
|
||||
{
|
||||
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
|
||||
Log(clientLogEventDto, userId, clientName, clientVersion, deviceId);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bulk post events from client.
|
||||
/// </summary>
|
||||
/// <param name="clientLogEventDtos">The list of client log dtos.</param>
|
||||
/// <response code="204">All events logged.</response>
|
||||
/// <response code="403">Event logging disabled.</response>
|
||||
/// <returns>Submission status.</returns>
|
||||
[HttpPost("Bulk")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
|
||||
{
|
||||
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
|
||||
foreach (var dto in clientLogEventDtos)
|
||||
{
|
||||
Log(dto, userId, clientName, clientVersion, deviceId);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upload a document.
|
||||
/// </summary>
|
||||
/// <response code="200">Document saved.</response>
|
||||
/// <response code="403">Event logging disabled.</response>
|
||||
/// <response code="413">Upload size too large.</response>
|
||||
/// <returns>Create response.</returns>
|
||||
[HttpPost("Document")]
|
||||
[ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
||||
[AcceptsFile(MediaTypeNames.Text.Plain)]
|
||||
[RequestSizeLimit(MaxDocumentSize)]
|
||||
public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
|
||||
{
|
||||
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
if (Request.ContentLength > MaxDocumentSize)
|
||||
{
|
||||
// Manually validate to return proper status code.
|
||||
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
|
||||
}
|
||||
|
||||
var (clientName, clientVersion, _, _) = GetRequestInformation();
|
||||
var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
|
||||
.ConfigureAwait(false);
|
||||
return Ok(new ClientLogDocumentResponseDto(fileName));
|
||||
}
|
||||
|
||||
private void Log(
|
||||
ClientLogEventDto dto,
|
||||
Guid userId,
|
||||
string clientName,
|
||||
string clientVersion,
|
||||
string deviceId)
|
||||
{
|
||||
_clientEventLogger.Log(new ClientLogEvent(
|
||||
dto.Timestamp,
|
||||
dto.Level,
|
||||
userId,
|
||||
clientName,
|
||||
clientVersion,
|
||||
deviceId,
|
||||
dto.Message));
|
||||
}
|
||||
|
||||
private (string ClientName, string ClientVersion, Guid UserId, string DeviceId) GetRequestInformation()
|
||||
{
|
||||
var clientName = ClaimHelpers.GetClient(HttpContext.User) ?? "unknown-client";
|
||||
var clientVersion = ClaimHelpers.GetIsApiKey(HttpContext.User)
|
||||
? "apikey"
|
||||
: ClaimHelpers.GetVersion(HttpContext.User) ?? "unknown-version";
|
||||
var userId = ClaimHelpers.GetUserId(HttpContext.User) ?? Guid.Empty;
|
||||
var deviceId = ClaimHelpers.GetDeviceId(HttpContext.User) ?? "unknown-device-id";
|
||||
|
||||
return (clientName, clientVersion, userId, deviceId);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
namespace Jellyfin.Api.Models.ClientLogDtos
|
||||
{
|
||||
/// <summary>
|
||||
/// Client log document response dto.
|
||||
/// </summary>
|
||||
public class ClientLogDocumentResponseDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ClientLogDocumentResponseDto"/> class.
|
||||
/// </summary>
|
||||
/// <param name="fileName">The file name.</param>
|
||||
public ClientLogDocumentResponseDto(string fileName)
|
||||
{
|
||||
FileName = fileName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting filename.
|
||||
/// </summary>
|
||||
public string FileName { get; }
|
||||
}
|
||||
}
|
30
Jellyfin.Api/Models/ClientLogDtos/ClientLogEventDto.cs
Normal file
30
Jellyfin.Api/Models/ClientLogDtos/ClientLogEventDto.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Api.Models.ClientLogDtos
|
||||
{
|
||||
/// <summary>
|
||||
/// The client log dto.
|
||||
/// </summary>
|
||||
public class ClientLogEventDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the event timestamp.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log level.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public LogLevel Level { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log message.
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
|
@ -44,6 +44,7 @@
|
|||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Graylog" Version="2.2.2" />
|
||||
<PackageReference Include="Serilog.Sinks.Map" Version="1.0.2" />
|
||||
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.0.7" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ using Emby.Server.Implementations;
|
|||
using Jellyfin.Server.Implementations;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.ClientEvent;
|
||||
using MediaBrowser.Controller.Extensions;
|
||||
using MediaBrowser.Model.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
@ -25,6 +26,7 @@ using Microsoft.Extensions.Logging;
|
|||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Logging;
|
||||
using Serilog.Filters;
|
||||
using SQLitePCL;
|
||||
using ConfigurationExtensions = MediaBrowser.Controller.Extensions.ConfigurationExtensions;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
|
@ -596,22 +598,46 @@ namespace Jellyfin.Server
|
|||
{
|
||||
// Serilog.Log is used by SerilogLoggerFactory when no logger is specified
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithThreadId()
|
||||
.WriteTo.Logger(lc =>
|
||||
lc.ReadFrom.Configuration(configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithThreadId()
|
||||
.Filter.ByExcluding(Matching.FromSource<ClientEventLogger>()))
|
||||
.WriteTo.Logger(lc =>
|
||||
lc.WriteTo.Map(
|
||||
"ClientName",
|
||||
(clientName, wt)
|
||||
=> wt.File(
|
||||
Path.Combine(appPaths.LogDirectoryPath, "log_" + clientName + "_.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
outputTemplate: "{Message:l}{NewLine}{Exception}",
|
||||
encoding: Encoding.UTF8))
|
||||
.Filter.ByIncludingOnly(Matching.FromSource<ClientEventLogger>()))
|
||||
.CreateLogger();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message:lj}{NewLine}{Exception}")
|
||||
.WriteTo.Async(x => x.File(
|
||||
Path.Combine(appPaths.LogDirectoryPath, "log_.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message}{NewLine}{Exception}",
|
||||
encoding: Encoding.UTF8))
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithThreadId()
|
||||
.WriteTo.Logger(lc =>
|
||||
lc.WriteTo.Async(x => x.File(
|
||||
Path.Combine(appPaths.LogDirectoryPath, "log_.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
outputTemplate: "{Message:l}{NewLine}{Exception}",
|
||||
encoding: Encoding.UTF8))
|
||||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] [{ThreadId}] {SourceContext}: {Message:lj}{NewLine}{Exception}")
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithThreadId())
|
||||
.WriteTo.Logger(lc =>
|
||||
lc
|
||||
.WriteTo.Map(
|
||||
"ClientName",
|
||||
(clientName, wt)
|
||||
=> wt.File(
|
||||
Path.Combine(appPaths.LogDirectoryPath, "log_" + clientName + "_.log"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
outputTemplate: "{Message:l}{NewLine}{Exception}",
|
||||
encoding: Encoding.UTF8))
|
||||
.Filter.ByIncludingOnly(Matching.FromSource<ClientEventLogger>()))
|
||||
.CreateLogger();
|
||||
|
||||
Log.Logger.Fatal(ex, "Failed to create/read logger configuration");
|
||||
|
|
55
MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs
Normal file
55
MediaBrowser.Controller/ClientEvent/ClientEventLogger.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
31
MediaBrowser.Controller/ClientEvent/IClientEventLogger.cs
Normal file
31
MediaBrowser.Controller/ClientEvent/IClientEventLogger.cs
Normal file
|
@ -0,0 +1,31 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Model.ClientLog;
|
||||
|
||||
namespace MediaBrowser.Controller.ClientEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// The client event logger.
|
||||
/// </summary>
|
||||
public interface IClientEventLogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs the event from the client.
|
||||
/// </summary>
|
||||
/// <param name="clientLogEvent">The client log event.</param>
|
||||
void Log(ClientLogEvent clientLogEvent);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a file to the log directory.
|
||||
/// </summary>
|
||||
/// <param name="clientName">The client name writing the document.</param>
|
||||
/// <param name="clientVersion">The client version writing the document.</param>
|
||||
/// <param name="fileContents">The file contents to write.</param>
|
||||
/// <returns>The created file name.</returns>
|
||||
Task<string> WriteDocumentAsync(
|
||||
string clientName,
|
||||
string clientVersion,
|
||||
Stream fileContents);
|
||||
}
|
||||
}
|
75
MediaBrowser.Model/ClientLog/ClientLogEvent.cs
Normal file
75
MediaBrowser.Model/ClientLog/ClientLogEvent.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Model.ClientLog
|
||||
{
|
||||
/// <summary>
|
||||
/// The client log event.
|
||||
/// </summary>
|
||||
public class ClientLogEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ClientLogEvent"/> class.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The log timestamp.</param>
|
||||
/// <param name="level">The log level.</param>
|
||||
/// <param name="userId">The user id.</param>
|
||||
/// <param name="clientName">The client name.</param>
|
||||
/// <param name="clientVersion">The client version.</param>
|
||||
/// <param name="deviceId">The device id.</param>
|
||||
/// <param name="message">The message.</param>
|
||||
public ClientLogEvent(
|
||||
DateTime timestamp,
|
||||
LogLevel level,
|
||||
Guid? userId,
|
||||
string clientName,
|
||||
string clientVersion,
|
||||
string deviceId,
|
||||
string message)
|
||||
{
|
||||
Timestamp = timestamp;
|
||||
UserId = userId;
|
||||
ClientName = clientName;
|
||||
ClientVersion = clientVersion;
|
||||
DeviceId = deviceId;
|
||||
Message = message;
|
||||
Level = level;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the event timestamp.
|
||||
/// </summary>
|
||||
public DateTime Timestamp { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the log level.
|
||||
/// </summary>
|
||||
public LogLevel Level { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user id.
|
||||
/// </summary>
|
||||
public Guid? UserId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the client name.
|
||||
/// </summary>
|
||||
public string ClientName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the client version.
|
||||
/// </summary>
|
||||
public string ClientVersion { get; }
|
||||
|
||||
///
|
||||
/// <summary>
|
||||
/// Gets the device id.
|
||||
/// </summary>
|
||||
public string DeviceId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the log message.
|
||||
/// </summary>
|
||||
public string Message { get; }
|
||||
}
|
||||
}
|
|
@ -459,5 +459,10 @@ namespace MediaBrowser.Model.Configuration
|
|||
/// Gets or sets a value indicating whether older plugins should automatically be deleted from the plugin folder.
|
||||
/// </summary>
|
||||
public bool RemoveOldPlugins { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether clients should be allowed to upload logs.
|
||||
/// </summary>
|
||||
public bool AllowClientLogUpload { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user