I was writing some stuff last night and just thought I would comment it up and share it.
Maybe some of y’all could find it useful.
Code →
//##########################################################
//# Author: Kevin Lincecum 2025
//#
//# Example Debug Stuff, put this at the top of your code
//# This is just some examples, use what you like.
//##########################################################
//Output Variables ->
// ListErrors (DataSet)
// DebugOut (String)
//This little delegate inits a DataSet to hold debug data
Func<DataSet> InitExceptionsDS = () =>
{
//Exceptions
var ds = new DataSet();
var tbl0 = new DataTable("Exceptions");
tbl0.Columns.Add("Exception", typeof(Exception));
ds.Tables.Add(tbl0);
//Error Messages
var tbl1 = new DataTable("Errors");
tbl1.Columns.Add("Message", typeof(string));
ds.Tables.Add(tbl1);
//A mock "Console"
var tbl2 = new DataTable("Console");
tbl2.Columns.Add("Messages", typeof(string));
ds.Tables.Add(tbl2);
return ds;
};
//This is the DataSet we are returning with our function
ListErrors = InitExceptionsDS();
//Base Methods to add things to the dataset
Action<DataSet, Exception> AddExceptionToDataSetBase = (exDS, exception) => exDS.Tables["Exceptions"].Rows.Add(exception);
Action<DataSet, string, string> AddMessageToDataSetBase = (exDS, tblName, message) => exDS.Tables[tblName].Rows.Add(message);
//Just a Dumper for neat Console output if you want it
Func <DataSet, string, string, string> DumpMessagesToString = (exDS, tblName, fieldName) => String.Join(String.Empty, exDS.Tables[tblName].AsEnumerable().Select(fld => fld.Field<string>(fieldName)));
//Convenience methods so I would have to write less stuff in the actual code.
var dsForDebug = ListErrors;
Action<Exception> AddExceptionToDataSet = (exception) => AddExceptionToDataSetBase(dsForDebug, exception);
Action<string> AddErrorToDataSet = (message) => AddMessageToDataSetBase (dsForDebug, "Errors", message);
Action<string> Write = (message) => AddMessageToDataSetBase (dsForDebug, "Console", message);
Action<string> WriteLine = (message) => Write(message + Environment.NewLine);
Func <string> DumpConsole = () => DumpMessagesToString (dsForDebug, "Console", "Messages");
//Mock Console
var Console = new { Write, WriteLine };
try
{
//Code in here
int zero = 0;
//Demo
for(int x = 0; x < 5; x++)
{
try
{
Console.WriteLine($"Try #{(x + 1).ToString()}...");
int nope = x / zero;
}
catch (Exception iEx)
{
AddExceptionToDataSet(iEx);
AddErrorToDataSet("Don't do that dummy.");
}
}
//'Write' is supported too
Console.Write("This is the first part of a long sentence ... ");
Console.WriteLine("and this is the rest of it. lol");
//I do, most of you anyway.
throw new BLException("I don't like you!");
}
catch (Exception ex)
{
AddExceptionToDataSet(ex);
Console.WriteLine("WTF Happened?");
}
finally
{
Console.WriteLine("I'm done, just done lol.");
//I dumped the whole "Console" output to a single string.
//Do I need to? No, but I wanted to.
DebugOut = DumpConsole();
}
ListErrors Output →
{
"Exceptions": [
{
"Exception": {
"ClassName": "System.DivideByZeroException",
"Message": "Attempted to divide by zero.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147352558,
"Source": "efx.github",
"WatsonBuckets": null
}
},
{
"Exception": {
"ClassName": "System.DivideByZeroException",
"Message": "Attempted to divide by zero.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147352558,
"Source": "efx.github",
"WatsonBuckets": null
}
},
{
"Exception": {
"ClassName": "System.DivideByZeroException",
"Message": "Attempted to divide by zero.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147352558,
"Source": "efx.github",
"WatsonBuckets": null
}
},
{
"Exception": {
"ClassName": "System.DivideByZeroException",
"Message": "Attempted to divide by zero.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147352558,
"Source": "efx.github",
"WatsonBuckets": null
}
},
{
"Exception": {
"ClassName": "System.DivideByZeroException",
"Message": "Attempted to divide by zero.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147352558,
"Source": "efx.github",
"WatsonBuckets": null
}
},
{
"Exception": {
"ClassName": "Ice.BLException",
"Message": "I don't like you!",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at EFx.GitHub.Implementation.PushFilesToGitHubImpl.Run()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146232832,
"Source": "efx.github",
"WatsonBuckets": null
}
}
],
"Errors": [
{
"Message": "Don't do that dummy."
},
{
"Message": "Don't do that dummy."
},
{
"Message": "Don't do that dummy."
},
{
"Message": "Don't do that dummy."
},
{
"Message": "Don't do that dummy."
}
],
"Console": [
{
"Messages": "Try #1...\r\n"
},
{
"Messages": "Try #2...\r\n"
},
{
"Messages": "Try #3...\r\n"
},
{
"Messages": "Try #4...\r\n"
},
{
"Messages": "Try #5...\r\n"
},
{
"Messages": "This is the first part of a long sentence ... "
},
{
"Messages": "and this is the rest of it. lol\r\n"
},
{
"Messages": "WTF Happened?\r\n"
},
{
"Messages": "I'm done, just done lol.\r\n"
}
]
}
DebugOut Output →
Try #1...
Try #2...
Try #3...
Try #4...
Try #5...
This is the first part of a long sentence ... and this is the rest of it. lol
WTF Happened?
I'm done, just done lol.