< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.CreateUserLoggingConfigFile
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/CreateUserLoggingConfigFile.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 76
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Perform()0%2040%
ExistingConfigUnmodified(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/CreateUserLoggingConfigFile.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using MediaBrowser.Common.Configuration;
 6using Newtonsoft.Json.Linq;
 7
 8namespace Jellyfin.Server.Migrations.Routines
 9{
 10    /// <summary>
 11    /// Migration to initialize the user logging configuration file "logging.user.json".
 12    /// If the deprecated logging.json file exists and has a custom config, it will be used as logging.user.json,
 13    /// otherwise a blank file will be created.
 14    /// </summary>
 15#pragma warning disable CS0618 // Type or member is obsolete
 16    [JellyfinMigration("2025-04-20T06:00:00", nameof(CreateUserLoggingConfigFile), "EF103419-8451-40D8-9F34-D1A8E93A1679
 17    internal class CreateUserLoggingConfigFile : IMigrationRoutine
 18#pragma warning restore CS0618 // Type or member is obsolete
 19    {
 20        /// <summary>
 21        /// File history for logging.json as existed during this migration creation. The contents for each has been mini
 22        /// </summary>
 023        private readonly List<string> _defaultConfigHistory = new List<string>
 024        {
 025            // 9a6c27947353585391e211aa88b925f81e8cd7b9
 026            @"{""Serilog"":{""MinimumLevel"":{""Default"":""Information"",""Override"":{""Microsoft"":""Warning"",""Syst
 027            // 71bdcd730705a714ee208eaad7290b7c68df3885
 028            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 029            // a44936f97f8afc2817d3491615a7cfe1e31c251c
 030            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 031            // 7af3754a11ad5a4284f107997fb5419a010ce6f3
 032            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 033            // 60691349a11f541958e0b2247c9abc13cb40c9fb
 034            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 035            // 65fe243afbcc4b596cf8726708c1965cd34b5f68
 036            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 037            // 96c9af590494aa8137d5a061aaf1e68feee60b67
 038            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 039        };
 40
 41        private readonly IApplicationPaths _appPaths;
 42
 43        public CreateUserLoggingConfigFile(IApplicationPaths appPaths)
 44        {
 045            _appPaths = appPaths;
 046        }
 47
 48        /// <inheritdoc/>
 49        public void Perform()
 50        {
 051            var logDirectory = _appPaths.ConfigurationDirectoryPath;
 052            var existingConfigPath = Path.Combine(logDirectory, "logging.json");
 53
 54            // If the existing logging.json config file is unmodified, then 'reset' it by moving it to 'logging.old.json
 55            // NOTE: This config file has 'reloadOnChange: true', so this change will take effect immediately even thoug
 056            if (File.Exists(existingConfigPath) && ExistingConfigUnmodified(existingConfigPath))
 57            {
 058                File.Move(existingConfigPath, Path.Combine(logDirectory, "logging.old.json"));
 59            }
 060        }
 61
 62        /// <summary>
 63        /// Check if the existing logging.json file has not been modified by the user by comparing it to all the
 64        /// versions in our git history. Until now, the file has never been migrated after first creation so users
 65        /// could have any version from the git history.
 66        /// </summary>
 67        /// <exception cref="IOException"><paramref name="oldConfigPath"/> does not exist or could not be read.</excepti
 68        private bool ExistingConfigUnmodified(string oldConfigPath)
 69        {
 070            var existingConfigJson = JToken.Parse(File.ReadAllText(oldConfigPath));
 071            return _defaultConfigHistory
 072                .Select(JToken.Parse)
 073                .Any(historicalConfigJson => JToken.DeepEquals(existingConfigJson, historicalConfigJson));
 74        }
 75    }
 76}