Is there a way to grab the function name and file name when I’m inside a function? I’m trying to add log files all over the place like so, and want to be able to just copy and paster my logfile line of code in certain functions:
public class NewCorrGenerator
{
public void Start()
{
bool rc = true;
myLogFile.InfoToFileWithTime("NewCorrGenerator - Start", appContext);
etc.....
}
}
As you can see is there a way to grab the class name and function name in a string and output it to myLogFile function?
You can use GetCurrentMethod. This will return an object of type
MethodBase. TheNameproperty to get the name of the method.DeclaringTypewill give you the class. This isn’t the same as the file name, but it should be close if you use one type per file.