working cli with dummy hosted service (logging, config)
This commit is contained in:
parent
507861bc28
commit
ab8687b41e
@ -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<IPlayerWatcher, PlayerWatcher>()
|
||||
.AddTransient<IWatcherCollection, WatcherCollection>()
|
||||
.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<Program>();
|
||||
//logger.LogInformation("Example log message");
|
||||
|
||||
//var logger = serviceProvider.GetService<ILoggerFactory>()
|
||||
// .CreateLogger<Program>();
|
||||
//logger.LogDebug("Starting application");
|
||||
|
||||
var logger = serviceProvider.GetService<ILogger>();
|
||||
|
||||
logger.LogDebug("All done!");
|
||||
var host = CreateHostBuilder(args);
|
||||
await host.RunConsoleAsync();
|
||||
}
|
||||
|
||||
static IHostBuilder CreateHostBuilder(string[] args)
|
||||
=> Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((context, services) => {
|
||||
services.AddTransient<IPlayerWatcher, PlayerWatcher>();
|
||||
services.AddTransient<IWatcherCollection, WatcherCollection>();
|
||||
services.AddHostedService<WatcherService>();
|
||||
})
|
||||
.ConfigureLogging(builder => {
|
||||
builder
|
||||
.AddSimpleConsole(options => {
|
||||
options.IncludeScopes = true;
|
||||
options.SingleLine = true;
|
||||
options.TimestampFormat = "yyyy-mm-dd hh:mm:ss ";
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||
<PackageReference Include="SpotifyAPI.Web" Version="6.2.2" />
|
||||
@ -18,4 +19,10 @@
|
||||
<ProjectReference Include="..\Selector\Selector.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
15
Selector.CLI/SelectorOptions.cs
Normal file
15
Selector.CLI/SelectorOptions.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
48
Selector.CLI/WatcherService.cs
Normal file
48
Selector.CLI/WatcherService.cs
Normal file
@ -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<WatcherService> Logger;
|
||||
private readonly IConfiguration Config;
|
||||
|
||||
public WatcherService(ILogger<WatcherService> 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;
|
||||
}
|
||||
}
|
||||
}
|
20
Selector.CLI/appsettings.json
Normal file
20
Selector.CLI/appsettings.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user