aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/ServerSetupApp/StartupLogger.cs
blob: 2b86dc0c1a63f03815642f3e61120cc187ea0ec1 (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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Jellyfin.Server.Migrations.Routines;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.ServerSetupApp;

/// <inheritdoc/>
public class StartupLogger : IStartupLogger
{
    private readonly SetupServer.StartupLogEntry? _groupEntry;

    /// <summary>
    /// Initializes a new instance of the <see cref="StartupLogger"/> class.
    /// </summary>
    public StartupLogger()
    {
        Loggers = [];
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="StartupLogger"/> class.
    /// </summary>
    private StartupLogger(SetupServer.StartupLogEntry? groupEntry) : this()
    {
        _groupEntry = groupEntry;
    }

    internal static IStartupLogger Logger { get; } = new StartupLogger();

    private List<ILogger> Loggers { get; set; }

    /// <inheritdoc/>
    public IStartupLogger BeginGroup(FormattableString logEntry)
    {
        var startupEntry = new SetupServer.StartupLogEntry()
        {
            Content = logEntry.ToString(CultureInfo.InvariantCulture),
            DateOfCreation = DateTimeOffset.Now
        };

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

        return new StartupLogger(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)
    {
        foreach (var item in Loggers.Where(e => e.IsEnabled(logLevel)))
        {
            item.Log(logLevel, eventId, state, exception, formatter);
        }

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

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

    /// <inheritdoc/>
    public IStartupLogger With(ILogger logger)
    {
        return new StartupLogger(_groupEntry)
        {
            Loggers = [.. Loggers, logger]
        };
    }
}