| | 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 | | { |
| 1807 | 19 | | BaseLogger = logger; |
| 1807 | 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> |
| 1785 | 27 | | internal StartupLogger(ILogger logger, StartupLogTopic? topic) : this(logger) |
| | 28 | | { |
| 1785 | 29 | | _topic = topic; |
| 1785 | 30 | | } |
| | 31 | |
|
| 1 | 32 | | internal static IStartupLogger Logger { get; set; } = new StartupLogger(NullLogger.Instance); |
| | 33 | |
|
| | 34 | | /// <inheritdoc/> |
| 7182 | 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 | | { |
| 882 | 45 | | return new StartupLogger(BaseLogger, AddToTopic(logEntry)); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | /// <inheritdoc/> |
| | 49 | | public IStartupLogger With(ILogger logger) |
| | 50 | | { |
| 882 | 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 | | { |
| 903 | 68 | | var startupEntry = new StartupLogTopic() |
| 903 | 69 | | { |
| 903 | 70 | | Content = logEntry.ToString(CultureInfo.InvariantCulture), |
| 903 | 71 | | DateOfCreation = DateTimeOffset.Now |
| 903 | 72 | | }; |
| | 73 | |
|
| 903 | 74 | | if (Topic is null) |
| | 75 | | { |
| 63 | 76 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 840 | 80 | | Topic.Children.Add(startupEntry); |
| | 81 | | } |
| | 82 | |
|
| 903 | 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 | | { |
| 2226 | 102 | | if (BaseLogger.IsEnabled(logLevel)) |
| | 103 | | { |
| | 104 | | // if enabled allow the base logger also to receive the message |
| 588 | 105 | | BaseLogger.Log(logLevel, eventId, state, exception, formatter); |
| | 106 | | } |
| | 107 | |
|
| 2226 | 108 | | var startupEntry = new StartupLogTopic() |
| 2226 | 109 | | { |
| 2226 | 110 | | LogLevel = logLevel, |
| 2226 | 111 | | Content = formatter(state, exception), |
| 2226 | 112 | | DateOfCreation = DateTimeOffset.Now |
| 2226 | 113 | | }; |
| | 114 | |
|
| 2226 | 115 | | if (Topic is null) |
| | 116 | | { |
| 0 | 117 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | 118 | | } |
| | 119 | | else |
| | 120 | | { |
| 2226 | 121 | | Topic.Children.Add(startupEntry); |
| | 122 | | } |
| 2226 | 123 | | } |
| | 124 | | } |