| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Linq; |
| | 5 | | using Jellyfin.Server.Migrations.Routines; |
| | 6 | | using Microsoft.Extensions.Logging; |
| | 7 | |
|
| | 8 | | namespace Jellyfin.Server.ServerSetupApp; |
| | 9 | |
|
| | 10 | | /// <inheritdoc/> |
| | 11 | | public class StartupLogger : IStartupLogger |
| | 12 | | { |
| | 13 | | private readonly SetupServer.StartupLogEntry? _groupEntry; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | 17 | | /// </summary> |
| | 18 | | public StartupLogger() |
| | 19 | | { |
| 1702 | 20 | | Loggers = []; |
| 1702 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | 25 | | /// </summary> |
| 1701 | 26 | | private StartupLogger(SetupServer.StartupLogEntry? groupEntry) : this() |
| | 27 | | { |
| 1701 | 28 | | _groupEntry = groupEntry; |
| 1701 | 29 | | } |
| | 30 | |
|
| 1 | 31 | | internal static IStartupLogger Logger { get; } = new StartupLogger(); |
| | 32 | |
|
| | 33 | | private List<ILogger> Loggers { get; set; } |
| | 34 | |
|
| | 35 | | /// <inheritdoc/> |
| | 36 | | public IStartupLogger BeginGroup(FormattableString logEntry) |
| | 37 | | { |
| 861 | 38 | | var startupEntry = new SetupServer.StartupLogEntry() |
| 861 | 39 | | { |
| 861 | 40 | | Content = logEntry.ToString(CultureInfo.InvariantCulture), |
| 861 | 41 | | DateOfCreation = DateTimeOffset.Now |
| 861 | 42 | | }; |
| | 43 | |
|
| 861 | 44 | | if (_groupEntry is null) |
| | 45 | | { |
| 21 | 46 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 840 | 50 | | _groupEntry.Children.Add(startupEntry); |
| | 51 | | } |
| | 52 | |
|
| 861 | 53 | | return new StartupLogger(startupEntry); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | /// <inheritdoc/> |
| | 57 | | public IDisposable? BeginScope<TState>(TState state) |
| | 58 | | where TState : notnull |
| | 59 | | { |
| 0 | 60 | | return null; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <inheritdoc/> |
| | 64 | | public bool IsEnabled(LogLevel logLevel) |
| | 65 | | { |
| 0 | 66 | | return true; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <inheritdoc/> |
| | 70 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Excepti |
| | 71 | | { |
| 4200 | 72 | | foreach (var item in Loggers.Where(e => e.IsEnabled(logLevel))) |
| | 73 | | { |
| 0 | 74 | | item.Log(logLevel, eventId, state, exception, formatter); |
| | 75 | | } |
| | 76 | |
|
| 2100 | 77 | | var startupEntry = new SetupServer.StartupLogEntry() |
| 2100 | 78 | | { |
| 2100 | 79 | | LogLevel = logLevel, |
| 2100 | 80 | | Content = formatter(state, exception), |
| 2100 | 81 | | DateOfCreation = DateTimeOffset.Now |
| 2100 | 82 | | }; |
| | 83 | |
|
| 2100 | 84 | | if (_groupEntry is null) |
| | 85 | | { |
| 0 | 86 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | 87 | | } |
| | 88 | | else |
| | 89 | | { |
| 2100 | 90 | | _groupEntry.Children.Add(startupEntry); |
| | 91 | | } |
| 2100 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <inheritdoc/> |
| | 95 | | public IStartupLogger With(ILogger logger) |
| | 96 | | { |
| 840 | 97 | | return new StartupLogger(_groupEntry) |
| 840 | 98 | | { |
| 840 | 99 | | Loggers = [.. Loggers, logger] |
| 840 | 100 | | }; |
| | 101 | | } |
| | 102 | | } |