Skip to content

Commit

Permalink
DYN-5755 : restrict logging in service mode (#13860)
Browse files Browse the repository at this point in the history
* restrict logging in service mode

* add todo

---------

Co-authored-by: Bogdan Zavu <[email protected]>

passed.
  • Loading branch information
BogdanZavu authored and sm6srw committed Apr 5, 2023
1 parent fde0923 commit a0a7175
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/DynamoCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
50 changes: 48 additions & 2 deletions src/DynamoCore/Logging/DynamoLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -159,7 +160,7 @@ public IEnumerable<NotificationMessage> StartupNotifications
/// </summary>
/// <param name="debugSettings">Debug settings</param>
/// <param name="logDirectory">Directory path where log file will be written</param>
[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)
{

Expand All @@ -172,6 +173,7 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory) : this(deb
/// <param name="debugSettings">Debug settings</param>
/// <param name="logDirectory">Directory path where log file will be written</param>
/// <param name="isTestMode">Test mode is true or false.</param>
[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)
Expand Down Expand Up @@ -203,6 +205,7 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean is
/// <param name="logDirectory">Directory path where log file will be written</param>
/// <param name="isTestMode">Test mode is true or false.</param>
/// <param name="isCLIMode">We want to allow logging when CLI mode is true even if we are in test mode.</param>
[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)
{
Expand All @@ -213,6 +216,42 @@ public DynamoLogger(DebugSettings debugSettings, string logDirectory, Boolean is
}
}

/// <summary>
/// Initializes a new instance of <see cref="DynamoLogger"/> class
/// with specified debug settings and directory where to write logs
/// </summary>
/// <param name="debugSettings">Debug settings</param>
/// <param name="logDirectory">Directory path where log file will be written</param>
/// <param name="isTestMode">Test mode is true or false.</param>
/// <param name="isCLIMode">We want to allow logging when CLI mode is true even if we are in test mode.</param>
/// <param name="isServiceMode">We want restrict logging in service mode to console only due to lambda limitations.</param>
/// 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)
{
lock (guardMutex)
{
this.debugSettings = debugSettings;
_isDisposed = false;

WarningLevel = WarningLevel.Mild;
Warning = "";

notifications = new List<NotificationMessage>();

testMode = isTestMode;
cliMode = isCLIMode;
serviceMode = isServiceMode;

if (!testMode && !isServiceMode)
{
StartLoggingToConsoleAndFile(logDirectory);
}

XmlDocumentationExtensions.LogToConsole += Log;
}
}

/// <summary>
/// Logs the specified message.
/// </summary>
Expand Down Expand Up @@ -244,6 +283,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
Expand Down Expand Up @@ -444,7 +489,8 @@ private void StartLoggingToConsoleAndFile(string logDirectory)
{
lock (this.guardMutex)
{
if (FileWriter != null && ConsoleWriter != null)
if (serviceMode ||
(FileWriter != null && ConsoleWriter != null))
{
return;
}
Expand Down
8 changes: 7 additions & 1 deletion src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -2439,6 +2439,12 @@ private void RegisterHomeWorkspace(HomeWorkspaceModel newWorkspace)
/// </summary>
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
Expand Down

0 comments on commit a0a7175

Please sign in to comment.