< 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: 31
Coverable lines: 31
Total lines: 82
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%
get_Id()100%210%
get_Name()100%210%
get_PerformOnNewInstall()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    internal class CreateUserLoggingConfigFile : IMigrationRoutine
 16    {
 17        /// <summary>
 18        /// File history for logging.json as existed during this migration creation. The contents for each has been mini
 19        /// </summary>
 020        private readonly List<string> _defaultConfigHistory = new List<string>
 021        {
 022            // 9a6c27947353585391e211aa88b925f81e8cd7b9
 023            @"{""Serilog"":{""MinimumLevel"":{""Default"":""Information"",""Override"":{""Microsoft"":""Warning"",""Syst
 024            // 71bdcd730705a714ee208eaad7290b7c68df3885
 025            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 026            // a44936f97f8afc2817d3491615a7cfe1e31c251c
 027            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 028            // 7af3754a11ad5a4284f107997fb5419a010ce6f3
 029            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 030            // 60691349a11f541958e0b2247c9abc13cb40c9fb
 031            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 032            // 65fe243afbcc4b596cf8726708c1965cd34b5f68
 033            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 034            // 96c9af590494aa8137d5a061aaf1e68feee60b67
 035            @"{""Serilog"":{""MinimumLevel"":""Information"",""WriteTo"":[{""Name"":""Console"",""Args"":{""outputTempla
 036        };
 37
 38        private readonly IApplicationPaths _appPaths;
 39
 40        public CreateUserLoggingConfigFile(IApplicationPaths appPaths)
 41        {
 042            _appPaths = appPaths;
 043        }
 44
 45        /// <inheritdoc/>
 046        public Guid Id => Guid.Parse("{EF103419-8451-40D8-9F34-D1A8E93A1679}");
 47
 48        /// <inheritdoc/>
 049        public string Name => "CreateLoggingConfigHeirarchy";
 50
 51        /// <inheritdoc/>
 052        public bool PerformOnNewInstall => false;
 53
 54        /// <inheritdoc/>
 55        public void Perform()
 56        {
 057            var logDirectory = _appPaths.ConfigurationDirectoryPath;
 058            var existingConfigPath = Path.Combine(logDirectory, "logging.json");
 59
 60            // If the existing logging.json config file is unmodified, then 'reset' it by moving it to 'logging.old.json
 61            // NOTE: This config file has 'reloadOnChange: true', so this change will take effect immediately even thoug
 062            if (File.Exists(existingConfigPath) && ExistingConfigUnmodified(existingConfigPath))
 63            {
 064                File.Move(existingConfigPath, Path.Combine(logDirectory, "logging.old.json"));
 65            }
 066        }
 67
 68        /// <summary>
 69        /// Check if the existing logging.json file has not been modified by the user by comparing it to all the
 70        /// versions in our git history. Until now, the file has never been migrated after first creation so users
 71        /// could have any version from the git history.
 72        /// </summary>
 73        /// <exception cref="IOException"><paramref name="oldConfigPath"/> does not exist or could not be read.</excepti
 74        private bool ExistingConfigUnmodified(string oldConfigPath)
 75        {
 076            var existingConfigJson = JToken.Parse(File.ReadAllText(oldConfigPath));
 077            return _defaultConfigHistory
 078                .Select(JToken.Parse)
 079                .Any(historicalConfigJson => JToken.DeepEquals(existingConfigJson, historicalConfigJson));
 80        }
 81    }
 82}