Fix log dir

This commit is contained in:
Bond_009 2019-01-01 21:34:12 +01:00 committed by Vasily
parent 33889e5352
commit 95a5dd8810
4 changed files with 54 additions and 35 deletions

View File

@ -1,5 +1,4 @@
using System; using System.IO;
using System.IO;
using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Configuration;
namespace Emby.Server.Implementations.AppBase namespace Emby.Server.Implementations.AppBase
@ -13,10 +12,11 @@ namespace Emby.Server.Implementations.AppBase
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class. /// Initializes a new instance of the <see cref="BaseApplicationPaths"/> class.
/// </summary> /// </summary>
protected BaseApplicationPaths(string programDataPath, string appFolderPath) protected BaseApplicationPaths(string programDataPath, string appFolderPath, string logDirectoryPath)
{ {
ProgramDataPath = programDataPath; ProgramDataPath = programDataPath;
ProgramSystemPath = appFolderPath; ProgramSystemPath = appFolderPath;
LogDirectoryPath = logDirectoryPath;
} }
public string ProgramDataPath { get; private set; } public string ProgramDataPath { get; private set; }
@ -106,6 +106,11 @@ namespace Emby.Server.Implementations.AppBase
} }
} }
/// <summary>
/// The _log directory
/// </summary>
private string _logDirectoryPath;
/// <summary> /// <summary>
/// Gets the path to the log directory /// Gets the path to the log directory
/// </summary> /// </summary>
@ -114,7 +119,18 @@ namespace Emby.Server.Implementations.AppBase
{ {
get get
{ {
return Path.Combine(ProgramDataPath, "logs"); if (string.IsNullOrEmpty(_logDirectoryPath))
{
_logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
Directory.CreateDirectory(_logDirectoryPath);
}
return _logDirectoryPath;
}
set
{
_logDirectoryPath = value;
} }
} }

View File

@ -13,8 +13,8 @@ namespace Emby.Server.Implementations
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class. /// Initializes a new instance of the <see cref="BaseApplicationPaths" /> class.
/// </summary> /// </summary>
public ServerApplicationPaths(string programDataPath, string appFolderPath, string applicationResourcesPath) public ServerApplicationPaths(string programDataPath, string appFolderPath, string applicationResourcesPath, string logDirectoryPath = null)
: base(programDataPath, appFolderPath) : base(programDataPath, appFolderPath, logDirectoryPath)
{ {
ApplicationResourcesPath = applicationResourcesPath; ApplicationResourcesPath = applicationResourcesPath;
} }

View File

@ -39,27 +39,23 @@ namespace Jellyfin.Server
public static async Task<int> Main(string[] args) public static async Task<int> Main(string[] args)
{ {
StartupOptions options = new StartupOptions(args); StartupOptions options = new StartupOptions(args);
Version version = Assembly.GetEntryAssembly().GetName().Version;
Assembly entryAssembly = Assembly.GetEntryAssembly();
if (options.ContainsOption("-v") || options.ContainsOption("--version")) if (options.ContainsOption("-v") || options.ContainsOption("--version"))
{ {
Console.WriteLine(entryAssembly.GetName().Version.ToString()); Console.WriteLine(version.ToString());
return 0; return 0;
} }
string dataPath = options.GetOption("-programdata"); ServerApplicationPaths appPaths = createApplicationPaths(options);
string binPath = entryAssembly.Location;
ServerApplicationPaths appPaths = CreateApplicationPaths(binPath, dataPath);
await createLogger(appPaths); await createLogger(appPaths);
_loggerFactory = new SerilogLoggerFactory(); _loggerFactory = new SerilogLoggerFactory();
_logger = _loggerFactory.CreateLogger("Main"); _logger = _loggerFactory.CreateLogger("Main");
AppDomain.CurrentDomain.UnhandledException += (sender, e) 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(); EnvironmentInfo environmentInfo = getEnvironmentInfo();
ApplicationHost.LogEnvironmentInfo(_logger, appPaths, environmentInfo); ApplicationHost.LogEnvironmentInfo(_logger, appPaths, environmentInfo);
@ -106,9 +102,14 @@ namespace Jellyfin.Server
return 0; 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)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
@ -126,28 +127,30 @@ namespace Jellyfin.Server
programDataPath = Path.Combine(programDataPath, "jellyfin"); 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) 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 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 // For some reason the csproj name is used instead of the assembly name
using (Stream rscstr = typeof(Program).Assembly using (Stream rscstr = typeof(Program).Assembly
.GetManifestResourceStream("Jellyfin.Server.Resources.Configuration.logging.json")) .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); await rscstr.CopyToAsync(fstr);
} }
@ -169,7 +172,7 @@ namespace Jellyfin.Server
Serilog.Log.Logger = new LoggerConfiguration() Serilog.Log.Logger = new LoggerConfiguration()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File( .WriteTo.File(
Path.Combine(logDir, "log_.log"), Path.Combine(appPaths.LogDirectoryPath, "log_.log"),
rollingInterval: RollingInterval.Day, rollingInterval: RollingInterval.Day,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}") outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}")
.Enrich.FromLogContext() .Enrich.FromLogContext()
@ -227,6 +230,7 @@ namespace Jellyfin.Server
case PlatformID.Win32NT: case PlatformID.Win32NT:
return MediaBrowser.Model.System.OperatingSystem.Windows; return MediaBrowser.Model.System.OperatingSystem.Windows;
case PlatformID.Unix: case PlatformID.Unix:
default:
{ {
string osDescription = RuntimeInformation.OSDescription; string osDescription = RuntimeInformation.OSDescription;
if (osDescription.Contains("linux", StringComparison.OrdinalIgnoreCase)) if (osDescription.Contains("linux", StringComparison.OrdinalIgnoreCase))
@ -241,9 +245,8 @@ namespace Jellyfin.Server
{ {
return MediaBrowser.Model.System.OperatingSystem.BSD; 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}");
} }
} }

View File

@ -9,12 +9,11 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
using System.Threading; using System.Threading;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.System namespace MediaBrowser.Api.System
{ {
@ -137,11 +136,12 @@ namespace MediaBrowser.Api.System
try 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<FileSystemMetadata>();
} }
var result = files.Select(i => new LogFile var result = files.Select(i => new LogFile