diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index 31d554f..d2cd32e 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -1,42 +1,34 @@ using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Configuration; namespace Selector.CLI { class Program { - static void Main(string[] args) + public static async Task Main(string[] args) { - var serviceProvider = new ServiceCollection() - //.AddLogging(b => - // b.AddFilter("Microsoft", LogLevel.Warning) - // .AddFilter("System", LogLevel.Warning) - // .AddFilter("Selector.CLI.Program", LogLevel.Debug) - // .AddConsole() - //) - .AddTransient() - .AddTransient() - .BuildServiceProvider(); - - //using var loggerFactory = LoggerFactory.Create(builder => - //{ - // builder - // .AddFilter("Microsoft", LogLevel.Warning) - // .AddFilter("System", LogLevel.Warning) - // .AddFilter("Selector.CLI.Program", LogLevel.Debug) - // .AddConsole(); - //}); - //ILogger logger = loggerFactory.CreateLogger(); - //logger.LogInformation("Example log message"); - - //var logger = serviceProvider.GetService() - // .CreateLogger(); - //logger.LogDebug("Starting application"); - - var logger = serviceProvider.GetService(); - - logger.LogDebug("All done!"); + var host = CreateHostBuilder(args); + await host.RunConsoleAsync(); } + + static IHostBuilder CreateHostBuilder(string[] args) + => Host.CreateDefaultBuilder(args) + .ConfigureServices((context, services) => { + services.AddTransient(); + services.AddTransient(); + services.AddHostedService(); + }) + .ConfigureLogging(builder => { + builder + .AddSimpleConsole(options => { + options.IncludeScopes = true; + options.SingleLine = true; + options.TimestampFormat = "yyyy-mm-dd hh:mm:ss "; + }); + }); } } diff --git a/Selector.CLI/Selector.CLI.csproj b/Selector.CLI/Selector.CLI.csproj index 8ae1861..9f25b36 100644 --- a/Selector.CLI/Selector.CLI.csproj +++ b/Selector.CLI/Selector.CLI.csproj @@ -9,6 +9,7 @@ + @@ -18,4 +19,10 @@ + + + PreserveNewest + + + diff --git a/Selector.CLI/SelectorOptions.cs b/Selector.CLI/SelectorOptions.cs new file mode 100644 index 0000000..072fd23 --- /dev/null +++ b/Selector.CLI/SelectorOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Selector.CLI +{ + class SelectorOptions + { + public const string Options = "Selector"; + + public int Number { get; set; } + } +} diff --git a/Selector.CLI/WatcherService.cs b/Selector.CLI/WatcherService.cs new file mode 100644 index 0000000..1241afa --- /dev/null +++ b/Selector.CLI/WatcherService.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Selector.CLI +{ + class WatcherService : IHostedService + { + private readonly ILogger Logger; + private readonly IConfiguration Config; + + public WatcherService(ILogger logger, IConfiguration config) + { + Logger = logger; + Config = config; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + Logger.LogInformation("Starting up"); + + foreach ((var key, var pair) in Config.AsEnumerable()) + //foreach ((var key, var pair) in Config.GetSection("Selector").AsEnumerable()) + { + Logger.LogInformation($"{key} => {pair}"); + } + + using(Logger.BeginScope("A New Scope!")) + { + Logger.LogError("From the scope!"); + } + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + Logger.LogInformation("Shutting down"); + return Task.CompletedTask; + } + } +} diff --git a/Selector.CLI/appsettings.json b/Selector.CLI/appsettings.json new file mode 100644 index 0000000..4314c3f --- /dev/null +++ b/Selector.CLI/appsettings.json @@ -0,0 +1,20 @@ +{ + "string config": "asdf", + "Selector": { + "Number": 4 + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + }, + "Console": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + //"Microsoft.Hosting.Lifetime": "Information" + } + } + } +} \ No newline at end of file