aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/ServerSetupApp/StartupLogger.cs
blob: b72b0c0eabf365fe5943687f7b84b3098fb243a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Globalization;
using System.Threading;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Jellyfin.Server.ServerSetupApp;

/// <inheritdoc/>
public class StartupLogger : IStartupLogger
{
    private static readonly AsyncLocal<StartupLogTopic?> _ambientTopic = new();

    private readonly StartupLogTopic? _topic;

    /// <summary>
    /// Initializes a new instance of the <see cref="StartupLogger"/> class.
    /// </summary>
    /// <param name="logger">The underlying base logger.</param>
    public StartupLogger(ILogger logger)
    {
        BaseLogger = logger;
        _topic = _ambientTopic.Value;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="StartupLogger"/> class.
    /// </summary>
    /// <param name="logger">The underlying base logger.</param>
    /// <param name="topic">The group for this logger.</param>
    internal StartupLogger(ILogger logger, StartupLogTopic? topic) : this(logger)
    {
        _topic = topic;
    }

    internal static IStartupLogger Logger { get; set; } = new StartupLogger(NullLogger.Instance);

    /// <inheritdoc/>
    public StartupLogTopic? Topic => _topic;

    /// <summary>
    /// Gets or Sets the underlying base logger.
    /// </summary>
    protected ILogger BaseLogger { get; set; }

    /// <summary>
    /// Makes <paramref name="topic"/> the topic that loggers created on this execution context attach to.
    /// </summary>
    /// <param name="topic">The topic to nest newly created loggers under.</param>
    /// <returns>A scope that restores the previously ambient topic when disposed.</returns>
    internal static IDisposable BeginAmbientTopic(StartupLogTopic? topic)
    {
        var scope = new AmbientTopicScope(_ambientTopic.Value);
        _ambientTopic.Value = topic;
        return scope;
    }

    /// <inheritdoc/>
    public IStartupLogger BeginGroup(FormattableString logEntry)
    {
        return new StartupLogger(BaseLogger, AddToTopic(logEntry));
    }

    /// <inheritdoc/>
    public IStartupLogger With(ILogger logger)
    {
        return new StartupLogger(logger, Topic);
    }

    /// <inheritdoc/>
    public IStartupLogger<TCategory> With<TCategory>(ILogger logger)
    {
        return new StartupLogger<TCategory>(logger, Topic);
    }

    /// <inheritdoc/>
    public IStartupLogger<TCategory> BeginGroup<TCategory>(FormattableString logEntry)
    {
        return new StartupLogger<TCategory>(BaseLogger, AddToTopic(logEntry));
    }

    private StartupLogTopic AddToTopic(FormattableString logEntry)
    {
        var startupEntry = new StartupLogTopic()
        {
            Content = logEntry.ToString(CultureInfo.InvariantCulture),
            DateOfCreation = DateTimeOffset.Now
        };

        if (Topic is null)
        {
            SetupServer.LogQueue?.Enqueue(startupEntry);
        }
        else
        {
            Topic.Children.Add(startupEntry);
        }

        return startupEntry;
    }

    /// <inheritdoc/>
    public IDisposable? BeginScope<TState>(TState state)
        where TState : notnull
    {
        return null;
    }

    /// <inheritdoc/>
    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    /// <inheritdoc/>
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
    {
        if (BaseLogger.IsEnabled(logLevel))
        {
            // if enabled allow the base logger also to receive the message
            BaseLogger.Log(logLevel, eventId, state, exception, formatter);
        }

        var startupEntry = new StartupLogTopic()
        {
            LogLevel = logLevel,
            Content = formatter(state, exception),
            DateOfCreation = DateTimeOffset.Now
        };

        if (Topic is null)
        {
            SetupServer.LogQueue?.Enqueue(startupEntry);
        }
        else
        {
            Topic.Children.Add(startupEntry);
        }
    }

    private sealed class AmbientTopicScope : IDisposable
    {
        private readonly StartupLogTopic? _previous;

        public AmbientTopicScope(StartupLogTopic? previous)
        {
            _previous = previous;
        }

        public void Dispose()
        {
            _ambientTopic.Value = _previous;
        }
    }
}