Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent multiple logging #671

Merged
merged 2 commits into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
512 changes: 257 additions & 255 deletions backend/Origam.BI.CrystalReports.NetFx/CrystalReportHelper.cs

Large diffs are not rendered by default.

37 changes: 36 additions & 1 deletion backend/Origam.Common/Extensions/LogExtensions.cs
Expand Up @@ -21,11 +21,14 @@

using System;
using log4net;
using Microsoft.Extensions.Logging;

namespace Origam.Extensions
{
public static class LogExtensions
{
public static readonly string IsLoggedKey = "logged";

// Intended for error handling of logging code.
// Remember to wrap all calls to this method in if(log.IsXXXEnabled){}
// to minimize performance impact of logging.
Expand All @@ -37,8 +40,40 @@ public static void RunHandled(this ILog log, Action loggingAction)
}
catch (Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
}
}

public static void LogOrigamError(this ILog log, string message, Exception ex)
{
if (ex.Data.Contains(IsLoggedKey) && Equals(ex.Data[IsLoggedKey], true))
{
return;
}
log.Error(message, ex);
ex.Data[IsLoggedKey] = true;
}
public static void LogOrigamError(this ILog log, Exception ex)
{
if (ex.Data.Contains(IsLoggedKey) && Equals(ex.Data[IsLoggedKey], true))
{
return;
}
log.Error(ex);
ex.Data[IsLoggedKey] = true;
}
}

public static class ILoggerExtensions
{
public static void LogOrigamError(this ILogger log, Exception ex, string message)
{
if (ex.Data.Contains(LogExtensions.IsLoggedKey) && Equals(ex.Data[LogExtensions.IsLoggedKey], true))
{
return;
}
log.LogError(ex, message);
ex.Data[LogExtensions.IsLoggedKey] = true;
}
}
}
7 changes: 6 additions & 1 deletion backend/Origam.Common/OrigamException.cs
Expand Up @@ -20,6 +20,7 @@
#endregion

using System;
using System.Collections;
using System.Text;

namespace Origam
Expand All @@ -41,9 +42,13 @@ public OrigamException (string message) : base(message)

public OrigamException (string message, Exception innerException) : base(message, innerException)
{
foreach (DictionaryEntry entry in innerException.Data)
{
Data[entry.Key] = entry.Value;
}
}

public OrigamException (string message, string customStackTrace, Exception innerException) : base(message, innerException)
public OrigamException (string message, string customStackTrace, Exception innerException) : this(message, innerException)
{
this.AppendStackTrace(customStackTrace);
}
Expand Down
3 changes: 2 additions & 1 deletion backend/Origam.Common/ResourceMonitor.cs
Expand Up @@ -22,6 +22,7 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using Origam.Extensions;

namespace Origam
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public static void Rollback(string transactionId, string savePointName)
{
if(log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
log.LogOrigamError(ex.Message, ex);
}

if(errorMessage != "") errorMessage += Environment.NewLine;
Expand Down
8 changes: 4 additions & 4 deletions backend/Origam.DA.Service/AbstractSqlDataService.cs
Expand Up @@ -154,7 +154,7 @@ private void HandleException(Exception ex, string commandText, bool logAsDebug)
{
if (log.IsErrorEnabled && !logAsDebug)
{
log.Error(
log.LogOrigamError(
$"{ex.Message}, SQL: {commandText}",
ex);
}
Expand Down Expand Up @@ -413,7 +413,7 @@ internal override IDbTransaction GetTransaction(string transactionId, IsolationL
{
try
{
log.Error(DebugClass.ListRowErrors(dataset), ex);
log.LogOrigamError(DebugClass.ListRowErrors(dataset), ex);
using(System.IO.StreamWriter w = System.IO.File.CreateText(AppDomain.CurrentDomain.BaseDirectory + @"\debug\" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-") + DateTime.Now.Ticks.ToString() + "___" + "MsSqlDataService_error.txt"))
{
w.WriteLine(DebugClass.ListRowErrors(dataset));
Expand Down Expand Up @@ -639,7 +639,7 @@ internal override IDbTransaction GetTransaction(string transactionId, IsolationL
{
if (log.IsErrorEnabled)
{
log.Error("Update failed", e);
log.LogOrigamError("Update failed", e);
}
if (newTransaction)
{
Expand Down Expand Up @@ -1154,7 +1154,7 @@ public override DataSet ExecuteProcedure(string name, string entityOrder, DataSt
{
if(log.IsErrorEnabled)
{
log.Error("Stored Procedure Call failed", e);
log.LogOrigamError("Stored Procedure Call failed", e);
}

throw;
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.DA.Service/BinFileLoader.cs
Expand Up @@ -153,7 +153,7 @@ public void LoadInto(ItemTracker itemTracker)
}
catch (IOException ex)
{
log.Error($"Could not remove {indexFile}", ex);
log.LogOrigamError($"Could not remove {indexFile}", ex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.Gui.Designer/ControlSetEditor.cs
Expand Up @@ -1789,7 +1789,7 @@ private void ControlSetEditor_Closed(object sender, System.EventArgs e)
}
catch(Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
Expand Down
5 changes: 3 additions & 2 deletions backend/Origam.Mail/NetStandardMailService.cs
Expand Up @@ -26,6 +26,7 @@
using System.Xml.XPath;
using System.Net.Mime;
using System.Net;
using Origam.Extensions;
using Origam.Service.Core;

namespace Origam.Mail
Expand Down Expand Up @@ -129,7 +130,7 @@ public override int SendMail1(IXmlContainer mailDocument, string server, int por
}
catch(Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
throw;
}
}
Expand Down Expand Up @@ -232,7 +233,7 @@ public override int SendMail2(MailData mailData, string server, int port)
}
catch (Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
throw;
}
}
Expand Down
3 changes: 2 additions & 1 deletion backend/Origam.OrigamEngine/OrigamEngine.cs
Expand Up @@ -36,6 +36,7 @@
using System.Linq;
using Origam.DA.ObjectPersistence;
using System.Security.Principal;
using Origam.Extensions;

namespace Origam.OrigamEngine
{
Expand Down Expand Up @@ -246,7 +247,7 @@ private static void RestartTimer_Elapsed(object sender, System.Timers.ElapsedEve
RestartTimer.Interval *= 10;
if(log.IsErrorEnabled)
{
log.Error("Could not get restart status. Will retry in " + RestartTimer.Interval / 1000 + "seconds.", ex);
log.LogOrigamError("Could not get restart status. Will retry in " + RestartTimer.Interval / 1000 + "seconds.", ex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.Rule/DatasetRuleHandler.cs
Expand Up @@ -138,7 +138,7 @@ public void OnRowChanged(DataRowChangeEventArgs e, IDataDocument data, DataStruc
catch(Exception ex)
{
var origamRuleException = new OrigamRuleException(ResourceUtils.GetString("ErrorRuleFailureRecord", e.Row.Table.DisplayExpression, Environment.NewLine + ex.Message), ex, e.Row);
log.Error(origamRuleException); // DataTable will ignore the exception after we throw it so we at least log it here
log.LogOrigamError(origamRuleException); // DataTable will ignore the exception after we throw it so we at least log it here
throw origamRuleException;
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.Rule/RuleEngine.cs
Expand Up @@ -3873,7 +3873,7 @@ public RowSecurityState RowLevelSecurityState(DataRow row, object profileId, Gui
{
if (log.IsErrorEnabled)
{
log.Error(string.Format(
log.LogOrigamError(string.Format(
"Failed evaluating security rule for child relation {0} for entity {1}",
rel?.RelationName, entityId), ex);
}
Expand Down
7 changes: 4 additions & 3 deletions backend/Origam.Security.Common/AccountMailSender.cs
Expand Up @@ -28,6 +28,7 @@
using System.Net.Mail;
using log4net;
using Origam.DA;
using Origam.Extensions;
using Origam.Workbench.Services;
using Origam.Workbench.Services.CoreServices;

Expand Down Expand Up @@ -129,7 +130,7 @@ public class AccountMailSender
{
if (log.IsErrorEnabled)
{
log.Error("Failed to send new user registration mail", ex);
log.LogOrigamError("Failed to send new user registration mail", ex);
}

throw new Exception(Resources.FailedToSendNewUserRegistrationMail);
Expand Down Expand Up @@ -264,7 +265,7 @@ public void SendMultiFactorAuthCode(string email, string code)
{
if (log.IsErrorEnabled)
{
log.Error("Failed to send multi factor authentication mail", ex);
log.LogOrigamError("Failed to send multi factor authentication mail", ex);
}

throw new Exception(Resources.FailedToSendMultiFactorAuthCode);
Expand Down Expand Up @@ -373,7 +374,7 @@ public void SendMultiFactorAuthCode(string email, string code)
{
if (log.IsErrorEnabled)
{
log.Error(string.Format("Failed to send password reset "
log.LogOrigamError(string.Format("Failed to send password reset "
+ "mail for username `{0}', email `{1}'",
username, email), ex);
}
Expand Down
5 changes: 3 additions & 2 deletions backend/Origam.Security/OrigamProfileProvider.cs
Expand Up @@ -24,6 +24,7 @@
using System.Data;

using Origam.DA;
using Origam.Extensions;
using Origam.Workbench.Services;

namespace Origam.Security
Expand Down Expand Up @@ -115,15 +116,15 @@ override public object GetProfile(string userName)
{
if(log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
log.LogOrigamError(ex.Message, ex);
}
throw;
}
catch(Exception ex)
{
if(log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
log.LogOrigamError(ex.Message, ex);
}
throw new Exception(
ResourceUtils.GetString("ErrorUnableToLoadProfile0")
Expand Down
5 changes: 3 additions & 2 deletions backend/Origam.Security/OrigamTenantProfileProvider.cs
Expand Up @@ -23,6 +23,7 @@
using System.Collections;
using System.Data;
using Origam.DA;
using Origam.Extensions;
using Origam.Workbench.Services;

namespace Origam.Security
Expand Down Expand Up @@ -145,15 +146,15 @@ override public object GetProfile(string userName)
{
if(log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
log.LogOrigamError(ex.Message, ex);
}
throw;
}
catch(Exception ex)
{
if(log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
log.LogOrigamError(ex.Message, ex);
}
throw new Exception(
ResourceUtils.GetString("ErrorUnableToLoadProfile0")
Expand Down
3 changes: 1 addition & 2 deletions backend/Origam.Server/Controller/AbstractController.cs
Expand Up @@ -109,8 +109,7 @@ protected IActionResult RunWithErrorHandler(Func<IActionResult> func)
{
return StatusCode(420, ex);
}

log.LogError(ex, ex.Message);
log.LogOrigamError(ex, ex.Message);
return StatusCode(500, ex);
}
}
Expand Down
3 changes: 2 additions & 1 deletion backend/Origam.Server/Pages/UserApiProcessor.cs
Expand Up @@ -39,6 +39,7 @@
using Origam.Workbench.Services;
using System.Linq;
using System.Web;
using Origam.Extensions;
using Origam.Service.Core;

namespace Origam.Server.Pages
Expand Down Expand Up @@ -150,7 +151,7 @@ IParameterService parameterService
{
if (log.IsErrorEnabled)
{
log.Error(
log.LogOrigamError(
$@"Error occured ({ex.GetType()}) for request:
{context.Request?.AbsoluteUri}: {ex.Message}"
, ex);
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.Server/ServerCoreUIService.cs
Expand Up @@ -188,7 +188,7 @@ public PortalResult InitPortal(int maxRequestLength)
{
if(log.IsFatalEnabled)
{
log.Error(
log.LogOrigamError(
"Failed to destroy session " + id.ToString()
+ ".", ex);
}
Expand Down
3 changes: 2 additions & 1 deletion backend/Origam.Workbench.Services/PersistenceService.cs
Expand Up @@ -32,6 +32,7 @@
using Origam.Schema;
using System.IO;
using System.Text;
using Origam.Extensions;

namespace Origam.Workbench.Services
{
Expand Down Expand Up @@ -314,7 +315,7 @@ public void InitializeRepository()
}
catch (Exception ex)
{
log.Error("Failed to initialize repository...", ex);
log.LogOrigamError("Failed to initialize repository...", ex);
ResourceMonitor.Rollback(transactionId);
}
}
Expand Down
5 changes: 3 additions & 2 deletions backend/Origam.Workbench.Services/TracingService.cs
Expand Up @@ -23,6 +23,7 @@
using System.Data;
using System.Reflection;
using Origam.DA;
using Origam.Extensions;

namespace Origam.Workbench.Services
{
Expand Down Expand Up @@ -121,7 +122,7 @@ public void TraceWorkflow(Guid workflowInstanceId, Guid workflowId, string workf
}
catch (Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
}
}

Expand Down Expand Up @@ -170,7 +171,7 @@ public void TraceRule(Guid ruleId, string ruleName, string ruleInput, string rul
}
catch (Exception ex)
{
log.Error(ex);
log.LogOrigamError(ex);
}
}

Expand Down
3 changes: 2 additions & 1 deletion backend/Origam.Workbench/Pads/ServerLogPad.cs
Expand Up @@ -23,6 +23,7 @@
using System.IO;
using System.Net;
using System.Windows.Forms;
using Origam.Extensions;

namespace Origam.Workbench.Pads
{
Expand Down Expand Up @@ -178,7 +179,7 @@ private void timer_Tick(object sender, EventArgs e)
SetOutputText(message);
if(log.IsErrorEnabled)
{
log.Error(message, ex);
log.LogOrigamError(message, ex);
}
}
}
Expand Down