diff --git a/MediaBrowser.Server.Mono/EmbyServer.csproj b/MediaBrowser.Server.Mono/EmbyServer.csproj index 038a26f9d1..da7883e6bf 100644 --- a/MediaBrowser.Server.Mono/EmbyServer.csproj +++ b/MediaBrowser.Server.Mono/EmbyServer.csproj @@ -17,9 +17,13 @@ + - + + + + @@ -50,6 +54,10 @@ + + + + diff --git a/MediaBrowser.Server.Mono/Program.cs b/MediaBrowser.Server.Mono/Program.cs index 0630df431a..1e47322f1d 100644 --- a/MediaBrowser.Server.Mono/Program.cs +++ b/MediaBrowser.Server.Mono/Program.cs @@ -23,6 +23,7 @@ using X509Certificate = System.Security.Cryptography.X509Certificates.X509Certif using System.Threading; using InteropServices = System.Runtime.InteropServices; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Configuration; using ILogger = Microsoft.Extensions.Logging.ILogger; using Serilog; using Serilog.AspNetCore; @@ -53,12 +54,9 @@ namespace MediaBrowser.Server.Mono var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath); _appPaths = appPaths; - using (var loggerFactory = new SerilogLoggerFactory( - new LoggerConfiguration() - .Enrich.FromLogContext() - .WriteTo.Console() - .CreateLogger() - , true)) + createLogger(); + + using (var loggerFactory = new SerilogLoggerFactory()) { _loggerFactory = loggerFactory; @@ -156,6 +154,47 @@ namespace MediaBrowser.Server.Mono } } + private static async Task createLogger() + { + try + { + string path = Path.Combine(_appPaths.ProgramDataPath, "logging.json"); + + if (!File.Exists(path)) + { + var assembly = typeof(MainClass).Assembly; + // For some reason the csproj name is used instead of the assembly name + var resourcePath = "EmbyServer.Resources.Configuration.logging.json"; + using (Stream rscstr = assembly.GetManifestResourceStream(resourcePath)) + using (Stream fstr = File.Open(path, FileMode.CreateNew)) + { + await rscstr.CopyToAsync(fstr); + } + } + var configuration = new ConfigurationBuilder() + .AddJsonFile(path) + .Build(); + + Serilog.Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .Enrich.FromLogContext() + .CreateLogger(); + } + catch (Exception ex) + { + Serilog.Log.Logger = new LoggerConfiguration() + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}") + .WriteTo.File( + Path.Combine(AppContext.BaseDirectory, "logs", "log_.log"), + rollingInterval: RollingInterval.Day, + outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}") + .Enrich.FromLogContext() + .CreateLogger(); + + Serilog.Log.Logger.Fatal(ex, "Failed to read logger config"); + } + } + private static MonoEnvironmentInfo GetEnvironmentInfo() { var info = new MonoEnvironmentInfo(); diff --git a/MediaBrowser.Server.Mono/Resources/Configuration/logging.json b/MediaBrowser.Server.Mono/Resources/Configuration/logging.json new file mode 100644 index 0000000000..68b57b2761 --- /dev/null +++ b/MediaBrowser.Server.Mono/Resources/Configuration/logging.json @@ -0,0 +1,19 @@ +{ + "Serilog": { + "MinimumLevel": "Information", + "WriteTo": [ + { "Name": "Console", + "Args": { + "outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message}{NewLine}{Exception}" + } + }, + { "Name": "File", + "Args": { + "path": "logs//log_.log", + "rollingInterval": "Day", + "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message}{NewLine}{Exception}" + } + } + ] + } +}