Skip to content

Instantly share code, notes, and snippets.

@INTERNALINTERFERENCE
Created July 3, 2024 08:46
Show Gist options
  • Save INTERNALINTERFERENCE/12aa6548cb05546e4ec628704da4f0cb to your computer and use it in GitHub Desktop.
Save INTERNALINTERFERENCE/12aa6548cb05546e4ec628704da4f0cb to your computer and use it in GitHub Desktop.
public sealed class MessageErrorsManager
{
readonly ConcurrentDictionary<string, MessageErrorObject> errors = new();
public void AddError(MessageErrorObject? error)
{
var correlationId = CorrelationIdHelper.Get();
try
{
Log.WriteDebug($"{nameof(MessageErrorsManager)}: started.", correlationId);
if(error is null)
{
Log.WriteDebug($"Error is null.", correlationId);
return;
}
if(errors.ContainsKey(error.Error))
{
Log.WriteDebug($"Increment error: {error}.", correlationId);
errors[error.Error].Count++;
return;
}
errors[error.Error] = error;
Log.WriteWarning($"Was error: {error}.", correlationId);
}
catch(Exception ex)
{
Log.WriteException(ex, correlationId);
}
finally
{
Log.WriteDebug($"{nameof(MessageErrorsManager)}: ended.", correlationId);
}
}
public void ClearErrors()
{
Log.WriteDebug($"{nameof(MessageErrorsManager)}: clear errors, errors count - {errors.Count}.", null);
errors.Clear();
}
public IEnumerable<string> GetCriticalHealtnessErrors()
{
IEnumerable<string> result = Array.Empty<string>();
try
{
result = errors.Values.Where(e => e.HasCriticalHealthinessErrors).Select(e => e.Error);
return result;
}
catch(Exception ex)
{
Log.WriteException(ex, null);
return result;
}
finally
{
Log.WriteError($"{nameof(GetCriticalHealtnessErrors)}: return value - {result.ToJson()}.", null);
}
}
public bool HasCriticalHealtnessErrors() => GetCriticalHealtnessErrors().Any();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment