From aa85155c037b464c9a3eed32043c1694ba220431 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Thu, 29 Jun 2023 08:12:32 +0100 Subject: [PATCH] Improved code for reading appsettings.json --- vs4win/Chapter04/Instrumenting/Program.cs | 23 +++++++++++++++++++++-- vscode/Chapter04/Instrumenting/Program.cs | 23 +++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/vs4win/Chapter04/Instrumenting/Program.cs b/vs4win/Chapter04/Instrumenting/Program.cs index 4eaf455..69ed96a 100644 --- a/vs4win/Chapter04/Instrumenting/Program.cs +++ b/vs4win/Chapter04/Instrumenting/Program.cs @@ -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 + ()?.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(); diff --git a/vscode/Chapter04/Instrumenting/Program.cs b/vscode/Chapter04/Instrumenting/Program.cs index 4eaf455..69ed96a 100644 --- a/vscode/Chapter04/Instrumenting/Program.cs +++ b/vscode/Chapter04/Instrumenting/Program.cs @@ -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 + ()?.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();