Yes, this is!
Here is an snippet of one of the routine I use within my custom exception
(It contains more, but was simplified for clarity).
Playing with the stackFrameLevel, you can determine which routine within the
stack you want to get information from.
Hope this help
José
protected string BuildMsg( string Msg,
Status.Stat MsgStatus,
Severity.Level SeverLevel,
short stackFrameLevel)
{
int nDebLocation = 0;
try
{
// Convert severity to 1 char
string strCurrSeverity = Severity.ToShortString(SeverLevel);
// Get stack information (FileName, Method, Line#)
StackFrame stFrame = new StackFrame(stackFrameLevel, true);
}
string strFileName;
if ( stFrame.GetFileName() != null)
strFileName =
stFrame.GetFileName().Substring(stFrame.GetFileName().LastIndexOf(@"\")+1);
else
strFileName = "Unknown???";
// Build information
string strFullInfo = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff",
null) +
" [" + processInfo.MainModule.ModuleName + " - " +
strFileName + " - " + stFrame.GetMethod().Name +
"() - Line:" + (stFrame.GetFileLineNumber()).ToString() + "] " +
Environment.NewLine +
" [" + strCurrSeverity + ":" + ((int)MsgStatus).ToString("D5")
+ "] " + Msg + Environment.NewLine;
return strFullInfo;
}
catch (Exception ex)
{
return "Unable to build msg for logger. Err: " + ex.ToString() + ".
Dbg: " +
nDebLocation +
". Make sure the .PDB file has been copied in the Binary directory!";
}
}
You have to play with StackFrame class. It provides a whole lot of
information you can extract for your logging purpose.
Mike Oliszewski said:
Is it possible for a c# function to get it's own name ? We have LOTS of
logging calls for which the programmer has to manually add the name of the
function as part of the output, often people copy code from one area to
another and then forget to update the name. It would be really slick if we
were able to programmatically get the function name without a huge
processing overhead.
Is this possible ? Is it possible to get a calling functions name ? Any
samples anywhere ?