Skip to content

Commit

Permalink
Improved code for reading appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
markjprice committed Jun 29, 2023
1 parent 8d7bba1 commit aa85155
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
23 changes: 21 additions & 2 deletions vs4win/Chapter04/Instrumenting/Program.cs
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -10,8 +17,8 @@

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!");
Expand All @@ -20,10 +27,18 @@
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);

Expand All @@ -35,6 +50,9 @@

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");
Expand All @@ -43,4 +61,5 @@
int unitsInStock = 12;
LogSourceDetails(unitsInStock > 10);

Console.WriteLine("Press enter to exit.");
Console.ReadLine();
23 changes: 21 additions & 2 deletions vscode/Chapter04/Instrumenting/Program.cs
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -10,8 +17,8 @@

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!");
Expand All @@ -20,10 +27,18 @@
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);

Expand All @@ -35,6 +50,9 @@

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");
Expand All @@ -43,4 +61,5 @@
int unitsInStock = 12;
LogSourceDetails(unitsInStock > 10);

Console.WriteLine("Press enter to exit.");
Console.ReadLine();

0 comments on commit aa85155

Please sign in to comment.