Эх сурвалжийг харах

Moved configuration setup to separate module

Lukas Angerer 4 жил өмнө
parent
commit
0e08f56e50

+ 22 - 0
CustomHostingDemo/ConfigurationModule.cs

@@ -0,0 +1,22 @@
+using Microsoft.Extensions.Configuration;
+using Ninject.Modules;
+using System;
+using System.IO;
+using System.Reflection;
+
+namespace CustomHostingDemo
+{
+    public class ConfigurationModule : NinjectModule
+    {
+        public override void Load()
+        {
+            var config = new ConfigurationBuilder()
+                .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
+                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
+                .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
+                .Build();
+
+            Bind<IConfiguration>().ToConstant(config).InSingletonScope();
+        }
+    }
+}

+ 3 - 0
CustomHostingDemo/HelloCommand.cs

@@ -39,6 +39,9 @@ namespace CustomHostingDemo
                 _logger.LogInfo($"P: --date {args.Date}");
                 _logger.LogInfo($"P: --verbose {args.Verbose}");
 
+                _logger.LogWarning("This is your last warning!");
+                _logger.LogError("Just kidding. This is a test.");
+
                 return Task.FromResult(0);
             }
         }

+ 2 - 2
CustomHostingDemo/Logging/CliLogger.cs

@@ -24,7 +24,7 @@ namespace CustomHostingDemo.Logging
         public ICliLogger LogError(string message)
         {
             return Log(new ContainerSpan(
-                ForegroundColorSpan.Red(),
+                ForegroundColorSpan.LightRed(),
                 new ContentSpan("[ERROR] "),
                 new ContentSpan(message),
                 ForegroundColorSpan.Reset()
@@ -49,7 +49,7 @@ namespace CustomHostingDemo.Logging
         public ICliLogger LogWarning(string message)
         {
             return Log(new ContainerSpan(
-                ForegroundColorSpan.Yellow(),
+                ForegroundColorSpan.LightYellow(),
                 new ContentSpan("[WARN] "),
                 new ContentSpan(message),
                 ForegroundColorSpan.Reset()

+ 2 - 14
CustomHostingDemo/Program.cs

@@ -1,13 +1,9 @@
 using CustomHostingDemo.Logging;
-using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.Logging;
 using Ninject;
-using System;
 using System.CommandLine;
 using System.CommandLine.Builder;
 using System.CommandLine.Parsing;
-using System.IO;
-using System.Reflection;
 
 namespace CustomHostingDemo
 {
@@ -16,21 +12,13 @@ namespace CustomHostingDemo
         static int Main(string[] args)
         {
             var kernel = new StandardKernel(
+                new ConfigurationModule(),
                 new LoggingModule(),
                 new CommandHandlerModule()
             );
 
-            var config = new ConfigurationBuilder()
-                .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
-                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
-                .AddJsonFile(Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), "custom-demo.json"), optional: true, reloadOnChange: false)
-                .Build();
-
-            kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
-
             var logger = kernel.Get<Logger<Program>>();
-            logger.LogInformation($"Informational Message - Hello {config["Hello"]}");
-            logger.LogWarning("Ooopsie!");
+            logger.LogWarning("Hello");
 
             var rootCommand = new RootCommand()
             {