From 7c3cac9984c435fdd12a9b65632c86a23011a572 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 2 Sep 2021 18:17:16 +0200 Subject: [PATCH] Host: Cleaning up logs and lifetime on systemd --- Kyoo.Core/Application.cs | 16 +++++++++++++++- Kyoo.Core/Kyoo.Core.csproj | 2 ++ deployment/kyoo.service | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Kyoo.Core/Application.cs b/Kyoo.Core/Application.cs index 150dfb6a..d3a2553f 100644 --- a/Kyoo.Core/Application.cs +++ b/Kyoo.Core/Application.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Hosting.Systemd; using Microsoft.Extensions.Logging; using Microsoft.Win32; using Serilog; @@ -200,6 +201,7 @@ namespace Kyoo.Core .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) .UseEnvironment(_environment) + .UseSystemd() .ConfigureAppConfiguration(x => _SetupConfig(x, args)) .UseSerilog((host, services, builder) => _ConfigureLogging(builder, host.Configuration, services)) .ConfigureServices(x => x.AddRouting()) @@ -259,7 +261,19 @@ namespace Kyoo.Core const string template = "[{@t:HH:mm:ss} {@l:u3} {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1), 15} " - + "({@i:0000000000})] {@m}{#if not EndsWith(@m, '\n')}\n{#end}{@x}"; + + "({@i:D10})] {@m}{#if not EndsWith(@m, '\n')}\n{#end}{@x}"; + + if (SystemdHelpers.IsSystemdService()) + { + const string syslogTemplate = "[{SourceContext,-35}] {Message:lj}{NewLine}{Exception}"; + + builder + .WriteTo.Console(new ExpressionTemplate(template, theme: TemplateTheme.Code)) + .WriteTo.LocalSyslog("Kyoo", outputTemplate: syslogTemplate) + .Enrich.WithThreadId() + .Enrich.FromLogContext(); + return; + } builder .WriteTo.Console(new ExpressionTemplate(template, theme: TemplateTheme.Code)) diff --git a/Kyoo.Core/Kyoo.Core.csproj b/Kyoo.Core/Kyoo.Core.csproj index 0e37a4f9..917b5a31 100644 --- a/Kyoo.Core/Kyoo.Core.csproj +++ b/Kyoo.Core/Kyoo.Core.csproj @@ -23,10 +23,12 @@ + + diff --git a/deployment/kyoo.service b/deployment/kyoo.service index 121ffc9d..5727b7c5 100644 --- a/deployment/kyoo.service +++ b/deployment/kyoo.service @@ -9,6 +9,8 @@ Environment=KYOO_DATADIR=/var/lib/kyoo ExecStart=/usr/lib/kyoo/Kyoo.Host.Console Restart=on-abort TimeoutSec=20 +StandardOutput=null +StandardError=null [Install] WantedBy=multi-user.target