Skip to content

Commit eec089d

Browse files
Adds debug logging to config manager
Adds debug logging to the service config manager to assist with troubleshooting configuration loading issues. This change adds logging for scenarios where the config file is empty, not found, or fails to load. Improves diagnostic logging and fixes typo Updates logging to use Console.WriteLine instead of Debug.WriteLine for better visibility. Includes server config status in HTTP challenge server logs. Corrects a typo in the CLI help text for backup import. Handles exceptions when loading service config Improves the robustness of the service configuration loading process by catching potential exceptions. Specifically, it catches exceptions when attempting to access the AppData path and unauthorized access exceptions when reading the service configuration file. This prevents the service from crashing and provides more informative error messages to the user. Fixes server connection configuration. Ensures the client uses the provided connection configuration or defaults to the service configuration. Adds logging to display the service configuration for debugging. Adds debug logging for config loading Adds logging statements to the API client and service config manager to aid in debugging configuration loading issues. This helps track the base URI and service config file paths used by the application.
1 parent 592e7ae commit eec089d

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/Certify.CLI/CertifyCLI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal void ShowHelp()
108108
System.Console.WriteLine("certify credential list : list current stored credential summary information");
109109
System.Console.WriteLine("certify activate <email address> <key> : activate your Certify The Web install using your license key");
110110
System.Console.WriteLine("certify backup export <directory or full filename> <encryption secret> : export a backup file (autonamed if a directory) using the given secret password for encryption.");
111-
System.Console.WriteLine("certify backup import preview <full filename> <encryption secret> : import a backup file using the given secret password for encryption. 'preview' is optional and us used to test a backup without importing anything.");
111+
System.Console.WriteLine("certify backup import preview <full filename> <encryption secret> : import a backup file using the given secret password for encryption. 'preview' is optional and is used to test a backup without importing anything.");
112112
System.Console.WriteLine("\n\n");
113113
System.Console.WriteLine("For help, see the docs at https://docs.certifytheweb.com");
114114

src/Certify.Client/CertifyApiClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ public partial class CertifyApiClient : ICertifyInternalApiClient
7171
internal string _accessToken { get; set; } = "";
7272
internal string _refreshToken { get; set; } = "";
7373

74-
public CertifyApiClient(Providers.IServiceConfigProvider configProvider, Shared.ServerConnection config = null)
74+
public CertifyApiClient(Providers.IServiceConfigProvider configProvider, Shared.ServerConnection connectionConfig = null)
7575
{
7676
_configProvider = configProvider;
77-
_connectionConfig = config ?? GetDefaultServerConnection();
77+
78+
_connectionConfig = connectionConfig ?? new ServerConnection(configProvider.GetServiceConfig());
7879

7980
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}" + _baseUri;
8081

82+
System.Console.WriteLine($"CertifyApiClient: using baseURI {_baseUri}");
8183
CreateHttpClient();
8284

8385
}
@@ -129,6 +131,9 @@ private void SetClientAuthorizationBearerToken()
129131
public ServerConnection GetDefaultServerConnection()
130132
{
131133
var serviceCfg = _configProvider.GetServiceConfig();
134+
135+
System.Console.WriteLine($"CertifyApiClient: GetDefaultServerConnection service config: {serviceCfg?.Host}:{serviceCfg?.Port} ({serviceCfg?.UseHTTPS} {serviceCfg.ServiceFaultMsg})");
136+
132137
return new ServerConnection(serviceCfg);
133138
}
134139

src/Certify.Models/Shared/ServerConnection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Newtonsoft.Json;
23

34
namespace Certify.Shared
45
{
@@ -39,6 +40,9 @@ public ServerConnection(ServiceConfig config)
3940
Mode = "direct";
4041
Authentication = "default";
4142
IsDefault = true;
43+
44+
System.Console.WriteLine($"ServerConnection: Created with config: {JsonConvert.SerializeObject(config)}");
45+
System.Console.WriteLine($"ServerConnection: Connection: {JsonConvert.SerializeObject(this)}");
4246
}
4347

4448
public override string ToString()

src/Certify.Shared/Utils/HttpChallengeServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public bool Start(ServiceConfig serverConfig, string controlKey = null, string c
146146

147147
Log($"Http Challenge Server Started: {uriPrefix}", true);
148148
Log($"Control Key: {_controlKey}: Check Key: {_checkKey}");
149-
Log($"Using Internal API: {_baseUri}");
149+
Log($"Using Internal API: {_baseUri} {serverConfig.ConfigStatus}");
150150

151151
_ = Task.Run(ServerTask);
152152

src/Certify.Shared/Utils/ServiceConfigManager.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,76 @@ public static ServiceConfig GetAppServiceConfig()
2020
ConfigStatus = ConfigStatus.DefaultFailed
2121
};
2222

23-
var appDataPath = EnvironmentUtil.EnsuredAppDataPath();
23+
var appDataPath = string.Empty;
24+
try
25+
{
26+
appDataPath = EnvironmentUtil.EnsuredAppDataPath();
27+
System.Console.WriteLine($"ServiceConfigManager: using AppData path. {appDataPath}");
28+
}
29+
catch (Exception exp)
30+
{
31+
System.Console.WriteLine($"ServiceConfigManager: Failed to get AppData path. {exp.Message}");
32+
serviceConfig.ServiceFaultMsg = $"Failed to get AppData path. {exp.Message}";
33+
return serviceConfig;
34+
}
35+
2436
var serviceConfigFile = Path.Combine(appDataPath, "serviceconfig.json");
2537
#if DEBUG
2638
serviceConfigFile = Path.Combine(appDataPath, "serviceconfig.debug.json");
2739
#endif
40+
41+
System.Console.WriteLine($"ServiceConfigManager: using service config file. {serviceConfigFile}");
2842
try
2943
{
3044
if (File.Exists(serviceConfigFile))
3145
{
46+
System.Console.WriteLine($"ServiceConfigManager: config exists. {serviceConfigFile}");
3247
var config = File.ReadAllText(serviceConfigFile);
3348
if (!string.IsNullOrWhiteSpace(config))
3449
{
3550
serviceConfig = JsonConvert.DeserializeObject<ServiceConfig>(config);
51+
52+
System.Console.WriteLine($"ServiceConfigManager: Loaded {config}");
53+
System.Console.WriteLine($"ServiceConfigManager: Parsed {JsonConvert.SerializeObject(serviceConfig)}");
54+
}
55+
else
56+
{
57+
System.Console.WriteLine($"ServiceConfigManager: Empty service config found at {serviceConfigFile}");
3658
}
3759

3860
serviceConfig.ConfigStatus = ConfigStatus.NotModified;
3961
}
4062
else
4163
{
4264
serviceConfig.ConfigStatus = ConfigStatus.New;
65+
66+
System.Console.WriteLine($"ServiceConfigManager: No service config found at {serviceConfigFile}");
67+
4368
}
4469
}
70+
catch (UnauthorizedAccessException uaExp)
71+
{
72+
serviceConfig.ConfigStatus = ConfigStatus.DefaultFailed;
73+
serviceConfig.ServiceFaultMsg = $"Access denied to service configuration file at {serviceConfigFile}. {uaExp.Message}";
74+
System.Console.WriteLine($"ServiceConfigManager: {serviceConfig.ServiceFaultMsg}");
75+
}
4576
catch (Exception exp)
4677
{
4778
if (serviceConfig != null)
4879
{
4980
serviceConfig.ConfigStatus = ConfigStatus.DefaultFailed;
5081
serviceConfig.ServiceFaultMsg = $"There was a problem loading the service configuration from {serviceConfigFile} {exp.Message}";
5182
}
83+
else
84+
{
85+
System.Diagnostics.Debug.WriteLine($"ServiceConfigManager: Fault loading service config found at {exp}");
86+
}
5287
}
5388

5489
// if something went wrong, default to standard config
5590
if (serviceConfig == null)
5691
{
92+
System.Console.WriteLine($"ServiceConfigManager: Falling back to default service config.");
5793
serviceConfig = new ServiceConfig()
5894
{
5995
ConfigStatus = ConfigStatus.DefaultFailed

0 commit comments

Comments
 (0)