logfile

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi,

I want to implement a log function, so all the exception that occur will be
written to a logfile

so:
try {something}
catch (Exception e)
{ write e in logfile}

What are the best solutions for that?
Greetz
JC
 
Create a singleton class called LogFile, in the ctor create it, expose a
Write method that accepts a string, in the dtor close it. Create a global
scope LogForm instance, then use it at will.

-Chris
 
What about implementing an own exception class MyBaseException deriving from
ApplicationException ? If you log the source of the exception and the
message within MyBaseException's constructor, all is automatically logged.
You could also enhance the logging by recursing through all inner exceptions
....

public class MyBaseException : ApplicationException
{
private string m_Source;
private System.Exception m_InnerException;

public MyBaseException(string source, string message) : base(message)
{
this.m_Source = source;

// use static log function ...
LogFile.Write(this.Source + "(): " + this.Message);
}

public MyBaseException(string source, string message, System.Exception
innerException) : base(message, innerException)
{
this.m_Source = source;

// use static log function ...
if (this.InnerException != null)
LogFile.Write(this.Source + "(): " + this.Message + "[" +
this.InnerException.Message + "]");
else
LogFile.Write(this.Source + "(): " + this.Message);
}

public string Source
{
get
{
return this.m_Source;
}
}
}

public class MathClass
{
public static double Mul(double x, double y)
{
try
{
return checked(x * y);
}
catch (System.Exception ex)
{
throw new MyBaseException("MathClass.Mul", "Unexpected
exception", ex);
}
}

public static double Div(double x, double y)
{
try
{
if (y == 0)
throw new MyBaseException("MathClass.Div", "Division by zero
forbidden");

return checked(x / y);
}
catch (MyBaseException)
{
throw;
}
catch (System.Exception ex)
{
throw new MyBaseException("MathClass.Mul", "Unexpected
exception", ex);
}
}
}

Greetings, Christian
 
Hello, Christian

MyBaseException sounds great when I need to throw Exception
out ot the function. However, if I don't want to throw Exception,
and just want to handle the Exception in the Catch section,
maybe Chris's way is more suitable?

Or you have other solutions ?

--
Best Regards,
Jan Yeh

MVP(Windows CE.NET), MCAD, .NETcf Developer & Consultant
Mobile Mind Co., Ltd. @ Taiwan
 
Jan,

As I mentioned, I used to wrap my code like this:

public static int Mul(int x, int y)
{
try
{
return checked(x * y);
}
catch (System.Exception ex)
{
throw new MyBaseException("MathClass.Mul", "Unexpected exception",
ex);
}
}

I simply forward MyBaseExceptions till the point it's handled. For all other
exception types I throw a new MyBaseException and pass the original
exception as inner exception. Due to the fact that my log class recursively
logs the inner exception also exceptions of unknown types get logged.

So, if you call "MathClass.Mul(int.MaxValue, int.MaxValue)" you can catch a
MyBaseException with inner exception OverflowException. The appropriate log
output could look like:

MathClass.Mul: MyBaseException (Unexpected exception) <-- ???:
OverflowException (OverflowException)

Greetings, Christian
 
This is a nice solution for some problems, but not sure it's correct for
this... (maybe it is, I don't know).

First, you may want a bit more of a generic "handler" that can handle not
only errors, but warnings, and "information" types of events. You wouldn't
want to throw a new error each time you pass something off to this object --
there's a bit of an overhead with that. Instead, you can simply catch the
error, and use your Logger object to do whatever you'd like (similar to
Chris' idea).

-Brian
 
Back
Top