(SOLVED)
I have my own class library that I've been successively developed, iterated and modified during my programming course, it contains a multitude of methods that I use on the regular. For example I have a method that is called from any class using the class with B_Core.WriteDebug(string);
.
This method currently only takes whatever text I've written, and allows me to not include the System.Diagnostics
in the classes using my own library.
This can, on occassion lead to situations where I simply want to know a single digit, and it could be something like B_Core.WriteDebug(integer.ToString());
. As I've developed as a programmer I do try to always include a reference point, but when I miss it, I sometimes just get a line in the Debug output saying "4" or something along those lines.
So, I am currently trying to make a decent correction to this method. Currently the only idea I have is to parse a StackTrace to the method.
public static void WriteDebug(string text, StackTrace trace) {
var frame = trace.GetFrame(0);
string writeString = "Line: " +
frame.GetFileLineNumber() + " | " +
frame.GetFileName() + " | " +
frame.GetMethod() + " | Text:\n";
writeString += text;
// Debug output => Line: x | File: Program.cs | Main | Text:
// Hello world!
Debug.WriteLine(writeString);
}
This'll work, but it requires me to add the using System.Diagnostics
to each class that is gonna use the B_Core.WriteLine()
. This can result in some pointless written additions, it's also the class granting easy accessibility to Debug.WriteLine()
, which removes part of my reason for having my own method for this in the library.
It also requires me to actually parse the StackTrace, adding more things to write when I simply want to know a simple thing for troubleshooting or similar.
The ideal scenario would be something like:
public static void WriteDebug(string text) { //Removed the StackTrace parse
var frame = Caller.trace.GetFrame(0); //Added a hypothetical Caller object
string writeString = "Line: " +
frame.GetFileLineNumber() + " | " +
frame.GetFileName() + " | " +
frame.GetMethod() + " | Text:\n";
writeString += text;
// Debug output => Line: x | File: Program.cs | Main | Text:
// Hello world!
Debug.WriteLine(writeString);
}
Where I'd get the data by just referencing the calling method itself. So my question is there any way to do something like this without adding extra references atop the calling method's class as well as hopefully not having to parse any other object to the method?
Thanks for any insight into this.
Best regards!
Solution, thanks to Fildor in the comments. The answer by Victor is also good and might be closer to the actual question. But I've decided to use Fildor's recommendation.
This is the method at this time:
public static void WriteTrace(string text, bool printTrace = true,
[System.Runtime.CompilerServices.CallerMemberName] string membName = "",
[System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
[System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {
if (printTrace)
Trace.Write("Line: " +
lineNumber + " | " +
filePath.Split('\\').Last() + " | " +
membName + ":\t"
);
Trace.WriteLine(text);
//Output => Line: 208 | BMenu.cs | MoveSelection: 0
}
You can achieve this by using Attributes:
FunctionName that called: CallerMemeberNameAttribute
Caller File: CallerFilePathAttribute
Caller Line Number: CallerLineNumberAttribute