4 July 2011

Get the Function name,Line number,Columnnumber of the Error in C#

//Get the Function name,Line number,Columnnumber of the Error in C#
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
CustomError();
}
catch (Exception ex)
{
StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
MessageBox.Show("Function Name:"+trace.GetFrame(0).GetMethod().Name);
MessageBox.Show("Line Number: " + trace.GetFrame(0).GetFileLineNumber());
MessageBox.Show("Column: " + trace.GetFrame(0).GetFileColumnNumber());
}
}

private void CustomError()
{
throw new Exception("An error has happened");
}
}
}

No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...