mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
Improved code for reading appsettings.json
This commit is contained in:
parent
8d7bba1aec
commit
aa85155c03
|
|
@ -1,6 +1,13 @@
|
|||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
Console.WriteLine("Warning! Versions 7.0.3xx and 7.0.4xx have bugs that cause an exception:");
|
||||
Console.WriteLine("Microsoft.Extensions.Configuration.Binder version: {0}",
|
||||
typeof(ConfigurationBinder).Assembly.GetCustomAttribute
|
||||
<AssemblyFileVersionAttribute>()?.Version);
|
||||
Console.WriteLine();
|
||||
|
||||
string logPath = Path.Combine(Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.DesktopDirectory), "log.txt");
|
||||
|
||||
|
|
@ -10,8 +17,8 @@ TextWriterTraceListener logFile = new(File.CreateText(logPath));
|
|||
|
||||
Trace.Listeners.Add(logFile);
|
||||
|
||||
// text writer is buffered, so this option calls
|
||||
// Flush() on all listeners after writing
|
||||
// Text writer is buffered, so this option calls
|
||||
// Flush() on all listeners after writing.
|
||||
Trace.AutoFlush = true;
|
||||
|
||||
Debug.WriteLine("Debug says, I am watching!");
|
||||
|
|
@ -20,10 +27,18 @@ Trace.WriteLine("Trace says, I am watching!");
|
|||
Console.WriteLine("Reading from appsettings.json in {0}",
|
||||
arg0: Directory.GetCurrentDirectory());
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("--appsettings.json contents--");
|
||||
Console.WriteLine(File.ReadAllText(Path.Combine(
|
||||
Directory.GetCurrentDirectory(), "appsettings.json")));
|
||||
|
||||
ConfigurationBuilder builder = new();
|
||||
|
||||
builder.SetBasePath(Directory.GetCurrentDirectory());
|
||||
|
||||
// Add the appsettings.json file to the processed configuration.
|
||||
// Make reading this file mandatory so an exception will be thrown
|
||||
// if the file is not found.
|
||||
builder.AddJsonFile("appsettings.json",
|
||||
optional: false, reloadOnChange: true);
|
||||
|
||||
|
|
@ -35,6 +50,9 @@ TraceSwitch ts = new(
|
|||
|
||||
configuration.GetSection("PacktSwitch").Bind(ts);
|
||||
|
||||
// Output the trace switch level from appsettings.json.
|
||||
Console.WriteLine($"Trace switch level: {ts.Level}");
|
||||
|
||||
Trace.WriteLineIf(ts.TraceError, "Trace error");
|
||||
Trace.WriteLineIf(ts.TraceWarning, "Trace warning");
|
||||
Trace.WriteLineIf(ts.TraceInfo, "Trace information");
|
||||
|
|
@ -43,4 +61,5 @@ Trace.WriteLineIf(ts.TraceVerbose, "Trace verbose");
|
|||
int unitsInStock = 12;
|
||||
LogSourceDetails(unitsInStock > 10);
|
||||
|
||||
Console.WriteLine("Press enter to exit.");
|
||||
Console.ReadLine();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
Console.WriteLine("Warning! Versions 7.0.3xx and 7.0.4xx have bugs that cause an exception:");
|
||||
Console.WriteLine("Microsoft.Extensions.Configuration.Binder version: {0}",
|
||||
typeof(ConfigurationBinder).Assembly.GetCustomAttribute
|
||||
<AssemblyFileVersionAttribute>()?.Version);
|
||||
Console.WriteLine();
|
||||
|
||||
string logPath = Path.Combine(Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.DesktopDirectory), "log.txt");
|
||||
|
||||
|
|
@ -10,8 +17,8 @@ TextWriterTraceListener logFile = new(File.CreateText(logPath));
|
|||
|
||||
Trace.Listeners.Add(logFile);
|
||||
|
||||
// text writer is buffered, so this option calls
|
||||
// Flush() on all listeners after writing
|
||||
// Text writer is buffered, so this option calls
|
||||
// Flush() on all listeners after writing.
|
||||
Trace.AutoFlush = true;
|
||||
|
||||
Debug.WriteLine("Debug says, I am watching!");
|
||||
|
|
@ -20,10 +27,18 @@ Trace.WriteLine("Trace says, I am watching!");
|
|||
Console.WriteLine("Reading from appsettings.json in {0}",
|
||||
arg0: Directory.GetCurrentDirectory());
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("--appsettings.json contents--");
|
||||
Console.WriteLine(File.ReadAllText(Path.Combine(
|
||||
Directory.GetCurrentDirectory(), "appsettings.json")));
|
||||
|
||||
ConfigurationBuilder builder = new();
|
||||
|
||||
builder.SetBasePath(Directory.GetCurrentDirectory());
|
||||
|
||||
// Add the appsettings.json file to the processed configuration.
|
||||
// Make reading this file mandatory so an exception will be thrown
|
||||
// if the file is not found.
|
||||
builder.AddJsonFile("appsettings.json",
|
||||
optional: false, reloadOnChange: true);
|
||||
|
||||
|
|
@ -35,6 +50,9 @@ TraceSwitch ts = new(
|
|||
|
||||
configuration.GetSection("PacktSwitch").Bind(ts);
|
||||
|
||||
// Output the trace switch level from appsettings.json.
|
||||
Console.WriteLine($"Trace switch level: {ts.Level}");
|
||||
|
||||
Trace.WriteLineIf(ts.TraceError, "Trace error");
|
||||
Trace.WriteLineIf(ts.TraceWarning, "Trace warning");
|
||||
Trace.WriteLineIf(ts.TraceInfo, "Trace information");
|
||||
|
|
@ -43,4 +61,5 @@ Trace.WriteLineIf(ts.TraceVerbose, "Trace verbose");
|
|||
int unitsInStock = 12;
|
||||
LogSourceDetails(unitsInStock > 10);
|
||||
|
||||
Console.WriteLine("Press enter to exit.");
|
||||
Console.ReadLine();
|
||||
|
|
|
|||
Loading…
Reference in a new issue