Skip to content

Commit

Permalink
[Config] add config to disable write error to file system (#403)
Browse files Browse the repository at this point in the history
* disable error records persistence

* split RecordDebugMessage and FlushDebugMessages

* invoke RecordDebugMessages only when write error and throw terminating error

* combine if condition in a bool function

* Uppercase first letter for method name
  • Loading branch information
VeryEarly authored Aug 11, 2023
1 parent 7984785 commit 9b5f8b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Authentication.Abstractions/Models/ConfigKeysForCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public static class ConfigKeysForCommon
public const string EnableDataCollection = "EnableDataCollection";
public const string EnableTestCoverage = "EnableTestCoverage";
public const string CheckForUpgrade = "CheckForUpgrade";
//Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023)
public const string DisableErrorRecordsPersistence = "DisableErrorRecordsPersistence";
public const string EnableErrorRecordsPersistence = "EnableErrorRecordsPersistence";
}
}
27 changes: 19 additions & 8 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,11 @@ protected void WriteSurvey()
}
protected new void WriteError(ErrorRecord errorRecord)
{
FlushDebugMessages(IsDataCollectionAllowed());
FlushDebugMessages();
if (ShouldRecordDebugMessages())
{
RecordDebugMessages();
}
if (_qosEvent != null && errorRecord != null)
{
_qosEvent.Exception = errorRecord.Exception;
Expand All @@ -515,7 +519,11 @@ protected void WriteSurvey()

protected new void ThrowTerminatingError(ErrorRecord errorRecord)
{
FlushDebugMessages(IsDataCollectionAllowed());
FlushDebugMessages();
if (ShouldRecordDebugMessages())
{
RecordDebugMessages();
}
base.ThrowTerminatingError(errorRecord);
}

Expand Down Expand Up @@ -634,13 +642,8 @@ protected void SafeWriteOutputPSObject(string typeName, params object[] args)
WriteObject(customObject);
}

private void FlushDebugMessages(bool record = false)
private void FlushDebugMessages()
{
if (record)
{
RecordDebugMessages();
}

string message;
while (DebugMessages.TryDequeue(out message))
{
Expand Down Expand Up @@ -777,6 +780,14 @@ private void RecordDebugMessages()
}
}

//Use DisableErrorRecordsPersistence as opt-out for now, will replace it with EnableErrorRecordsPersistence as opt-in at next major release (November 2023)
private bool ShouldRecordDebugMessages()
{
return (!AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)
|| !configManager.GetConfigValue<bool>(ConfigKeysForCommon.DisableErrorRecordsPersistence))
&& IsDataCollectionAllowed();
}

/// <summary>
/// Invoke this method when the cmdlet is completed or terminated.
/// </summary>
Expand Down

0 comments on commit 9b5f8b9

Please sign in to comment.