stack trace and stack frame

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all i posted this question yesterday. no answers yet.
please do reply if you have any ideas. thanks a lot.
Subject: stack frame stack trace


can the information from the stack be obtained only when
exceptions are thrown? kindly reply.
..
 
Below is a simplified version of what I use in my tracing and error
handling.
It is used to build a message that includes information of both the stack
frame and
a message string/ID/severity

So, to answer your question, you can get information of the stack at any
time (and not only in case of exception handling)

If you want the information to be available in Release mode, you have to go
under
project property -> configuration property -> build (Release) and set
Generate Debug information to True.


José


//--------------------------------------------------------------------------
-------
/// <summary>
/// Simplified method used to build msg to be traced to local File.
/// </summary>
/// <param name="Msg">msg to be traced</param>
/// <param name="MsgStatus">Current StatusID of the message</param>
/// <param name="SeverLevel">Severity level</param>
/// <param name="stackFrameLevel">Index to consider in the stack frame to
retrieve information
/// (such as FileName, Method name and line#)</param>
/// <returns>original msg with extra information (dateTime,...)</returns>
protected string BuildMsg( string Msg,
int MsgID,
Severity.Level SeverLevel,
short stackFrameLevel)
{
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???";


string strFullInfo = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff",
null) +
" [" + gprocessInfo.MainModule.ModuleName + " - " +
strFileName + " - " + stFrame.GetMethod().Name +
"() - Line:" + (stFrame.GetFileLineNumber()).ToString() + "] " +
Environment.NewLine +
" [" + strCurrSeverity + ":" + MsgID.ToString("D5") + "] " +
Msg + Environment.NewLine;

return strFullInfo;
}
catch (Exception ex)
{
return "Unable to build msg for logger. Err: " + ex.ToString() +
". Make sure the .PDB file has been copied in the Binary directory!";
}
}
 
Back
Top