srinath vuyyuru123

Hi

Can any one suggest me to how to store/track all the exceptions(Major/Minor) occured in my application.

This helps me to track and Fix the Bugs Before Client Reports me.

Thanks

Srinath Vuyyuru



Re: Visual C# General Storing Exceptions in File using C#

utkuozan

I am currently using the same technique to track the software I am currently developing. Yet this might not be the most efficient way, it works informative. Let me describe the process.

First of all I have a class named cl_Logger to handle exception loggings. It is up to you what to log, but I am currently logging

  • Exception time
  • Exception sender (A form or class)
  • Exception sender (Which form or class)
  • Exceptino creating function
  • Exception message

in a text file. My cl_Logger class is quite simple:

class cl_Logger

{

public void createExceptionLog(string senderType, string className, string senderMethod, string senderError)

{

string CurrentTime = "[" + DateTime.Now.ToString() + "]";

string SenderType = "[" + senderType + "]";

string ClassName = "[" + className + "]";

string SenderMethod = "[" + senderMethod + "]";

string SenderError = "[" + senderError + "]";

if (File.Exists("Errors.log"))

{

using (StreamWriter sw = File.AppendText("Errors.log"))

{

sw.WriteLine(CurrentTime + " " + SenderType + " " + ClassName + " " + SenderMethod + " " + SenderError);

sw.Close();

}

}

else

{

using (StreamWriter sw = File.CreateText("Errors.log"))

{

sw.WriteLine(CurrentTime + " " + SenderType + " " + ClassName + " " + SenderMethod + " " + SenderError);

sw.Close();

}

}

}

}

And at the place where I catch exceptions I am using a code like this

catch (Exception ex)

{

cl_DataHolder dh = new cl_DataHolder();

if (dh.willLog)

{

cl_Logger log = new cl_Logger();

log.createExceptionLog("Form", "frmMyForm", "private void SetParameters(ParameterFields parameterFields, ArrayList arrayList)", ex.Message);

}

}

cl_DataHolder is a general purpose class I use to hold all kind of datas. It checkes if the logs are set to be recorded. If yes, then records the log, else skips it. This way I get asn Errors.log file holding the exceptions.

Regards






Re: Visual C# General Storing Exceptions in File using C#

bruno_1

I think the whole idea of reporting every error is great, and it helps a lot to see all errors and know in which order they're thrown.
Every thing depends on what you need, however i think that all the parameters you took can be extracted from the exception itself:

ex.Source = the source
ex.TargetSite.ToString(): get the method where the error was thrown
ex.Message: a message

Also there is much more info in the exception itself, specially if it was a special exception like a sql exception, it will give you what sal error number and this is very handy to identify the problem.
So maybe making a createExceptionLog(Exception ex) is more useful because you already have lot of info in the exception object parameter, and you can narrow the info to what useful.

Hope this helped





Re: Visual C# General Storing Exceptions in File using C#

utkuozan

Yaps, bruno is quite right. Somehow I thought the solution quite straightforward, missing the big picture. Instead of defining the data yourself, extracting from the exception itself might be a better solution. But of course if you'd need any other data or spec that is not stored in exception class, you might implement it yourself as well.

By the way bruno, my biggest problem on my mind nowadays is catching the line number of the code where the exception has thrown. Bu I don't want it to be static. I mean, I don't want to write the number of line myself; because updating the code changes the line number itself as well. Is there any possibility of getting the line number of the exception or more easily the line number of the catch block






Re: Visual C# General Storing Exceptions in File using C#

bruno_1

You can get the line and the calling method by ex.StackTrace
at WindowsApplication3.Form2.Form2_Load(Object sender, EventArgs e) in C:\Users\zaynoun\Desktop\WindowsApplication3\WindowsApplication3\Form2.cs:line 24, you can extract useful info from this using regular expressions ,here i throwed the error in line 24.