| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 5 | | |
| | | 6 | | namespace Jellyfin.Server.ServerSetupApp; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc/> |
| | | 9 | | public class StartupLogger : IStartupLogger |
| | | 10 | | { |
| | | 11 | | private readonly StartupLogTopic? _topic; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="logger">The underlying base logger.</param> |
| | | 17 | | public StartupLogger(ILogger logger) |
| | | 18 | | { |
| | 1933 | 19 | | BaseLogger = logger; |
| | 1933 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="logger">The underlying base logger.</param> |
| | | 26 | | /// <param name="topic">The group for this logger.</param> |
| | 1911 | 27 | | internal StartupLogger(ILogger logger, StartupLogTopic? topic) : this(logger) |
| | | 28 | | { |
| | 1911 | 29 | | _topic = topic; |
| | 1911 | 30 | | } |
| | | 31 | | |
| | 1 | 32 | | internal static IStartupLogger Logger { get; set; } = new StartupLogger(NullLogger.Instance); |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | 7707 | 35 | | public StartupLogTopic? Topic => _topic; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or Sets the underlying base logger. |
| | | 39 | | /// </summary> |
| | | 40 | | protected ILogger BaseLogger { get; set; } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | | 43 | | public IStartupLogger BeginGroup(FormattableString logEntry) |
| | | 44 | | { |
| | 945 | 45 | | return new StartupLogger(BaseLogger, AddToTopic(logEntry)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc/> |
| | | 49 | | public IStartupLogger With(ILogger logger) |
| | | 50 | | { |
| | 945 | 51 | | return new StartupLogger(logger, Topic); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc/> |
| | | 55 | | public IStartupLogger<TCategory> With<TCategory>(ILogger logger) |
| | | 56 | | { |
| | 0 | 57 | | return new StartupLogger<TCategory>(logger, Topic); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | | 61 | | public IStartupLogger<TCategory> BeginGroup<TCategory>(FormattableString logEntry) |
| | | 62 | | { |
| | 21 | 63 | | return new StartupLogger<TCategory>(BaseLogger, AddToTopic(logEntry)); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private StartupLogTopic AddToTopic(FormattableString logEntry) |
| | | 67 | | { |
| | 966 | 68 | | var startupEntry = new StartupLogTopic() |
| | 966 | 69 | | { |
| | 966 | 70 | | Content = logEntry.ToString(CultureInfo.InvariantCulture), |
| | 966 | 71 | | DateOfCreation = DateTimeOffset.Now |
| | 966 | 72 | | }; |
| | | 73 | | |
| | 966 | 74 | | if (Topic is null) |
| | | 75 | | { |
| | 63 | 76 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | 903 | 80 | | Topic.Children.Add(startupEntry); |
| | | 81 | | } |
| | | 82 | | |
| | 966 | 83 | | return startupEntry; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | | 87 | | public IDisposable? BeginScope<TState>(TState state) |
| | | 88 | | where TState : notnull |
| | | 89 | | { |
| | 0 | 90 | | return null; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public bool IsEnabled(LogLevel logLevel) |
| | | 95 | | { |
| | 0 | 96 | | return true; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc/> |
| | | 100 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Excepti |
| | | 101 | | { |
| | 2394 | 102 | | if (BaseLogger.IsEnabled(logLevel)) |
| | | 103 | | { |
| | | 104 | | // if enabled allow the base logger also to receive the message |
| | 630 | 105 | | BaseLogger.Log(logLevel, eventId, state, exception, formatter); |
| | | 106 | | } |
| | | 107 | | |
| | 2394 | 108 | | var startupEntry = new StartupLogTopic() |
| | 2394 | 109 | | { |
| | 2394 | 110 | | LogLevel = logLevel, |
| | 2394 | 111 | | Content = formatter(state, exception), |
| | 2394 | 112 | | DateOfCreation = DateTimeOffset.Now |
| | 2394 | 113 | | }; |
| | | 114 | | |
| | 2394 | 115 | | if (Topic is null) |
| | | 116 | | { |
| | 0 | 117 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | 2394 | 121 | | Topic.Children.Add(startupEntry); |
| | | 122 | | } |
| | 2394 | 123 | | } |
| | | 124 | | } |