mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-04 03:27:14 -05:00 
			
		
		
		
	Host: Using serilog instead of microsoft logging and adding file logs
This commit is contained in:
		
							parent
							
								
									42469bfa67
								
							
						
					
					
						commit
						334a265e19
					
				@ -78,6 +78,7 @@ namespace Kyoo.Host.Windows
 | 
				
			|||||||
			{
 | 
								{
 | 
				
			||||||
				_options = options;
 | 
									_options = options;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									AppDomain.CurrentDomain.ProcessExit += (_, _) => Dispose();
 | 
				
			||||||
				Application.ApplicationExit += (_, _) => Dispose();
 | 
									Application.ApplicationExit += (_, _) => Dispose();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				_icon = new NotifyIcon();
 | 
									_icon = new NotifyIcon();
 | 
				
			||||||
@ -111,8 +112,8 @@ namespace Kyoo.Host.Windows
 | 
				
			|||||||
			/// <inheritdoc />
 | 
								/// <inheritdoc />
 | 
				
			||||||
			protected override void Dispose(bool disposing)
 | 
								protected override void Dispose(bool disposing)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				base.Dispose(disposing);
 | 
					 | 
				
			||||||
				_icon.Visible = false;
 | 
									_icon.Visible = false;
 | 
				
			||||||
 | 
									base.Dispose(disposing);
 | 
				
			||||||
				_icon.Dispose();
 | 
									_icon.Dispose();
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ using Microsoft.AspNetCore.StaticFiles;
 | 
				
			|||||||
using Microsoft.Extensions.Configuration;
 | 
					using Microsoft.Extensions.Configuration;
 | 
				
			||||||
using Microsoft.Extensions.DependencyInjection;
 | 
					using Microsoft.Extensions.DependencyInjection;
 | 
				
			||||||
using Microsoft.Extensions.Hosting;
 | 
					using Microsoft.Extensions.Hosting;
 | 
				
			||||||
 | 
					using Serilog;
 | 
				
			||||||
using IMetadataProvider = Kyoo.Abstractions.Controllers.IMetadataProvider;
 | 
					using IMetadataProvider = Kyoo.Abstractions.Controllers.IMetadataProvider;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Kyoo
 | 
					namespace Kyoo
 | 
				
			||||||
@ -151,6 +152,7 @@ namespace Kyoo
 | 
				
			|||||||
					app.UseHsts();
 | 
										app.UseHsts();
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}, SA.Before),
 | 
								}, SA.Before),
 | 
				
			||||||
 | 
								SA.New<IApplicationBuilder>(app => app.UseSerilogRequestLogging(), SA.Before),
 | 
				
			||||||
			SA.New<IApplicationBuilder>(app => app.UseResponseCompression(), SA.Routing + 1),
 | 
								SA.New<IApplicationBuilder>(app => app.UseResponseCompression(), SA.Routing + 1),
 | 
				
			||||||
			SA.New<IApplicationBuilder>(app => app.UseRouting(), SA.Routing),
 | 
								SA.New<IApplicationBuilder>(app => app.UseRouting(), SA.Routing),
 | 
				
			||||||
			SA.New<IApplicationBuilder>(app => app.UseEndpoints(x => x.MapControllers()), SA.Endpoint)
 | 
								SA.New<IApplicationBuilder>(app => app.UseEndpoints(x => x.MapControllers()), SA.Endpoint)
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,11 @@
 | 
				
			|||||||
	</PropertyGroup>
 | 
						</PropertyGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	<ItemGroup>
 | 
						<ItemGroup>
 | 
				
			||||||
 | 
							<PackageReference Include="Serilog" Version="2.10.0" />
 | 
				
			||||||
 | 
							<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
 | 
				
			||||||
 | 
							<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
 | 
				
			||||||
 | 
							<PackageReference Include="Serilog.Expressions" Version="3.2.0" />
 | 
				
			||||||
 | 
							<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
 | 
				
			||||||
		<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
 | 
							<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
 | 
				
			||||||
		<PackageReference Include="Autofac" Version="6.2.0" />
 | 
							<PackageReference Include="Autofac" Version="6.2.0" />
 | 
				
			||||||
		<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
 | 
							<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
 | 
				
			||||||
@ -44,8 +49,7 @@
 | 
				
			|||||||
		<ProjectReference Include="../Kyoo.WebApp/Kyoo.WebApp.csproj" />
 | 
							<ProjectReference Include="../Kyoo.WebApp/Kyoo.WebApp.csproj" />
 | 
				
			||||||
	</ItemGroup>
 | 
						</ItemGroup>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	<Target Name="BuildTranscoder" BeforeTargets="BeforeBuild"
 | 
						<Target Name="BuildTranscoder" BeforeTargets="BeforeBuild" Condition="'$(SkipTranscoder)' != 'true' And !Exists('$(TranscoderRoot)/build/$(TranscoderBinary)')">
 | 
				
			||||||
			Condition="'$(SkipTranscoder)' != 'true' And !Exists('$(TranscoderRoot)/build/$(TranscoderBinary)')">
 | 
					 | 
				
			||||||
		<Exec WorkingDirectory="$(TranscoderRoot)" Condition="'$(IsWindows)' != 'true'" Command="mkdir -p build %26%26 cd build %26%26 cmake .. %26%26 make -j" />
 | 
							<Exec WorkingDirectory="$(TranscoderRoot)" Condition="'$(IsWindows)' != 'true'" Command="mkdir -p build %26%26 cd build %26%26 cmake .. %26%26 make -j" />
 | 
				
			||||||
		<Exec WorkingDirectory="$(TranscoderRoot)" Condition="'$(IsWindows)' == 'true'" Command="(if not exist build mkdir build) %26%26 cd build %26%26 cmake .. -G "NMake Makefiles" %26%26 nmake" />
 | 
							<Exec WorkingDirectory="$(TranscoderRoot)" Condition="'$(IsWindows)' == 'true'" Command="(if not exist build mkdir build) %26%26 cd build %26%26 cmake .. -G "NMake Makefiles" %26%26 nmake" />
 | 
				
			||||||
	</Target>
 | 
						</Target>
 | 
				
			||||||
 | 
				
			|||||||
@ -131,19 +131,13 @@ namespace Kyoo
 | 
				
			|||||||
		/// This is meant to be used from <see cref="WebHostBuilderExtensions.UseStartup"/>.
 | 
							/// This is meant to be used from <see cref="WebHostBuilderExtensions.UseStartup"/>.
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
		/// <param name="host">The context of the web host.</param>
 | 
							/// <param name="host">The context of the web host.</param>
 | 
				
			||||||
		/// <param name="loggingConfiguration">
 | 
							/// <param name="logger">
 | 
				
			||||||
		/// The method used to configure the logger factory used by the plugin manager and plugins during startup.
 | 
							/// The logger factory used to log while the application is setting itself up.
 | 
				
			||||||
		/// </param>
 | 
							/// </param>
 | 
				
			||||||
		/// <returns>A new <see cref="PluginsStartup"/>.</returns>
 | 
							/// <returns>A new <see cref="PluginsStartup"/>.</returns>
 | 
				
			||||||
		public static PluginsStartup FromWebHost(WebHostBuilderContext host, 
 | 
							public static PluginsStartup FromWebHost(WebHostBuilderContext host, 
 | 
				
			||||||
			Action<HostBuilderContext, ILoggingBuilder> loggingConfiguration)
 | 
								ILoggerFactory logger)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			HostBuilderContext genericHost = new(new Dictionary<object, object>())
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				Configuration = host.Configuration,
 | 
					 | 
				
			||||||
				HostingEnvironment = host.HostingEnvironment
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
			ILoggerFactory logger = LoggerFactory.Create(builder => loggingConfiguration(genericHost, builder));
 | 
					 | 
				
			||||||
			HostServiceProvider hostProvider = new(host.HostingEnvironment, host.Configuration, logger);
 | 
								HostServiceProvider hostProvider = new(host.HostingEnvironment, host.Configuration, logger);
 | 
				
			||||||
			PluginManager plugins = new(
 | 
								PluginManager plugins = new(
 | 
				
			||||||
				hostProvider,
 | 
									hostProvider,
 | 
				
			||||||
 | 
				
			|||||||
@ -2,11 +2,15 @@ using System;
 | 
				
			|||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using Autofac.Extensions.DependencyInjection;
 | 
					using Autofac.Extensions.DependencyInjection;
 | 
				
			||||||
 | 
					using JetBrains.Annotations;
 | 
				
			||||||
using Microsoft.AspNetCore.Hosting;
 | 
					using Microsoft.AspNetCore.Hosting;
 | 
				
			||||||
using Microsoft.Extensions.Configuration;
 | 
					using Microsoft.Extensions.Configuration;
 | 
				
			||||||
using Microsoft.Extensions.DependencyInjection;
 | 
					using Microsoft.Extensions.DependencyInjection;
 | 
				
			||||||
using Microsoft.Extensions.Hosting;
 | 
					using Microsoft.Extensions.Hosting;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using Serilog;
 | 
				
			||||||
 | 
					using Serilog.Templates;
 | 
				
			||||||
 | 
					using Serilog.Templates.Themes;
 | 
				
			||||||
using SEnvironment = System.Environment;
 | 
					using SEnvironment = System.Environment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Kyoo
 | 
					namespace Kyoo
 | 
				
			||||||
@ -30,6 +34,19 @@ namespace Kyoo
 | 
				
			|||||||
		private const string Environment = "Production";
 | 
							private const string Environment = "Production";
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/// <summary>
 | 
				
			||||||
 | 
							/// Initialize the bootstrap logger to use it anywhere. This is done here so it is called before any method,
 | 
				
			||||||
 | 
							/// even if the <see cref="Main"/> is not used and this binary is used as a dll.
 | 
				
			||||||
 | 
							/// </summary>
 | 
				
			||||||
 | 
							static Program()
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								LoggerConfiguration config = new();
 | 
				
			||||||
 | 
								_ConfigureLogging(null, config);
 | 
				
			||||||
 | 
								Log.Logger = config.CreateBootstrapLogger().ForContext<Application>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								AppDomain.CurrentDomain.ProcessExit += (_, _) => Log.CloseAndFlush();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
		/// <summary>
 | 
							/// <summary>
 | 
				
			||||||
		/// Main function of the program
 | 
							/// Main function of the program
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
@ -67,6 +84,7 @@ namespace Kyoo
 | 
				
			|||||||
		private static IConfigurationBuilder SetupConfig(IConfigurationBuilder builder, string[] args)
 | 
							private static IConfigurationBuilder SetupConfig(IConfigurationBuilder builder, string[] args)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			return builder.SetBasePath(System.Environment.CurrentDirectory)
 | 
								return builder.SetBasePath(System.Environment.CurrentDirectory)
 | 
				
			||||||
 | 
									.AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, JsonConfigPath), false, true)
 | 
				
			||||||
				.AddJsonFile(JsonConfigPath, false, true)
 | 
									.AddJsonFile(JsonConfigPath, false, true)
 | 
				
			||||||
				.AddEnvironmentVariables()
 | 
									.AddEnvironmentVariables()
 | 
				
			||||||
				.AddEnvironmentVariables("KYOO_")
 | 
									.AddEnvironmentVariables("KYOO_")
 | 
				
			||||||
@ -78,44 +96,54 @@ namespace Kyoo
 | 
				
			|||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
		/// <param name="context">The host context that contains the configuration</param>
 | 
							/// <param name="context">The host context that contains the configuration</param>
 | 
				
			||||||
		/// <param name="builder">The logger builder to configure.</param>
 | 
							/// <param name="builder">The logger builder to configure.</param>
 | 
				
			||||||
		public static void ConfigureLogging(HostBuilderContext context, ILoggingBuilder builder)
 | 
							private static void _ConfigureLogging([CanBeNull] HostBuilderContext context, LoggerConfiguration builder)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			builder.AddConfiguration(context.Configuration.GetSection("logging"))
 | 
								if (context != null)
 | 
				
			||||||
				.AddSimpleConsole(x =>
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
					x.TimestampFormat = "[hh:mm:ss] ";
 | 
									try
 | 
				
			||||||
				})
 | 
									{
 | 
				
			||||||
				.AddDebug()
 | 
										builder.ReadFrom.Configuration(context.Configuration, "logging");
 | 
				
			||||||
				.AddEventSourceLogger();
 | 
									}
 | 
				
			||||||
 | 
									catch (Exception ex)
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										Log.Fatal(ex, "Could not read serilog configuration");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								const string template =
 | 
				
			||||||
 | 
									"[{@t:HH:mm:ss} {@l:u3} {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), 15} ({@i:10})] " 
 | 
				
			||||||
 | 
									+ "{@m}{#if not EndsWith(@m, '\n')}\n{#end}{@x}";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								builder
 | 
				
			||||||
 | 
									.WriteTo.Console(new ExpressionTemplate(template, theme: TemplateTheme.Code))
 | 
				
			||||||
 | 
									.WriteTo.Debug()
 | 
				
			||||||
 | 
									.WriteTo.RollingFile(new ExpressionTemplate(template), "logs/log-{Date}.log")
 | 
				
			||||||
 | 
									.Enrich.WithThreadId()
 | 
				
			||||||
 | 
									.Enrich.FromLogContext();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		/// <summary>
 | 
							/// <summary>
 | 
				
			||||||
		/// Create a a web host
 | 
							/// Create a a web host
 | 
				
			||||||
		/// </summary>
 | 
							/// </summary>
 | 
				
			||||||
		/// <param name="args">Command line parameters that can be handled by kestrel</param>
 | 
							/// <param name="args">Command line parameters that can be handled by kestrel</param>
 | 
				
			||||||
		/// <param name="loggingConfiguration">
 | 
					 | 
				
			||||||
		/// An action to configure the logging. If it is null, <see cref="ConfigureLogging"/> will be used.
 | 
					 | 
				
			||||||
		/// </param>
 | 
					 | 
				
			||||||
		/// <returns>A new web host instance</returns>
 | 
							/// <returns>A new web host instance</returns>
 | 
				
			||||||
		public static IHostBuilder CreateWebHostBuilder(string[] args,
 | 
							public static IHostBuilder CreateWebHostBuilder(string[] args)
 | 
				
			||||||
			Action<HostBuilderContext, ILoggingBuilder> loggingConfiguration = null)
 | 
					 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			IConfiguration configuration = SetupConfig(new ConfigurationBuilder(), args).Build();
 | 
								IConfiguration configuration = SetupConfig(new ConfigurationBuilder(), args).Build();
 | 
				
			||||||
			loggingConfiguration ??= ConfigureLogging;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return new HostBuilder()
 | 
								return new HostBuilder()
 | 
				
			||||||
				.UseServiceProviderFactory(new AutofacServiceProviderFactory())
 | 
									.UseServiceProviderFactory(new AutofacServiceProviderFactory())
 | 
				
			||||||
				.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
 | 
									.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
 | 
				
			||||||
				.UseEnvironment(Environment)
 | 
									.UseEnvironment(Environment)
 | 
				
			||||||
				.ConfigureAppConfiguration(x => SetupConfig(x, args))
 | 
									.ConfigureAppConfiguration(x => SetupConfig(x, args))
 | 
				
			||||||
				.ConfigureLogging(loggingConfiguration)
 | 
									.UseSerilog(_ConfigureLogging)
 | 
				
			||||||
				.ConfigureServices(x => x.AddRouting())
 | 
									.ConfigureServices(x => x.AddRouting())
 | 
				
			||||||
				.ConfigureWebHost(x => x
 | 
									.ConfigureWebHost(x => x
 | 
				
			||||||
					.UseKestrel(options => { options.AddServerHeader = false; })
 | 
										.UseKestrel(options => { options.AddServerHeader = false; })
 | 
				
			||||||
					.UseIIS()
 | 
										.UseIIS()
 | 
				
			||||||
					.UseIISIntegration()
 | 
										.UseIISIntegration()
 | 
				
			||||||
					.UseUrls(configuration.GetValue<string>("basics:url"))
 | 
										.UseUrls(configuration.GetValue<string>("basics:url"))
 | 
				
			||||||
					.UseStartup(host => PluginsStartup.FromWebHost(host, loggingConfiguration))
 | 
										.UseStartup(host => PluginsStartup.FromWebHost(host, new LoggerFactory().AddSerilog()))
 | 
				
			||||||
				);
 | 
									);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -144,10 +172,10 @@ namespace Kyoo
 | 
				
			|||||||
			if (!File.Exists(JsonConfigPath))
 | 
								if (!File.Exists(JsonConfigPath))
 | 
				
			||||||
				File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, JsonConfigPath), JsonConfigPath);
 | 
									File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, JsonConfigPath), JsonConfigPath);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	/// <summary>
 | 
						/// <summary>
 | 
				
			||||||
	/// An useless class only used to have a logger in the main.
 | 
						/// An useless class only used to have a logger in the main.
 | 
				
			||||||
	/// </summary>
 | 
						/// </summary>
 | 
				
			||||||
		private class Application {}
 | 
						internal class Application {}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -30,12 +30,14 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  "logging": {
 | 
					  "logging": {
 | 
				
			||||||
    "logLevel": {
 | 
					    "MinimumLevel": {
 | 
				
			||||||
      "default": "Warning",
 | 
					      "Default": "Warning",
 | 
				
			||||||
 | 
					      "Override": {
 | 
				
			||||||
        "Microsoft": "Warning",
 | 
					        "Microsoft": "Warning",
 | 
				
			||||||
        "Microsoft.Hosting.Lifetime": "Information",
 | 
					        "Microsoft.Hosting.Lifetime": "Information",
 | 
				
			||||||
      "Microsoft.EntityFrameworkCore": "None",
 | 
					        "Microsoft.EntityFrameworkCore": "Fatal",
 | 
				
			||||||
      "Kyoo": "Trace"
 | 
					        "Kyoo": "Verbose"
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user