| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Threading; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Server.ServerSetupApp; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc/> |
| | | 10 | | public class StartupLogger : IStartupLogger |
| | | 11 | | { |
| | 2 | 12 | | private static readonly AsyncLocal<StartupLogTopic?> _ambientTopic = new(); |
| | | 13 | | |
| | | 14 | | private readonly StartupLogTopic? _topic; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="logger">The underlying base logger.</param> |
| | | 20 | | public StartupLogger(ILogger logger) |
| | | 21 | | { |
| | 3627 | 22 | | BaseLogger = logger; |
| | 3627 | 23 | | _topic = _ambientTopic.Value; |
| | 3627 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="StartupLogger"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="logger">The underlying base logger.</param> |
| | | 30 | | /// <param name="topic">The group for this logger.</param> |
| | 3564 | 31 | | internal StartupLogger(ILogger logger, StartupLogTopic? topic) : this(logger) |
| | | 32 | | { |
| | 3564 | 33 | | _topic = topic; |
| | 3564 | 34 | | } |
| | | 35 | | |
| | 2 | 36 | | internal static IStartupLogger Logger { get; set; } = new StartupLogger(NullLogger.Instance); |
| | | 37 | | |
| | | 38 | | /// <inheritdoc/> |
| | 14879 | 39 | | public StartupLogTopic? Topic => _topic; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or Sets the underlying base logger. |
| | | 43 | | /// </summary> |
| | | 44 | | protected ILogger BaseLogger { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Makes <paramref name="topic"/> the topic that loggers created on this execution context attach to. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="topic">The topic to nest newly created loggers under.</param> |
| | | 50 | | /// <returns>A scope that restores the previously ambient topic when disposed.</returns> |
| | | 51 | | internal static IDisposable BeginAmbientTopic(StartupLogTopic? topic) |
| | | 52 | | { |
| | 141 | 53 | | var scope = new AmbientTopicScope(_ambientTopic.Value); |
| | 141 | 54 | | _ambientTopic.Value = topic; |
| | 141 | 55 | | return scope; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc/> |
| | | 59 | | public IStartupLogger BeginGroup(FormattableString logEntry) |
| | | 60 | | { |
| | 1771 | 61 | | return new StartupLogger(BaseLogger, AddToTopic(logEntry)); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc/> |
| | | 65 | | public IStartupLogger With(ILogger logger) |
| | | 66 | | { |
| | 1765 | 67 | | return new StartupLogger(logger, Topic); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | | 71 | | public IStartupLogger<TCategory> With<TCategory>(ILogger logger) |
| | | 72 | | { |
| | 0 | 73 | | return new StartupLogger<TCategory>(logger, Topic); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | | 77 | | public IStartupLogger<TCategory> BeginGroup<TCategory>(FormattableString logEntry) |
| | | 78 | | { |
| | 28 | 79 | | return new StartupLogger<TCategory>(BaseLogger, AddToTopic(logEntry)); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private StartupLogTopic AddToTopic(FormattableString logEntry) |
| | | 83 | | { |
| | 1799 | 84 | | var startupEntry = new StartupLogTopic() |
| | 1799 | 85 | | { |
| | 1799 | 86 | | Content = logEntry.ToString(CultureInfo.InvariantCulture), |
| | 1799 | 87 | | DateOfCreation = DateTimeOffset.Now |
| | 1799 | 88 | | }; |
| | | 89 | | |
| | 1799 | 90 | | if (Topic is null) |
| | | 91 | | { |
| | 97 | 92 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 1702 | 96 | | Topic.Children.Add(startupEntry); |
| | | 97 | | } |
| | | 98 | | |
| | 1799 | 99 | | return startupEntry; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc/> |
| | | 103 | | public IDisposable? BeginScope<TState>(TState state) |
| | | 104 | | where TState : notnull |
| | | 105 | | { |
| | 0 | 106 | | return null; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc/> |
| | | 110 | | public bool IsEnabled(LogLevel logLevel) |
| | | 111 | | { |
| | 0 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc/> |
| | | 116 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Excepti |
| | | 117 | | { |
| | 4736 | 118 | | if (BaseLogger.IsEnabled(logLevel)) |
| | | 119 | | { |
| | | 120 | | // if enabled allow the base logger also to receive the message |
| | 1271 | 121 | | BaseLogger.Log(logLevel, eventId, state, exception, formatter); |
| | | 122 | | } |
| | | 123 | | |
| | 4736 | 124 | | var startupEntry = new StartupLogTopic() |
| | 4736 | 125 | | { |
| | 4736 | 126 | | LogLevel = logLevel, |
| | 4736 | 127 | | Content = formatter(state, exception), |
| | 4736 | 128 | | DateOfCreation = DateTimeOffset.Now |
| | 4736 | 129 | | }; |
| | | 130 | | |
| | 4736 | 131 | | if (Topic is null) |
| | | 132 | | { |
| | 14 | 133 | | SetupServer.LogQueue?.Enqueue(startupEntry); |
| | | 134 | | } |
| | | 135 | | else |
| | | 136 | | { |
| | 4722 | 137 | | Topic.Children.Add(startupEntry); |
| | | 138 | | } |
| | 4722 | 139 | | } |
| | | 140 | | |
| | | 141 | | private sealed class AmbientTopicScope : IDisposable |
| | | 142 | | { |
| | | 143 | | private readonly StartupLogTopic? _previous; |
| | | 144 | | |
| | | 145 | | public AmbientTopicScope(StartupLogTopic? previous) |
| | | 146 | | { |
| | 141 | 147 | | _previous = previous; |
| | 141 | 148 | | } |
| | | 149 | | |
| | | 150 | | public void Dispose() |
| | | 151 | | { |
| | 141 | 152 | | _ambientTopic.Value = _previous; |
| | 141 | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |