From 0c051a9ab714d5619eb185a24ef0b5bf6a79938c Mon Sep 17 00:00:00 2001 From: Bogdan Zavu Date: Thu, 30 Mar 2023 17:05:12 -0400 Subject: [PATCH 1/2] restrict logging in service mode --- src/DynamoCLI/Program.cs | 4 +-- src/DynamoCore/Logging/DynamoLogger.cs | 45 +++++++++++++++++++++++++- src/DynamoCore/Models/DynamoModel.cs | 8 ++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/DynamoCLI/Program.cs b/src/DynamoCLI/Program.cs index 4d11087f13f..59a2c9e11bd 100644 --- a/src/DynamoCLI/Program.cs +++ b/src/DynamoCLI/Program.cs @@ -86,9 +86,9 @@ private static void RunKeepAlive(StartupUtils.CommandLineArguments cmdLineArgs) Console.WriteLine("Press Enter to shutdown..."); } } - catch + catch(Exception ex) { - Console.WriteLine("Server is shutting down due to an error"); + Console.WriteLine("Server is shutting down due to an error : " + ex.ToString()); } } diff --git a/src/DynamoCore/Logging/DynamoLogger.cs b/src/DynamoCore/Logging/DynamoLogger.cs index 8744f78dc7f..d355443da7d 100644 --- a/src/DynamoCore/Logging/DynamoLogger.cs +++ b/src/DynamoCore/Logging/DynamoLogger.cs @@ -77,6 +77,7 @@ public class DynamoLogger: NotificationObject, ILogger, IDisposable private bool _isDisposed; private readonly bool testMode; private readonly bool cliMode; + private readonly bool serviceMode; private TextWriter FileWriter { get; set; } private StringBuilder ConsoleWriter { get; set; } @@ -213,6 +214,41 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean is } } + /// + /// Initializes a new instance of class + /// with specified debug settings and directory where to write logs + /// + /// Debug settings + /// Directory path where log file will be written + /// Test mode is true or false. + /// We want to allow logging when CLI mode is true even if we are in test mode. + /// We want restrict logging in service mode to console only due to lambda limitations. + public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean isTestMode, Boolean isCLIMode, Boolean isServiceMode) + : this(debugSettings, logDirectory, isTestMode) + { + lock (guardMutex) + { + this.debugSettings = debugSettings; + _isDisposed = false; + + WarningLevel = WarningLevel.Mild; + Warning = ""; + + notifications = new List(); + + testMode = isTestMode; + cliMode = isCLIMode; + serviceMode = isServiceMode; + + if (!testMode && !isServiceMode) + { + StartLoggingToConsoleAndFile(logDirectory); + } + + XmlDocumentationExtensions.LogToConsole += Log; + } + } + /// /// Logs the specified message. /// @@ -244,6 +280,12 @@ private void Log(string message, LogLevel level, bool reportModification) return; } + if (serviceMode && (level == LogLevel.Console || level == LogLevel.File)) + { + ConsoleWriter.AppendLine("LogLevel switched to ConsoleOnly in service mode"); + level = LogLevel.ConsoleOnly; + } + switch (level) { //write to the console only @@ -444,7 +486,8 @@ private void StartLoggingToConsoleAndFile(string logDirectory) { lock (this.guardMutex) { - if (FileWriter != null && ConsoleWriter != null) + if (serviceMode || + (FileWriter != null && ConsoleWriter != null)) { return; } diff --git a/src/DynamoCore/Models/DynamoModel.cs b/src/DynamoCore/Models/DynamoModel.cs index b5c68726e84..226774c0f6f 100644 --- a/src/DynamoCore/Models/DynamoModel.cs +++ b/src/DynamoCore/Models/DynamoModel.cs @@ -648,7 +648,7 @@ protected DynamoModel(IStartConfiguration config) IsHeadless = config.IsHeadless; DebugSettings = new DebugSettings(); - Logger = new DynamoLogger(DebugSettings, pathManager.LogDirectory, IsTestMode, CLIMode); + Logger = new DynamoLogger(DebugSettings, pathManager.LogDirectory, IsTestMode, CLIMode, IsServiceMode); if (!IsServiceMode) { @@ -2418,6 +2418,12 @@ private void RegisterHomeWorkspace(HomeWorkspaceModel newWorkspace) /// protected void SaveBackupFiles(object state) { + //No backup files in ServiceMode due to Lambda restrictions + if (IsServiceMode) + { + return; + } + OnRequestDispatcherBeginInvoke(() => { // tempDict stores the list of backup files and their corresponding workspaces IDs From fdd242f336a6c25015f1678b60f0ce038fba9f17 Mon Sep 17 00:00:00 2001 From: Bogdan Zavu Date: Mon, 3 Apr 2023 16:18:00 -0400 Subject: [PATCH 2/2] add todo --- src/DynamoCore/Logging/DynamoLogger.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DynamoCore/Logging/DynamoLogger.cs b/src/DynamoCore/Logging/DynamoLogger.cs index d355443da7d..b9f04100bc2 100644 --- a/src/DynamoCore/Logging/DynamoLogger.cs +++ b/src/DynamoCore/Logging/DynamoLogger.cs @@ -160,7 +160,7 @@ public IEnumerable StartupNotifications /// /// Debug settings /// Directory path where log file will be written - [Obsolete("This will be removed in 3.0, please use DynamoLogger(debugSettings, logDirectory, isTestMode) instead.")] + [Obsolete("This will be removed in 3.0, please use DynamoLogger(debugSettings, logDirectory, isTestMode, isCLIMode, isServiceMode) instead.")] public DynamoLogger(DebugSettings debugSettings, string logDirectory) : this(debugSettings, logDirectory, false) { @@ -173,6 +173,7 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory) : this(deb /// Debug settings /// Directory path where log file will be written /// Test mode is true or false. + [Obsolete("This will be removed in 3.0, please use DynamoLogger(debugSettings, logDirectory, isTestMode, isCLIMode, isServiceMode) instead.")] public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean isTestMode) { lock (guardMutex) @@ -204,6 +205,7 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean is /// Directory path where log file will be written /// Test mode is true or false. /// We want to allow logging when CLI mode is true even if we are in test mode. + [Obsolete("This will be removed in 3.0, please use DynamoLogger(debugSettings, logDirectory, isTestMode, isCLIMode, isServiceMode) instead.")] public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean isTestMode, Boolean isCLIMode) :this(debugSettings, logDirectory, isTestMode) { @@ -223,6 +225,7 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean is /// Test mode is true or false. /// We want to allow logging when CLI mode is true even if we are in test mode. /// We want restrict logging in service mode to console only due to lambda limitations. + /// TODO(DYN-5757): Review usage of isTestMode,isTestMode,isServiceMode across Dynamo and see how we can consildate all these flags. public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean isTestMode, Boolean isCLIMode, Boolean isServiceMode) : this(debugSettings, logDirectory, isTestMode) {