Skip to content

Commit 75b9e42

Browse files
Improve remote instance serialization and exception logging in unattended installation (#5336)
* Improve exception logging in unattended installers * Switch to source-generated JSON serialization for `RemoteInstanceConverter` --------- Co-authored-by: Daniel Marbach <danielmarbach@users.noreply.github.com>
1 parent 26a20cd commit 75b9e42

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

src/ServiceControlInstaller.Engine/Configuration/ServiceControl/RemoteInstanceConverter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66

77
public static class RemoteInstanceConverter
88
{
9-
public static List<RemoteInstanceSetting> FromJson(string json) => JsonSerializer.Deserialize<List<RemoteInstanceSetting>>(json, Options);
9+
public static List<RemoteInstanceSetting> FromJson(string json) => JsonSerializer.Deserialize(json, RemoteInstanceContext.Default.ListRemoteInstanceSetting);
1010

11-
public static string ToJson(IList<RemoteInstanceSetting> settings) => JsonSerializer.Serialize(settings, Options);
12-
13-
static readonly JsonSerializerOptions Options = new()
14-
{
15-
WriteIndented = false,
16-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
17-
};
11+
public static string ToJson(List<RemoteInstanceSetting> settings) => JsonSerializer.Serialize(settings, RemoteInstanceContext.Default.ListRemoteInstanceSetting);
1812
}
1913

14+
[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
15+
[JsonSerializable(typeof(List<RemoteInstanceSetting>))]
16+
public partial class RemoteInstanceContext : JsonSerializerContext;
17+
2018
public class RemoteInstanceSetting
2119
{
2220
[JsonPropertyName("api_uri")]

src/ServiceControlInstaller.Engine/Unattended/UnattendAuditInstaller.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task<bool> Add(ServiceControlAuditNewInstance details, Func<PathInf
7070
}
7171
catch (Exception ex)
7272
{
73-
logger.Error(ex.Message);
73+
logger.Error("Error installing the service: {0}", ex);
7474
return false;
7575
}
7676

@@ -137,7 +137,7 @@ public bool Upgrade(ServiceControlAuditInstance instance, bool force)
137137
}
138138
catch (Exception ex)
139139
{
140-
logger.Error("Upgrade Failed: {0}", ex.Message);
140+
logger.Error("Upgrade Failed: {0}", ex);
141141
return false;
142142
}
143143

src/ServiceControlInstaller.Engine/Unattended/UnattendMonitoringInstaller.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task<bool> Add(MonitoringNewInstance details, Func<PathInfo, Task<b
6969
}
7070
catch (Exception ex)
7171
{
72-
logger.Error(ex.Message);
72+
logger.Error("Error installing the service: {0}", ex);
7373
return false;
7474
}
7575

@@ -130,7 +130,7 @@ public bool Upgrade(MonitoringInstance instance)
130130
}
131131
catch (Exception ex)
132132
{
133-
logger.Error("Upgrade Failed: {0}", ex.Message);
133+
logger.Error("Upgrade Failed: {0}", ex);
134134
return false;
135135
}
136136

@@ -178,7 +178,7 @@ internal async Task<bool> Update(MonitoringInstance instance, bool startService)
178178
}
179179
catch (Exception ex)
180180
{
181-
logger.Error("Update failed: {0}", ex.Message);
181+
logger.Error("Update failed: {0}", ex);
182182
return false;
183183
}
184184

src/ServiceControlInstaller.Engine/Unattended/UnattendServiceControlInstaller.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task<bool> Add(ServiceControlNewInstance details, Func<PathInfo, Ta
6161
}
6262
catch (Exception ex)
6363
{
64-
logger.Error(ex.Message);
64+
logger.Error("Error installing the service: {0}", ex);
6565
return false;
6666
}
6767

@@ -131,7 +131,7 @@ public bool Upgrade(ServiceControlInstance instance, ServiceControlUpgradeOption
131131
}
132132
catch (Exception ex)
133133
{
134-
logger.Error("Upgrade Failed: {0}", ex.Message);
134+
logger.Error("Upgrade Failed: {0}", ex);
135135
return false;
136136
}
137137
finally
@@ -175,7 +175,7 @@ internal async Task<bool> Update(ServiceControlInstance instance, bool startServ
175175
}
176176
catch (Exception ex)
177177
{
178-
logger.Error("Update failed: {0}", ex.Message);
178+
logger.Error("Update failed: {0}", ex);
179179
return false;
180180
}
181181
finally
@@ -270,7 +270,7 @@ public bool AddRemoteInstance(ServiceControlInstance instance, string[] remoteIn
270270
}
271271
catch (Exception ex)
272272
{
273-
logger.Error("Adding remote instances Failed: {0}", ex.Message);
273+
logger.Error("Adding remote instances Failed: {0}", ex);
274274
return false;
275275
}
276276
finally
@@ -310,7 +310,7 @@ public bool RemoveRemoteInstance(ServiceControlInstance instance, string[] remot
310310
}
311311
catch (Exception ex)
312312
{
313-
logger.Error("Removing remote instances Failed: {0}", ex.Message);
313+
logger.Error("Removing remote instances Failed: {0}", ex);
314314
return false;
315315
}
316316
finally

0 commit comments

Comments
 (0)