Program.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using CustomHostingDemo.Logging;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.Logging;
  4. using Ninject;
  5. using System;
  6. using System.CommandLine;
  7. using System.CommandLine.Builder;
  8. using System.CommandLine.Parsing;
  9. using System.IO;
  10. using System.Reflection;
  11. namespace CustomHostingDemo
  12. {
  13. class Program
  14. {
  15. static int Main(string[] args)
  16. {
  17. var kernel = new StandardKernel(
  18. new LoggingModule(),
  19. new CommandHandlerModule()
  20. );
  21. var config = new ConfigurationBuilder()
  22. .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
  23. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
  24. .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
  25. .Build();
  26. kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
  27. var logger = kernel.Get<Logger<Program>>();
  28. logger.LogInformation($"Informational Message - Hello {config["Hello"]}");
  29. logger.LogWarning("Ooopsie!");
  30. var rootCommand = new RootCommand()
  31. {
  32. new HelloCommand().Bind(kernel),
  33. };
  34. var parser = new CommandLineBuilder(rootCommand)
  35. .UseDefaults()
  36. .Build();
  37. return parser.InvokeAsync(args).Result;
  38. }
  39. }
  40. }