-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathConfigUtils.cs
More file actions
83 lines (72 loc) · 2.48 KB
/
Copy pathConfigUtils.cs
File metadata and controls
83 lines (72 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) One Identity LLC. All rights reserved.
namespace SampleA2aService;
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using Serilog;
using Serilog.Events;
internal static class ConfigUtils
{
public static string ReadRequiredSettingFromAppConfig(string key, string description)
{
try
{
var value = ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value))
{
return value;
}
Log.Error("{Key} is required in App.Config", key);
throw new InvalidOperationException($"Unable to start SampleA2aService with empty {description}.");
}
catch (ConfigurationErrorsException ex)
{
Log.Error(ex, "{Key} is required in App.Config", key);
throw new InvalidOperationException($"Unable to start SampleA2aService without {description}.", ex);
}
}
public static string ReadSettingFromAppConfigIfPresent(string key)
{
try
{
var value = ConfigurationManager.AppSettings[key];
return !string.IsNullOrEmpty(value) ? value : null;
}
catch (ConfigurationErrorsException)
{
return null;
}
}
public static void ConfigureLogging()
{
var logConfig = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Console();
var loggingDirectory = ReadSettingFromAppConfigIfPresent("LoggingDirectory");
if (loggingDirectory != null)
{
if (!Path.IsPathRooted(loggingDirectory))
{
loggingDirectory = Path.Combine(Assembly.GetEntryAssembly().Location, loggingDirectory);
}
logConfig.WriteTo.File(Path.Combine(loggingDirectory, "SampleA2aService-{Date}.log"),
LogEventLevel.Debug);
}
Log.Logger = logConfig.CreateLogger();
}
public static void CheckForDebugHook()
{
#if DEBUG
var debugBreak = ReadSettingFromAppConfigIfPresent("DebugBreak");
if (bool.TryParse(debugBreak, out var waitForDebugger) && waitForDebugger)
{
while (!System.Diagnostics.Debugger.IsAttached)
{
System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(2)).Wait();
Log.Debug("Waiting for debugger to attach");
}
System.Diagnostics.Debugger.Break();
Log.Debug("Debugger attached");
}
#endif
}
}