From 4ae6c29c771e1ce087c15ba6079c19d2f9f44b1e Mon Sep 17 00:00:00 2001 From: yophilav <54859653+yophilav@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:08:17 -0800 Subject: [PATCH] =?UTF-8?q?Ensure=20edge-agent=20and=20edge-hub=20use=20lo?= =?UTF-8?q?gger=20when=20logging=20exception=20stac=E2=80=A6=20(#7233)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cherry-picked: https://github.com/Azure/iotedge/commit/ce74e43181d4639e525830542dcf1b179121f200 Ensure edge-agent and edge-hub use logger when logging exception stack traces. --- .../Program.cs | 23 +++++--- .../Program.cs | 53 +++++++++++++------ 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Service/Program.cs b/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Service/Program.cs index dc14b24e26b..415b415f863 100644 --- a/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Service/Program.cs +++ b/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Service/Program.cs @@ -39,6 +39,8 @@ public class Program public static int Main() { Console.WriteLine($"{DateTime.UtcNow.ToLogString()} Edge Agent Main()"); + ILogger logger = null; + try { IConfigurationRoot configuration = new ConfigurationBuilder() @@ -46,20 +48,29 @@ public static int Main() .AddEnvironmentVariables() .Build(); - return MainAsync(configuration).Result; + // Bring up the logger before anything else so we can log errors ASAP + logger = SetupLogger(configuration); + + return MainAsync(configuration, logger).Result; } catch (Exception ex) { - Console.Error.WriteLine(ex); + if (logger != null) + { + logger.LogDebug(ex, "An unhandled exception occurred"); + } + else + { + // Fallback if the logger hasn't been set up, should pretty much never happen + Console.Error.WriteLine(ex); + } + return 1; } } - public static async Task MainAsync(IConfiguration configuration) + public static async Task MainAsync(IConfiguration configuration, ILogger logger) { - // Bring up the logger before anything else so we can log errors ASAP - ILogger logger = SetupLogger(configuration); - logger.LogInformation("Initializing Edge Agent."); VersionInfo versionInfo = VersionInfo.Get(VersionInfoFileName); diff --git a/edge-hub/core/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs b/edge-hub/core/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs index 3c1e99c67ef..5ca0a281b93 100644 --- a/edge-hub/core/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs +++ b/edge-hub/core/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs @@ -31,28 +31,47 @@ public class Program public static int Main() { - Console.WriteLine($"{DateTime.UtcNow.ToLogString()} Edge Hub Main()"); - IConfigurationRoot configuration = new ConfigurationBuilder() - .AddJsonFile(Constants.ConfigFileName) - .AddEnvironmentVariables() - .Build(); + ILogger logger = null; - return MainAsync(configuration).Result; - } + try + { + Console.WriteLine($"{DateTime.UtcNow.ToLogString()} Edge Hub Main()"); + IConfigurationRoot configuration = new ConfigurationBuilder() + .AddJsonFile(Constants.ConfigFileName) + .AddEnvironmentVariables() + .Build(); - static async Task MainAsync(IConfigurationRoot configuration) - { - string logLevel = configuration.GetValue($"{Logger.RuntimeLogLevelEnvKey}", "info"); - Logger.SetLogLevel(logLevel); + string logLevel = configuration.GetValue($"{Logger.RuntimeLogLevelEnvKey}", "info"); + Logger.SetLogLevel(logLevel); - // Set the LoggerFactory used by the Routing code. - if (configuration.GetValue("EnableRoutingLogging", false)) - { - Routing.LoggerFactory = Logger.Factory; + // Set the LoggerFactory used by the Routing code. + if (configuration.GetValue("EnableRoutingLogging", false)) + { + Routing.LoggerFactory = Logger.Factory; + } + + logger = Logger.Factory.CreateLogger("EdgeHub"); + + return MainAsync(configuration, logger).Result; } + catch (Exception ex) + { + if (logger != null) + { + logger.LogDebug(ex, "An unhandled exception occurred"); + } + else + { + // Fallback if the logger hasn't been set up, should pretty much never happen + Console.Error.WriteLine(ex); + } - ILogger logger = Logger.Factory.CreateLogger("EdgeHub"); + return 1; + } + } + static async Task MainAsync(IConfigurationRoot configuration, ILogger logger) + { try { EdgeHubCertificates certificates = await EdgeHubCertificates.LoadAsync(configuration, logger); @@ -165,7 +184,7 @@ static async Task MainAsync(IConfigurationRoot configuration) catch (Exception ex) { logger.LogError(ex, "Stopping with exception"); - throw; + return 1; } return 0;