I
IdleBrain
I am trying to log the Application name, Method name, line number and
column number whenever an exception is generated in an C# 2005
application using the following code.
Problem is that the line number that is being obtained by
stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage
function's line number. I am trying to write the line number of the
statement which is causing an exception and not the LogMessage
function's line number. I donot understand why this is happening.
I appreciate your help..Thanks.
try
{
//Code goes here.
}
catch (Exception ex)
{
AnotherForm.LogMessage(ex.Message);
}
Another Form:
public static void LogMessage(String strMessage)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = new StackFrame(1, true); //
stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
//Log the message into Event Logs.
EventLogger.WriteEntry(frmMain.APPLICATION_NAME + " :: "
+ methodBase.Name + " Line: " +
stackFrame.GetFileLineNumber()
+ " Col: " + stackFrame.GetFileColumnNumber() + " Msg:
" + strMessage, EventLogEntryType.Error);
}
column number whenever an exception is generated in an C# 2005
application using the following code.
Problem is that the line number that is being obtained by
stackFrame.GetFileLineNumber() represents AnotherForm.LogMessage
function's line number. I am trying to write the line number of the
statement which is causing an exception and not the LogMessage
function's line number. I donot understand why this is happening.
I appreciate your help..Thanks.
try
{
//Code goes here.
}
catch (Exception ex)
{
AnotherForm.LogMessage(ex.Message);
}
Another Form:
public static void LogMessage(String strMessage)
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = new StackFrame(1, true); //
stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
//Log the message into Event Logs.
EventLogger.WriteEntry(frmMain.APPLICATION_NAME + " :: "
+ methodBase.Name + " Line: " +
stackFrame.GetFileLineNumber()
+ " Col: " + stackFrame.GetFileColumnNumber() + " Msg:
" + strMessage, EventLogEntryType.Error);
}