Created
November 16, 2017 19:49
-
-
Save LindaLawton/e0c5d50864934aa5f2dfa45be3867c67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.IO; | |
namespace Daimto.DotnetCore.ConfigFiles | |
{ | |
class Program | |
{ | |
//https://stackoverflow.com/questions/44838424/wiring-and-injected-nlog-into-a-net-core-console-application | |
public static IConfigurationRoot Configuration { get; set; } | |
static void Main(string[] args) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); | |
Configuration = builder.Build(); | |
//setup our DI | |
var serviceProvider = new ServiceCollection() | |
.AddLogging() | |
.AddSingleton<IConfigurationRoot>(Configuration) | |
.BuildServiceProvider(); | |
//configure console logging | |
serviceProvider | |
.GetService<ILoggerFactory>() | |
.AddConsole(LogLevel.Debug); | |
var logger = serviceProvider.GetService<ILoggerFactory>() | |
.CreateLogger<Program>(); | |
logger.LogDebug("Starting application"); | |
Console.WriteLine($"Application name: {Configuration["ApplicationName"]} version: {Configuration["Version"]}"); | |
Console.WriteLine($"ServerPath: {Configuration["Settings:ServerPath"]} TokenExperation: {Configuration["Settings:TokenExperation"]}"); | |
TestClass.hello(); | |
Console.ReadLine(); | |
} | |
} | |
public class MyConfig | |
{ | |
public string ApplicationName { get; set; } | |
public int Version { get; set; } | |
public class Settings | |
{ | |
public string ServerPath { get; set; } | |
public int TokenExperation { get; set; } | |
} | |
} | |
public class TestClass { | |
public static void hello() { | |
Console.WriteLine("Hello from test class "); | |
Console.WriteLine($"ServerPath: {Program.Configuration["Settings:ServerPath"]} TokenExperation: {Program.Configuration["Settings:TokenExperation"]}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment