mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 22:35:36 +00:00
Initial commit
This commit is contained in:
parent
e523533d17
commit
10cceacca6
50 changed files with 1280 additions and 0 deletions
43
vscode/Chapter04/Instrumenting/Program.cs
Normal file
43
vscode/Chapter04/Instrumenting/Program.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
string logPath = Path.Combine(Environment.GetFolderPath(
|
||||
Environment.SpecialFolder.DesktopDirectory), "log.txt");
|
||||
|
||||
Console.WriteLine($"Writing to: {logPath}");
|
||||
|
||||
TextWriterTraceListener logFile = new(File.CreateText(logPath));
|
||||
|
||||
Trace.Listeners.Add(logFile);
|
||||
|
||||
// text writer is buffered, so this option calls
|
||||
// Flush() on all listeners after writing
|
||||
Trace.AutoFlush = true;
|
||||
|
||||
Debug.WriteLine("Debug says, I am watching!");
|
||||
Trace.WriteLine("Trace says, I am watching!");
|
||||
|
||||
Console.WriteLine("Reading from appsettings.json in {0}",
|
||||
arg0: Directory.GetCurrentDirectory());
|
||||
|
||||
ConfigurationBuilder builder = new();
|
||||
|
||||
builder.SetBasePath(Directory.GetCurrentDirectory());
|
||||
|
||||
builder.AddJsonFile("appsettings.json",
|
||||
optional: false, reloadOnChange: true);
|
||||
|
||||
IConfigurationRoot configuration = builder.Build();
|
||||
|
||||
TraceSwitch ts = new(
|
||||
displayName: "PacktSwitch",
|
||||
description: "This switch is set via a JSON config.");
|
||||
|
||||
configuration.GetSection("PacktSwitch").Bind(ts);
|
||||
|
||||
Trace.WriteLineIf(ts.TraceError, "Trace error");
|
||||
Trace.WriteLineIf(ts.TraceWarning, "Trace warning");
|
||||
Trace.WriteLineIf(ts.TraceInfo, "Trace information");
|
||||
Trace.WriteLineIf(ts.TraceVerbose, "Trace verbose");
|
||||
|
||||
Console.ReadLine();
|
||||
Loading…
Add table
Add a link
Reference in a new issue