Skip to content

BHoM_Engine: Add methods to record events from exceptions #2998

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

Merged
merged 10 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions BHoM_Engine/Compute/RecordError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
Expand All @@ -32,13 +34,23 @@ public static partial class Compute
/**** Public Methods ****/
/***************************************************/

[Description("Record an error within the BHoM logging system.")]
[Input("message", "The message to be recorded for this error.")]
[Output("success", "True if the error has been successfully recorded as a BHoM Event.")]
public static bool RecordError(string message)
{
return RecordEvent(new Event { Message = message, Type = EventType.Error });
}
}
}



/***************************************************/

[Description("Record an error with details of a C# exception within the BHoM logging system.")]
[Input("exception", "The C# exception being caught to provide the error and stack information for.")]
[Input("message", "An optional additional message which will be displayed first in the event log.")]
[Output("success", "True if the error has been successfully recorded as a BHoM Event.")]
public static bool RecordError(Exception exception, string message = "")
{
return RecordEvent(exception, message, EventType.Error);
}
}
}
36 changes: 34 additions & 2 deletions BHoM_Engine/Compute/RecordEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ public static bool RecordEvent(string message, EventType type = EventType.Unknow

/***************************************************/

[Description("Record an event with details of a C# exception within the BHoM logging system.")]
[Input("exception", "The C# exception being caught to provide the event and stack information for.")]
[Input("message", "An optional additional message which will be displayed first in the event log.")]
[Output("success", "True if the event has been successfully recorded as a BHoM Event.")]
public static bool RecordEvent(Exception exception, string message = "", EventType type = EventType.Unknown)
{
string exceptionMessage = "";

Exception e = exception;
while(e != null)
{
if (!string.IsNullOrEmpty(exceptionMessage))
exceptionMessage += $"{Environment.NewLine}{Environment.NewLine}";

exceptionMessage += $"{e.Message}";

e = e.InnerException;
}

if (!string.IsNullOrEmpty(message))
message = $"{message}\n\n{exceptionMessage}";
else
message = exceptionMessage;

return RecordEvent(new Event { Message = message, StackTrace = exception.StackTrace, Type = type });
}

/***************************************************/

[Description("Records an event in the BHoM event log.")]
[Input("newEvent", "Event to be logged.")]
[Output("success", "True if the event is logged successfully.")]
Expand All @@ -57,8 +86,11 @@ public static bool RecordEvent(Event newEvent)
return false;
}

string trace = System.Environment.StackTrace;
newEvent.StackTrace = string.Join("\n", trace.Split('\n').Skip(4).ToArray());
if (string.IsNullOrEmpty(newEvent.StackTrace))
{
string trace = System.Environment.StackTrace;
newEvent.StackTrace = string.Join("\n", trace.Split('\n').Skip(4).ToArray());
}

lock (Global.DebugLogLock)
{
Expand Down
20 changes: 16 additions & 4 deletions BHoM_Engine/Compute/RecordNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
Expand All @@ -32,13 +34,23 @@ public static partial class Compute
/**** Public Methods ****/
/***************************************************/

[Description("Record a note within the BHoM logging system.")]
[Input("message", "The message to be recorded for this note.")]
[Output("success", "True if the note has been successfully recorded as a BHoM Event.")]
public static bool RecordNote(string message)
{
return RecordEvent(new Event { Message = message, Type = EventType.Note });
}
}
}



/***************************************************/

[Description("Record a note with details of a C# exception within the BHoM logging system.")]
[Input("exception", "The C# exception being caught to provide the note and stack information for.")]
[Input("message", "An optional additional message which will be displayed first in the event log.")]
[Output("success", "True if the note has been successfully recorded as a BHoM Event.")]
public static bool RecordNote(Exception exception, string message = "")
{
return RecordEvent(exception, message, EventType.Note);
}
}
}
20 changes: 16 additions & 4 deletions BHoM_Engine/Compute/RecordWarning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
Expand All @@ -32,13 +34,23 @@ public static partial class Compute
/**** Public Methods ****/
/***************************************************/

[Description("Record a warning within the BHoM logging system.")]
[Input("message", "The message to be recorded for this warning.")]
[Output("success", "True if the warning has been successfully recorded as a BHoM Event.")]
public static bool RecordWarning(string message)
{
return RecordEvent(new Event { Message = message, Type = EventType.Warning });
}
}
}



/***************************************************/

[Description("Record a warning with details of a C# exception within the BHoM logging system.")]
[Input("exception", "The C# exception being caught to provide the warning and stack information for.")]
[Input("message", "An optional additional message which will be displayed first in the event log.")]
[Output("success", "True if the warning has been successfully recorded as a BHoM Event.")]
public static bool RecordWarning(Exception exception, string message = "")
{
return RecordEvent(exception, message, EventType.Warning);
}
}
}