diff --git a/src/SamplePlugin/Program.cs b/src/SamplePlugin/Program.cs index 6f68afc..19f9869 100644 --- a/src/SamplePlugin/Program.cs +++ b/src/SamplePlugin/Program.cs @@ -1,13 +1,4 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; -using Serilog; -using StreamDeckLib; -using System; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Threading; +using StreamDeckLib; using System.Threading.Tasks; namespace SamplePlugin @@ -19,80 +10,19 @@ class Program // Cheer 200 careypayette February 14, 2019 // Cheer 100 roberttables February 14, 2019 // Cheer 100 careypayette February 15, 2019 - // Cheer 100 devlead 15/2/2019 - - static ILoggerFactory GetLoggerFactory() - { - var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - Directory.SetCurrentDirectory(dir); - - var configuration = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") - .Build(); - - Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(configuration) - .CreateLogger(); - - var loggerFactory = new LoggerFactory() - .AddSerilog(Log.Logger); - - TopLogger = loggerFactory.CreateLogger("top"); - - TopLogger.LogInformation("Plugin started"); - - return loggerFactory; - } - - private static Microsoft.Extensions.Logging.ILogger TopLogger; + // Cheer 100 devlead 15/2/2019 static async Task Main(string[] args) { -#if DEBUG - // This gives us our "first chance" debugging, before even parsing the command - // line args, without the need to manually edit the code to toggle the feature - // ability on or off. - - if (args.Select(arg => arg.Replace("--", "-")) - .Any(arg => arg.Equals("-break"))) + using (var config = StreamDeckLib.Config.ConfigurationBuilder.BuildDefaultConfiguration(args)) { - Console.WriteLine("Debugging has been requested. Waiting for a debugger to attach..."); - Debugger.Launch(); - - while (!Debugger.IsAttached) - { - await Task.Delay(500); - Console.Write("."); - } - } -#endif - using (var loggerFactory = GetLoggerFactory()) - { - try - { - // codingbandit: I had to take out the using statement as it was causing the dispose method to be called (and disposing the socket connection) - await ConnectionManager.Initialize(args, loggerFactory) - .RegisterAction(new MySampleAction()) - .StartAsync(); + await ConnectionManager.Initialize(args, config.LoggerFactory) + .RegisterAction(new MySampleAction()) + .StartAsync(); - } - catch (Exception ex) - { - TopLogger.LogError(ex, "Error while running the plugin"); - } - - } - -#if DEBUG - if (Debugger.IsAttached) - { - // If a debugger is attached, give the developer a last chance to inspect - // variables, state, etc. before the process terminates. - Debugger.Break(); } -#endif } } diff --git a/src/SamplePlugin/SamplePlugin.csproj b/src/SamplePlugin/SamplePlugin.csproj index 9d8bb9b..f68b9ea 100644 --- a/src/SamplePlugin/SamplePlugin.csproj +++ b/src/SamplePlugin/SamplePlugin.csproj @@ -19,6 +19,7 @@ + diff --git a/src/StreamDeckLib.Config/ConfigurationBuilder.cs b/src/StreamDeckLib.Config/ConfigurationBuilder.cs new file mode 100644 index 0000000..b81560c --- /dev/null +++ b/src/StreamDeckLib.Config/ConfigurationBuilder.cs @@ -0,0 +1,80 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using CONFIG = Microsoft.Extensions.Configuration; + +namespace StreamDeckLib.Config +{ + public class ConfigurationBuilder : IDisposable + { + + private ConfigurationBuilder(string[] args) { + +#if DEBUG + // This gives us our "first chance" debugging, before even parsing the command + // line args, without the need to manually edit the code to toggle the feature + // ability on or off. + + if (args.Select(arg => arg.Replace("--", "-")) + .Any(arg => arg.Equals("-break"))) + { + Console.WriteLine("Debugging has been requested. Waiting for a debugger to attach..."); + Debugger.Launch(); + + while (!Debugger.IsAttached) + { + Task.Delay(500).GetAwaiter().GetResult(); + Console.Write("."); + } + } +#endif + } + private static ConfigurationBuilder Instance; + + public static ConfigurationBuilder BuildDefaultConfiguration(string[] args) { + + var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + Directory.SetCurrentDirectory(dir); + + var configuration = new CONFIG.ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); + + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + Instance = new ConfigurationBuilder(args) + { + LoggerFactory = new LoggerFactory() + .AddSerilog(Log.Logger) + }; + + return Instance; + + } + + public ILoggerFactory LoggerFactory { get; private set; } + + + public void Dispose() + { + + #if DEBUG + if (Debugger.IsAttached) + { + // If a debugger is attached, give the developer a last chance to inspect + // variables, state, etc. before the process terminates. + Debugger.Break(); + } +#endif + + } + } +} diff --git a/src/StreamDeckLib.Config/StreamDeckLib.Config.csproj b/src/StreamDeckLib.Config/StreamDeckLib.Config.csproj new file mode 100644 index 0000000..2c61f0e --- /dev/null +++ b/src/StreamDeckLib.Config/StreamDeckLib.Config.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + + + diff --git a/src/StreamDeckToolkit.sln b/src/StreamDeckToolkit.sln index 403d22d..1b0abd7 100644 --- a/src/StreamDeckToolkit.sln +++ b/src/StreamDeckToolkit.sln @@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Registration", "Registratio ..\scripts\registration\RegisterPluginAndStartStreamDeck.sh = ..\scripts\registration\RegisterPluginAndStartStreamDeck.sh EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamDeckLib.Config", "StreamDeckLib.Config\StreamDeckLib.Config.csproj", "{32D1D35F-58ED-4BB2-B1CE-473C89757282}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -46,6 +48,10 @@ Global {CAD3C79B-E603-4356-B5BD-5B2E57DFD8BE}.Debug|Any CPU.Build.0 = Debug|Any CPU {CAD3C79B-E603-4356-B5BD-5B2E57DFD8BE}.Release|Any CPU.ActiveCfg = Release|Any CPU {CAD3C79B-E603-4356-B5BD-5B2E57DFD8BE}.Release|Any CPU.Build.0 = Release|Any CPU + {32D1D35F-58ED-4BB2-B1CE-473C89757282}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32D1D35F-58ED-4BB2-B1CE-473C89757282}.Debug|Any CPU.Build.0 = Debug|Any CPU + {32D1D35F-58ED-4BB2-B1CE-473C89757282}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32D1D35F-58ED-4BB2-B1CE-473C89757282}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE