From 95a5dd881076f6649f4b5dc5a1897df98dc3c008 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Tue, 1 Jan 2019 21:34:12 +0100 Subject: [PATCH] Fix log dir --- .../AppBase/BaseApplicationPaths.cs | 24 +++++++-- .../ServerApplicationPaths.cs | 4 +- Jellyfin.Server/Program.cs | 51 ++++++++++--------- MediaBrowser.Api/System/SystemService.cs | 10 ++-- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs b/Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs index 52e421374b..76d0076a66 100644 --- a/Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs +++ b/Emby.Server.Implementations/AppBase/BaseApplicationPaths.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using MediaBrowser.Common.Configuration; namespace Emby.Server.Implementations.AppBase @@ -13,10 +12,11 @@ namespace Emby.Server.Implementations.AppBase /// /// Initializes a new instance of the class. /// - protected BaseApplicationPaths(string programDataPath, string appFolderPath) + protected BaseApplicationPaths(string programDataPath, string appFolderPath, string logDirectoryPath) { ProgramDataPath = programDataPath; ProgramSystemPath = appFolderPath; + LogDirectoryPath = logDirectoryPath; } public string ProgramDataPath { get; private set; } @@ -106,6 +106,11 @@ namespace Emby.Server.Implementations.AppBase } } + /// + /// The _log directory + /// + private string _logDirectoryPath; + /// /// Gets the path to the log directory /// @@ -114,7 +119,18 @@ namespace Emby.Server.Implementations.AppBase { get { - return Path.Combine(ProgramDataPath, "logs"); + if (string.IsNullOrEmpty(_logDirectoryPath)) + { + _logDirectoryPath = Path.Combine(ProgramDataPath, "logs"); + + Directory.CreateDirectory(_logDirectoryPath); + } + + return _logDirectoryPath; + } + set + { + _logDirectoryPath = value; } } diff --git a/Emby.Server.Implementations/ServerApplicationPaths.cs b/Emby.Server.Implementations/ServerApplicationPaths.cs index 1686a548bc..f5986f9433 100644 --- a/Emby.Server.Implementations/ServerApplicationPaths.cs +++ b/Emby.Server.Implementations/ServerApplicationPaths.cs @@ -13,8 +13,8 @@ namespace Emby.Server.Implementations /// /// Initializes a new instance of the class. /// - public ServerApplicationPaths(string programDataPath, string appFolderPath, string applicationResourcesPath) - : base(programDataPath, appFolderPath) + public ServerApplicationPaths(string programDataPath, string appFolderPath, string applicationResourcesPath, string logDirectoryPath = null) + : base(programDataPath, appFolderPath, logDirectoryPath) { ApplicationResourcesPath = applicationResourcesPath; } diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index dd5e427df2..aef22e020f 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -39,27 +39,23 @@ namespace Jellyfin.Server public static async Task Main(string[] args) { StartupOptions options = new StartupOptions(args); - - Assembly entryAssembly = Assembly.GetEntryAssembly(); + Version version = Assembly.GetEntryAssembly().GetName().Version; if (options.ContainsOption("-v") || options.ContainsOption("--version")) { - Console.WriteLine(entryAssembly.GetName().Version.ToString()); + Console.WriteLine(version.ToString()); return 0; } - string dataPath = options.GetOption("-programdata"); - string binPath = entryAssembly.Location; - ServerApplicationPaths appPaths = CreateApplicationPaths(binPath, dataPath); - + ServerApplicationPaths appPaths = createApplicationPaths(options); await createLogger(appPaths); _loggerFactory = new SerilogLoggerFactory(); _logger = _loggerFactory.CreateLogger("Main"); AppDomain.CurrentDomain.UnhandledException += (sender, e) - => _logger.LogCritical((Exception)e.ExceptionObject, "Unhandled Exception");; + => _logger.LogCritical((Exception)e.ExceptionObject, "Unhandled Exception"); - _logger.LogInformation("Jellyfin version: {Version}", entryAssembly.GetName().Version); + _logger.LogInformation("Jellyfin version: {Version}", version); EnvironmentInfo environmentInfo = getEnvironmentInfo(); ApplicationHost.LogEnvironmentInfo(_logger, appPaths, environmentInfo); @@ -106,9 +102,14 @@ namespace Jellyfin.Server return 0; } - private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath) + private static ServerApplicationPaths createApplicationPaths(StartupOptions options) { - if (string.IsNullOrEmpty(programDataPath)) + string programDataPath; + if (options.ContainsOption("-programdata")) + { + programDataPath = options.GetOption("-programdata"); + } + else { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { @@ -126,28 +127,30 @@ namespace Jellyfin.Server programDataPath = Path.Combine(programDataPath, "jellyfin"); } - string appFolderPath = Path.GetDirectoryName(applicationPath); + string logDir = Environment.GetEnvironmentVariable("JELLYFIN_LOG_DIR"); + if (string.IsNullOrEmpty(logDir)){ + logDir = Path.Combine(programDataPath, "logs"); + // $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager + Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", logDir); + } - return new ServerApplicationPaths(programDataPath, appFolderPath, appFolderPath); + string appPath = Assembly.GetEntryAssembly().Location; + + return new ServerApplicationPaths(programDataPath, appPath, appPath, logDir); } private static async Task createLogger(IApplicationPaths appPaths) { - string logDir = Environment.GetEnvironmentVariable("JELLYFIN_LOG_DIR"); - if (string.IsNullOrEmpty(logDir)){ - logDir = Path.Combine(appPaths.ProgramDataPath, "logs"); - Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", logDir); - } try { - string path = Path.Combine(appPaths.ConfigurationDirectoryPath, "logging.json"); + string configPath = Path.Combine(appPaths.ConfigurationDirectoryPath, "logging.json"); - if (!File.Exists(path)) + if (!File.Exists(configPath)) { // For some reason the csproj name is used instead of the assembly name using (Stream rscstr = typeof(Program).Assembly .GetManifestResourceStream("Jellyfin.Server.Resources.Configuration.logging.json")) - using (Stream fstr = File.Open(path, FileMode.CreateNew)) + using (Stream fstr = File.Open(configPath, FileMode.CreateNew)) { await rscstr.CopyToAsync(fstr); } @@ -169,7 +172,7 @@ namespace Jellyfin.Server Serilog.Log.Logger = new LoggerConfiguration() .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.File( - Path.Combine(logDir, "log_.log"), + Path.Combine(appPaths.LogDirectoryPath, "log_.log"), rollingInterval: RollingInterval.Day, outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}") .Enrich.FromLogContext() @@ -227,6 +230,7 @@ namespace Jellyfin.Server case PlatformID.Win32NT: return MediaBrowser.Model.System.OperatingSystem.Windows; case PlatformID.Unix: + default: { string osDescription = RuntimeInformation.OSDescription; if (osDescription.Contains("linux", StringComparison.OrdinalIgnoreCase)) @@ -241,9 +245,8 @@ namespace Jellyfin.Server { return MediaBrowser.Model.System.OperatingSystem.BSD; } - throw new Exception($"Can't resolve OS with description: {Environment.OSVersion.Platform}"); + throw new Exception($"Can't resolve OS with description: '{osDescription}'"); } - default: throw new Exception($"Can't resolve OS with description: {Environment.OSVersion.Platform}"); } } diff --git a/MediaBrowser.Api/System/SystemService.cs b/MediaBrowser.Api/System/SystemService.cs index d2880f735a..52e14dbcc9 100644 --- a/MediaBrowser.Api/System/SystemService.cs +++ b/MediaBrowser.Api/System/SystemService.cs @@ -9,12 +9,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; - -using MediaBrowser.Controller.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Net; using MediaBrowser.Model.Services; using System.Threading; +using Microsoft.Extensions.Logging; namespace MediaBrowser.Api.System { @@ -137,11 +136,12 @@ namespace MediaBrowser.Api.System try { - files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt" }, true, false); + files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false); } - catch (IOException) + catch (IOException ex) { - files = new FileSystemMetadata[] { }; + Logger.LogError(ex, "Error getting logs"); + files = Enumerable.Empty(); } var result = files.Select(i => new LogFile