-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V0.2.1 - Bug Fixes, and Better Logging (#35)
* Added "Debug" Configuration (#32) * Added Configuration options to support #31 * ALlow cfg to disable publishing discovery messages, for #31 * #31 - Only print discovery messages when specified by debug config. * If publshing messages is disabled- also don't spam the console the broker is not connected. * Correct NullReferenceExceptions (#33) * Remove uneeded braces. * This fixes #27, caused because the default value was null. * throw error, if for unexpected reasons, we do get a null http client. * Change default topic to "rPDU2MQTT". * Better Logging (#34) * Log a message indicating we found the config file. * Added starting logging configuration, for #25 * Renamed these files to be more concise. * Use serilog for logging. * Updating application to use Serilog Static Logging. Don't see value in using Microsoft.Extensions.Logging. Just- extra dependancies to move around. * Removed appsettings.json completely. * Static Logger, used for configuring dependancies, initial setup, etc. * Global using for Serilog. * Added serilog packages. * Removed static global using. "Information" does not have the needed context. * Log message showing that we did successfully load a configuration file. * Sorting usings * Added Configuration for Logging. * Added extension to configure logging. * Use serilog * Remove appsettings from .gitignore. * Use serilog here. * Added Path to default/example config. * Moved file-based options to dedicated class. Added file rollover support, and retention support. * Set minimum log level during startup to verbose. * Added FileRollover / FileRetention to Config.yaml
- Loading branch information
1 parent
c22a3de
commit 13b324f
Showing
26 changed files
with
303 additions
and
136 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,28 @@ | ||
using System.ComponentModel; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace rPDU2MQTT.Models.Config; | ||
|
||
/// <summary> | ||
/// Settings for debugging and diagnostics. | ||
/// </summary> | ||
public class DebugConfig | ||
{ | ||
/// <summary> | ||
/// This- determines if messages are actually PUSHED to the MQTT broker. | ||
/// </summary> | ||
/// <remarks> | ||
/// Intended usage, is to allow the entire process to be tested, and debugged, without actually publishing the mwssages. | ||
/// </remarks> | ||
[YamlMember(Alias = "PublishMessages")] | ||
[DefaultValue(true)] | ||
public bool PublishMessages { get; set; } = true; | ||
|
||
|
||
/// <summary> | ||
/// When enabled, this will print the MQTT discovery messages to the console. | ||
/// </summary> | ||
[YamlMember(Alias = "PrintDiscovery")] | ||
[DefaultValue(false)] | ||
public bool PrintDiscovery { get; set; } = false; | ||
} |
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,29 @@ | ||
using rPDU2MQTT.Models.Config.Schemas; | ||
using Serilog.Events; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace rPDU2MQTT.Models.Config; | ||
|
||
/// <summary> | ||
/// Configuration for logging. | ||
/// </summary> | ||
public class LoggingConfig | ||
{ | ||
public string? LogFilePath { get; set; } = null; | ||
|
||
[YamlMember(Alias = "Console")] | ||
public LoggingTargetConfiguration Console { get; set; } = new LoggingTargetConfiguration() | ||
{ | ||
Enabled = true, | ||
Severity = LogEventLevel.Information, | ||
}; | ||
|
||
[YamlMember(Alias = "File")] | ||
public FileLoggingTargetConfiguration File { get; set; } = new FileLoggingTargetConfiguration() | ||
{ | ||
Enabled = false, | ||
Severity = LogEventLevel.Debug, | ||
FileRetention = 30, | ||
FileRollover = RollingInterval.Day, | ||
}; | ||
} |
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
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
19 changes: 19 additions & 0 deletions
19
rPDU2MQTT/Models/Config/Schemas/FileLoggingTargetConfiguration.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,19 @@ | ||
namespace rPDU2MQTT.Models.Config.Schemas; | ||
|
||
public class FileLoggingTargetConfiguration : LoggingTargetConfiguration | ||
{ | ||
/// <summary> | ||
/// Path to log file which will be used. | ||
/// </summary> | ||
public string? Path { get; set; } = null; | ||
|
||
/// <summary> | ||
/// This determines when files are rolled over. | ||
/// </summary> | ||
public RollingInterval FileRollover { get; set; } = RollingInterval.Day; | ||
|
||
/// <summary> | ||
/// This determines how many rolled over files to retain. | ||
/// </summary> | ||
public int FileRetention { get; set; } = 30; | ||
} |
13 changes: 13 additions & 0 deletions
13
rPDU2MQTT/Models/Config/Schemas/LoggingTargetConfiguration.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,13 @@ | ||
using Serilog.Events; | ||
|
||
namespace rPDU2MQTT.Models.Config.Schemas; | ||
|
||
public class LoggingTargetConfiguration | ||
{ | ||
public LogEventLevel Severity { get; set; } = LogEventLevel.Information; | ||
|
||
public string Format { get; set; } = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"; | ||
|
||
public bool Enabled { get; set; } = false; | ||
|
||
} |
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 |
---|---|---|
@@ -1,28 +1,34 @@ | ||
using HiveMQtt.Client; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using rPDU2MQTT.Startup; | ||
using System.Runtime.InteropServices; | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console() | ||
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Verbose) | ||
.CreateLogger(); | ||
|
||
var host = Host.CreateDefaultBuilder(args) | ||
.ConfigureAppConfiguration(ConfigLoader.Configure) | ||
.ConfigureAppConfiguration(o => { o.AddEnvironmentVariables(); }) | ||
.ConfigureServices(ServiceConfiguration.Configure) | ||
.ConfigureLogging(logging => | ||
{ | ||
logging.ClearProviders(); | ||
logging.AddConsole(); | ||
logging.AddConsole(); | ||
}) | ||
.Build(); | ||
|
||
//Ensure we can actually connect to MQTT. | ||
var client = host.Services.GetRequiredService<IHiveMQClient>(); | ||
var logger = host.Services.GetRequiredService<ILogger<IHiveMQClient>>(); | ||
|
||
logger.LogInformation($"Connecting to MQTT Broker at {client.Options.Host}:{client.Options.Port}"); | ||
Log.Information($"Connecting to MQTT Broker at {client.Options.Host}:{client.Options.Port}"); | ||
|
||
await client.ConnectAsync(); | ||
|
||
logger.LogInformation("Successfully connected to broker!"); | ||
Log.Information("Successfully connected to broker!"); | ||
|
||
host.Run(); |
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
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
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
Oops, something went wrong.