-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: host builder pipeline logging
This change moves implementations to multiple, more sensible components. It also configures SeriLog correctly in the host builder pipeline.
- Loading branch information
1 parent
258c161
commit a89009a
Showing
5 changed files
with
163 additions
and
98 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/DaemonRunner/DaemonRunner/Service/Extensions/HostBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using NetDaemon.Service.Support; | ||
using Serilog; | ||
|
||
namespace NetDaemon.Service.Extensions | ||
{ | ||
public static class HostBuilderExtensions | ||
{ | ||
// We preserve the static logger so that we can access it statically and early on in the application lifecycle. | ||
private const bool PreserveStaticLogger = true; | ||
|
||
public static IHostBuilder UseNetDaemon(this IHostBuilder builder) | ||
{ | ||
return builder | ||
.UseNetDaemonSerilog() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddNetDaemon(); | ||
}); | ||
} | ||
|
||
private static IHostBuilder UseNetDaemonSerilog(this IHostBuilder builder) | ||
{ | ||
return builder.UseSerilog( | ||
(context, configuration) => SeriLogConfigurator.Configure(configuration), | ||
PreserveStaticLogger | ||
); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/DaemonRunner/DaemonRunner/Service/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace NetDaemon.Service.Extensions | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddNetDaemon(this IServiceCollection services) | ||
{ | ||
services.AddHttpClient(); | ||
services.AddHostedService<RunnerService>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using NetDaemon.Service.Extensions; | ||
using NetDaemon.Service.Support; | ||
using Serilog; | ||
|
||
namespace NetDaemon.Service | ||
{ | ||
public static class Runner | ||
{ | ||
private const string _hassioConfigPath = "/data/options.json"; | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.UseNetDaemon(); | ||
|
||
public static async Task Run(string[] args) | ||
{ | ||
try | ||
{ | ||
Log.Logger = SeriLogConfigurator.GetConfiguration().CreateLogger(); | ||
|
||
if (File.Exists(_hassioConfigPath)) | ||
{ | ||
try | ||
{ | ||
var hassAddOnSettings = await JsonSerializer.DeserializeAsync<HassioConfig>( | ||
File.OpenRead(_hassioConfigPath)).ConfigureAwait(false); | ||
if (hassAddOnSettings.LogLevel is object) | ||
{ | ||
SeriLogConfigurator.SetMinimumLogLevel(hassAddOnSettings.LogLevel); | ||
} | ||
if (hassAddOnSettings.GenerateEntitiesOnStart is object) | ||
{ | ||
Environment.SetEnvironmentVariable("HASS_GEN_ENTITIES", hassAddOnSettings.GenerateEntitiesOnStart.ToString()); | ||
} | ||
if (hassAddOnSettings.LogMessages is object && hassAddOnSettings.LogMessages == true) | ||
{ | ||
Environment.SetEnvironmentVariable("HASSCLIENT_MSGLOGLEVEL", "Default"); | ||
} | ||
if (hassAddOnSettings.ProjectFolder is object && | ||
string.IsNullOrEmpty(hassAddOnSettings.ProjectFolder) == false) | ||
{ | ||
Environment.SetEnvironmentVariable("HASS_RUN_PROJECT_FOLDER", hassAddOnSettings.ProjectFolder); | ||
} | ||
|
||
// We are in Hassio so hard code the path | ||
Environment.SetEnvironmentVariable("HASS_DAEMONAPPFOLDER", "/config/netdaemon"); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Fatal(e, "Failed to read the Home Assistant Add-on config"); | ||
} | ||
} | ||
else | ||
{ | ||
var envLogLevel = Environment.GetEnvironmentVariable("HASS_LOG_LEVEL"); | ||
if (!string.IsNullOrEmpty(envLogLevel)) | ||
{ | ||
SeriLogConfigurator.SetMinimumLogLevel(envLogLevel); | ||
} | ||
} | ||
|
||
await CreateHostBuilder(args).Build().RunAsync(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Fatal(e, "Failed to start host..."); | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/DaemonRunner/DaemonRunner/Service/Support/SeriLogConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Serilog; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Serilog.Sinks.SystemConsole.Themes; | ||
|
||
namespace NetDaemon.Service.Support | ||
{ | ||
internal static class SeriLogConfigurator | ||
{ | ||
private static readonly LoggingLevelSwitch LevelSwitch = new LoggingLevelSwitch(); | ||
|
||
public static LoggerConfiguration GetConfiguration() | ||
{ | ||
return Configure(new LoggerConfiguration()); | ||
} | ||
|
||
public static LoggerConfiguration Configure(LoggerConfiguration configuration) | ||
{ | ||
return configuration | ||
.MinimumLevel.ControlledBy(LevelSwitch) | ||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console(theme: AnsiConsoleTheme.Code); | ||
} | ||
|
||
public static void SetMinimumLogLevel(string level) | ||
{ | ||
LevelSwitch.MinimumLevel = level switch | ||
{ | ||
"info" => LogEventLevel.Information, | ||
"debug" => LogEventLevel.Debug, | ||
"error" => LogEventLevel.Error, | ||
"warning" => LogEventLevel.Warning, | ||
"trace" => LogEventLevel.Verbose, | ||
_ => LogEventLevel.Information | ||
}; | ||
} | ||
} | ||
} |