Skip to content

Commit

Permalink
Added Default Configuration Builder (#118)
Browse files Browse the repository at this point in the history
* Refactored to an Proxy interface to address #47

* Added remaining EventsSent to ConfigurationManager

* Attempting to eliminate memory leak

* Adding script to test the Plugin template

* WIP

* Updated to be more cross-platform friendly

* Re-added cheer graffiti

* Cleaning up the inner-loop build-debug-process

* Addressed logger and memory leak issues #73 #114

* Added a default configuration builder #34

 Addressed logger and memory leak issues #73 #114  (#117)

* Refactored to an Proxy interface to address #47

* Added remaining EventsSent to ConfigurationManager

* Attempting to eliminate memory leak

* Adding script to test the Plugin template

* WIP

* Updated to be more cross-platform friendly

* Re-added cheer graffiti

* Cleaning up the inner-loop build-debug-process

* Addressed logger and memory leak issues #73 #114
  • Loading branch information
csharpfritz authored Feb 15, 2019
1 parent ee798c2 commit 827ce2b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 76 deletions.
82 changes: 6 additions & 76 deletions src/SamplePlugin/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

}
}
Expand Down
1 change: 1 addition & 0 deletions src/SamplePlugin/SamplePlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\StreamDeckLib.Config\StreamDeckLib.Config.csproj" />
<ProjectReference Include="..\StreamDeckLib\StreamDeckLib.csproj" />
</ItemGroup>

Expand Down
80 changes: 80 additions & 0 deletions src/StreamDeckLib.Config/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
@@ -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

}
}
}
18 changes: 18 additions & 0 deletions src/StreamDeckLib.Config/StreamDeckLib.Config.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/StreamDeckToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 827ce2b

Please sign in to comment.