Skip to content

Commit

Permalink
Add system and ansi themes, Add development settings als lanchsettings (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlux committed Dec 23, 2020
1 parent b99b7ef commit af1cb6f
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ codecover

### Jetbrains / Rider ###
# User-specific files
.idea/
.idea/
appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NetDaemon.Infrastructure.Config
{
public class LoggingConfiguration
{
public string MinimumLevel { get; set; } = "Info";

public string ConsoleThemeType { get; set; } = "Ansi";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using Serilog.Sinks.SystemConsole.Themes;

namespace NetDaemon.Infrastructure.Config
{
public static class NetDaemonConsoleThemes
{
private static SystemConsoleTheme SystemTheme { get; } = new(new Dictionary<ConsoleThemeStyle, SystemConsoleThemeStyle>
{
[ConsoleThemeStyle.Text] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Gray},
[ConsoleThemeStyle.SecondaryText] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.DarkGray},
[ConsoleThemeStyle.TertiaryText] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.DarkGray},
[ConsoleThemeStyle.Invalid] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Yellow},
[ConsoleThemeStyle.Null] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.Name] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.String] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.Number] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.Boolean] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.Scalar] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Green},
[ConsoleThemeStyle.LevelVerbose] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Gray},
[ConsoleThemeStyle.LevelDebug] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.DarkYellow},
[ConsoleThemeStyle.LevelInformation] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.DarkGreen},
[ConsoleThemeStyle.LevelWarning] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Yellow},
[ConsoleThemeStyle.LevelError] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.Red},
[ConsoleThemeStyle.LevelFatal] = new SystemConsoleThemeStyle {Foreground = ConsoleColor.DarkRed},
});

private static AnsiConsoleTheme AnsiTheme { get; } = new(new Dictionary<ConsoleThemeStyle, string>
{
[ConsoleThemeStyle.Text] = "\x1b[38;5;0253m",
[ConsoleThemeStyle.SecondaryText] = "\x1b[38;5;0246m",
[ConsoleThemeStyle.TertiaryText] = "\x1b[38;5;0242m",
[ConsoleThemeStyle.Invalid] = "\x1b[33;1m",
[ConsoleThemeStyle.Null] = "\x1b[38;5;0038m",
[ConsoleThemeStyle.Number] = "\x1b[38;5;151m",
[ConsoleThemeStyle.Boolean] = "\x1b[38;5;0038m",
[ConsoleThemeStyle.LevelVerbose] = "\x1b[37m",
[ConsoleThemeStyle.LevelError] = "\u001b[0;31m",
[ConsoleThemeStyle.Name] = "\u001b[1;34m",
[ConsoleThemeStyle.LevelInformation] = "\u001b[0;36m",
[ConsoleThemeStyle.LevelWarning] = "\u001b[1;33m",
[ConsoleThemeStyle.LevelFatal] = "\u001b[0;31m",
[ConsoleThemeStyle.LevelDebug] = "\u001b[0;37m",
[ConsoleThemeStyle.Scalar] = "\u001b[1;34m",
[ConsoleThemeStyle.String] = "\u001b[0;36m",
});

public static ConsoleTheme GetThemeByType(string type)
{
return string.Equals(type, "system", StringComparison.InvariantCultureIgnoreCase) ? SystemTheme : AnsiTheme;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,90 +1,41 @@
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;

namespace NetDaemon.Infrastructure.Config
{
internal class NetDaemonTheme : Serilog.Sinks.SystemConsole.Themes.ConsoleTheme
{
public override bool CanBuffer => false;

protected override int ResetCharCount => 0;

public override bool Equals(object? obj)
{
return base.Equals(obj);
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override void Reset(TextWriter output)
{
output.Write("\u001b[0m");
}

public override int Set(TextWriter output, ConsoleThemeStyle style)
{
string? x = style switch
{
ConsoleThemeStyle.LevelError => "\u001b[0;31m",
ConsoleThemeStyle.Name => "\u001b[1;34m",
ConsoleThemeStyle.LevelInformation => "\u001b[0;36m",
ConsoleThemeStyle.LevelWarning => "\u001b[1;33m",
ConsoleThemeStyle.LevelFatal => "\u001b[0;31m",
ConsoleThemeStyle.LevelDebug => "\u001b[0;37m",
ConsoleThemeStyle.Scalar => "\u001b[1;34m",
ConsoleThemeStyle.String => "\u001b[0;36m",
_ => null
};

if (x is not null)
{
output.Write(x);
return x.Length;
}
return 0;
}

public override string? ToString()
{
return base.ToString();
}
}

public static class SerilogConfigurator
{
private static readonly LoggingLevelSwitch LevelSwitch = new LoggingLevelSwitch();

public static LoggerConfiguration Configure()
public static LoggerConfiguration Configure(LoggerConfiguration loggerConfiguration, IHostEnvironment hostingEnvironment)
{
var minimumLevel = GetMinimumLogLevel();
var loggingConfiguration = GetLoggingConfiguration(hostingEnvironment);

SetMinimumLogLevel(minimumLevel);
SetMinimumLogLevel(loggingConfiguration.MinimumLevel);

return new LoggerConfiguration()
return loggerConfiguration
.MinimumLevel.ControlledBy(LevelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console(theme: new NetDaemonTheme(), applyThemeToRedirectedOutput: true);
.WriteTo.Console(theme: NetDaemonConsoleThemes.GetThemeByType(loggingConfiguration.ConsoleThemeType), applyThemeToRedirectedOutput: true);
}

private static string GetMinimumLogLevel()
private static LoggingConfiguration GetLoggingConfiguration(IHostEnvironment hostingEnvironment)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();

var logValue = configuration.GetSection("Logging")["MinimumLevel"];

return string.IsNullOrEmpty(logValue) ? "info" : logValue;
var loggingConfiguration = new LoggingConfiguration();
configuration.GetSection("Logging").Bind(loggingConfiguration);
return loggingConfiguration;
}

public static void SetMinimumLogLevel(string level)
Expand All @@ -99,6 +50,5 @@ public static void SetMinimumLogLevel(string level)
_ => LogEventLevel.Information
};
}

}
}
14 changes: 5 additions & 9 deletions src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace NetDaemon
public static class NetDaemonExtensions
{
const string HassioConfigPath = "/data/options.json";

public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)
{
if (File.Exists(HassioConfigPath))
Expand All @@ -30,7 +31,6 @@ public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)
services.AddSingleton<IYamlConfig, YamlConfig>();
RegisterNetDaemonAssembly(services);
})
.ConfigureWebHostDefaults(webbuilder =>
{
Expand All @@ -41,12 +41,10 @@ public static IHostBuilder UseNetDaemon(this IHostBuilder hostBuilder)

public static IHostBuilder UseDefaultNetDaemonLogging(this IHostBuilder hostBuilder)
{
return hostBuilder
.ConfigureWebHostDefaults(webbuilder =>
{
Log.Logger = SerilogConfigurator.Configure().CreateLogger();
webbuilder.UseSerilog(Log.Logger);
});
return hostBuilder.UseSerilog((context, loggerConfiguration) =>
{
SerilogConfigurator.Configure(loggerConfiguration, context.HostingEnvironment);
});
}

public static void CleanupNetDaemon()
Expand All @@ -69,8 +67,6 @@ private static void RegisterNetDaemonAssembly(IServiceCollection services)
/// </summary>
private static bool UseLocalAssemblyLoading()
{


var appSource = Environment.GetEnvironmentVariable("NETDAEMON__APPSOURCE");

if (string.IsNullOrEmpty(appSource))
Expand Down
11 changes: 11 additions & 0 deletions src/Service/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Service": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development",
"HASS_DISABLE_LOCAL_ASM": "true"
}
}
}
}
13 changes: 9 additions & 4 deletions src/Service/Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
<ProjectReference Include="..\DaemonRunner\DaemonRunner\DaemonRunner.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include=".config\" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="apps\DebugApp.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="_appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Service/_appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Logging": {
"ConsoleThemeType": "System"
},
"NetDaemon": {
"Admin": false,
"ProjectFolder": "",
"SourceFolder": "",
"GenerateEntities": false
},
"HomeAssistant": {
"Host": "HOME_ASSISTANT_IP",
"Port": 8123,
"Ssl": false,
"Token": "TOKEN"
}
}
3 changes: 2 additions & 1 deletion src/Service/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Logging": {
"MinimumLevel": "Debug"
"MinimumLevel": "Debug",
"ConsoleThemeType": "Ansi"
},
"HomeAssistant": {
"Host": "localhost",
Expand Down

0 comments on commit af1cb6f

Please sign in to comment.