Parcourir la source

Initial commit

Lukas Angerer il y a 4 ans
commit
f3085b3f75

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+bin/
+obj/
+.vs/

+ 25 - 0
CustomHostingDemo.sln

@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.31229.75
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomHostingDemo", "CustomHostingDemo\CustomHostingDemo.csproj", "{7080E6AE-53AF-47F7-8710-9337E8ECAA93}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{7080E6AE-53AF-47F7-8710-9337E8ECAA93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{7080E6AE-53AF-47F7-8710-9337E8ECAA93}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{7080E6AE-53AF-47F7-8710-9337E8ECAA93}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{7080E6AE-53AF-47F7-8710-9337E8ECAA93}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {AF71F06E-50C5-40A6-8E8A-4E578B75A4BB}
+	EndGlobalSection
+EndGlobal

+ 21 - 0
CustomHostingDemo/CustomHostingDemo.csproj

@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net5.0</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
+    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
+    <PackageReference Include="Ninject" Version="3.3.4" />
+    <PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <None Update="appsettings.json">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
+</Project>

+ 30 - 0
CustomHostingDemo/Logging/ConsoleLogger.cs

@@ -0,0 +1,30 @@
+using Microsoft.Extensions.Logging;
+using System;
+
+namespace CustomHostingDemo.Logging
+{
+    public class ConsoleLogger : ILogger
+    {
+        public string Name { get; }
+
+        public ConsoleLogger(string name)
+        {
+            Name = name;
+        }
+
+        public IDisposable BeginScope<TState>(TState state)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool IsEnabled(LogLevel logLevel)
+        {
+            return logLevel != LogLevel.None;
+        }
+
+        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
+        {
+            Console.WriteLine($"[{logLevel}] {formatter(state, exception)}");
+        }
+    }
+}

+ 16 - 0
CustomHostingDemo/Logging/LoggerProvider.cs

@@ -0,0 +1,16 @@
+using Microsoft.Extensions.Logging;
+
+namespace CustomHostingDemo.Logging
+{
+    public class LoggerProvider : ILoggerProvider
+    {
+        public ILogger CreateLogger(string categoryName)
+        {
+            return new ConsoleLogger(categoryName);
+        }
+
+        public void Dispose()
+        {
+        }
+    }
+}

+ 34 - 0
CustomHostingDemo/Program.cs

@@ -0,0 +1,34 @@
+using CustomHostingDemo.Logging;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
+using Ninject;
+using System;
+using System.IO;
+using System.Reflection;
+
+namespace CustomHostingDemo
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var kernel = new StandardKernel();
+
+            var config = new ConfigurationBuilder()
+                .SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
+                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
+                .Build();
+
+            kernel.Bind<IConfiguration>().ToConstant(config).InSingletonScope();
+            kernel.Bind<ILoggerFactory>().To<LoggerFactory>().InSingletonScope();
+            kernel.Bind(typeof(Logger<>)).ToSelf().InSingletonScope();
+            kernel.Bind<ILoggerProvider>().To<LoggerProvider>().InSingletonScope();
+
+            Console.WriteLine($"Hello {config["Hello"]}");
+
+            var logger = kernel.Get<Logger<Program>>();
+            logger.LogInformation("Informational Message");
+            logger.LogWarning("Ooopsie!");
+        }
+    }
+}

+ 3 - 0
CustomHostingDemo/appsettings.json

@@ -0,0 +1,3 @@
+{
+  "Hello":  "World!"
+}