F
Flare
Hi i just read:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp
Wich is interesting reading by the way.
But. I have'nt used exception very much to anything else than ordinary
FileExeptions etc. Now i want to create my own exception hierarchy.
I want to do this so that i can log the exceltion information to a file or a
database. Doesent mather right now.
But I can't Figure out where to put the loggin rutine after reading the
article. I suggest that i make a baseexception, wich i Iseriazable
compliant. I have done that. But is it in my Baseclass exception that i
sjould implement my loggin routine?
Or is it in my derrived Custom Exception? Or should the logging rutine be in
a seperate class wich i call when i Catch one of my Cusotm Exceptions?
I have made this so far, but stopped because i have doubt that it s the way
to go.
THX in reagards if someone can clear out for me how to create custom
exceptions wich can be logged. (See sample code i have made)
Anders, Denmark
(Some Sample code)
========BASECLASSEXCEPTION==========
[Serializable]
public class TurabsCriticalException : ApplicationException
{
Exception t;
public TurabsCriticalException() : base()
{
saveExceptionToFile();
}
public TurabsCriticalException(string message) : base(message)
{
MessageInternal = message;
saveExceptionToFile();
}
public TurabsCriticalException(string message, Exception inner) :
base(message, inner)
{
t = inner;
MessageInternal = message;
saveExceptionToFile();
}
protected TurabsCriticalException(SerializationInfo info, StreamingContext
context) : base(info,context)
{
m_strMachineName = info.GetString("m_strMachineName");
}
public override void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("m_strMachineName", m_strMachineName, typeof(String));
info.AddValue("m_DateAndTIme", m_DateAndTime,typeof(DateTime));
base.GetObjectData (info, context);
}
private string m_strMachineName = Environment.MachineName;
public string MachineName
{
get{return m_strMachineName;}
set{m_strMachineName = value;}
}
private string m_MessageInternal = string.Empty;
public string MessageInternal
{get{return m_MessageInternal;}
set{m_MessageInternal = value;}
}
private DateTime m_DateAndTime = DateTime.Now;
public DateTime DateAndTime
{get{return m_DateAndTime;}
set{m_DateAndTime = value;}
}
protected void saveExceptionToFile()
{
try
{
FileStream fs = new FileStream(@"c:\Exception.txt" , FileMode.OpenOrCreate,
FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("===== EXCEPTION LOG FILE ======");
m_streamWriter.WriteLine(MachineName.ToString());
m_streamWriter.WriteLine(DateAndTime.ToShortTimeString());
m_streamWriter.Flush();
fs.Close();
}
catch(Exception ex)
{
string e = ex.Message;
//Write to the System log instead
}
finally
{
}
}
========DERRIVEDCLASSEXCEPTION==========
public class TurabsCriticalDatabaseException : TurabsCriticalException
{
public TurabsCriticalDatabaseException(string message) : base(message)
{
}
}
========TESTCLASS==========
public class DB
{
}
public int DbStuff()
{
throw new TurabsExceptions.TurabsCriticalDatabaseException("Server having
problem!");
}
}
========TESTCODE==========
DB db = new DB();
try
{
db.DbStuff(6);
}
catch(TurabsCriticalException ex)
{
// EXCEPTION HAS BEEN WRITTEN TO FILE
}
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp
Wich is interesting reading by the way.
But. I have'nt used exception very much to anything else than ordinary
FileExeptions etc. Now i want to create my own exception hierarchy.
I want to do this so that i can log the exceltion information to a file or a
database. Doesent mather right now.
But I can't Figure out where to put the loggin rutine after reading the
article. I suggest that i make a baseexception, wich i Iseriazable
compliant. I have done that. But is it in my Baseclass exception that i
sjould implement my loggin routine?
Or is it in my derrived Custom Exception? Or should the logging rutine be in
a seperate class wich i call when i Catch one of my Cusotm Exceptions?
I have made this so far, but stopped because i have doubt that it s the way
to go.
THX in reagards if someone can clear out for me how to create custom
exceptions wich can be logged. (See sample code i have made)
Anders, Denmark
(Some Sample code)
========BASECLASSEXCEPTION==========
[Serializable]
public class TurabsCriticalException : ApplicationException
{
Exception t;
public TurabsCriticalException() : base()
{
saveExceptionToFile();
}
public TurabsCriticalException(string message) : base(message)
{
MessageInternal = message;
saveExceptionToFile();
}
public TurabsCriticalException(string message, Exception inner) :
base(message, inner)
{
t = inner;
MessageInternal = message;
saveExceptionToFile();
}
protected TurabsCriticalException(SerializationInfo info, StreamingContext
context) : base(info,context)
{
m_strMachineName = info.GetString("m_strMachineName");
}
public override void GetObjectData(SerializationInfo info, StreamingContext
context)
{
info.AddValue("m_strMachineName", m_strMachineName, typeof(String));
info.AddValue("m_DateAndTIme", m_DateAndTime,typeof(DateTime));
base.GetObjectData (info, context);
}
private string m_strMachineName = Environment.MachineName;
public string MachineName
{
get{return m_strMachineName;}
set{m_strMachineName = value;}
}
private string m_MessageInternal = string.Empty;
public string MessageInternal
{get{return m_MessageInternal;}
set{m_MessageInternal = value;}
}
private DateTime m_DateAndTime = DateTime.Now;
public DateTime DateAndTime
{get{return m_DateAndTime;}
set{m_DateAndTime = value;}
}
protected void saveExceptionToFile()
{
try
{
FileStream fs = new FileStream(@"c:\Exception.txt" , FileMode.OpenOrCreate,
FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("===== EXCEPTION LOG FILE ======");
m_streamWriter.WriteLine(MachineName.ToString());
m_streamWriter.WriteLine(DateAndTime.ToShortTimeString());
m_streamWriter.Flush();
fs.Close();
}
catch(Exception ex)
{
string e = ex.Message;
//Write to the System log instead
}
finally
{
}
}
========DERRIVEDCLASSEXCEPTION==========
public class TurabsCriticalDatabaseException : TurabsCriticalException
{
public TurabsCriticalDatabaseException(string message) : base(message)
{
}
}
========TESTCLASS==========
public class DB
{
}
public int DbStuff()
{
throw new TurabsExceptions.TurabsCriticalDatabaseException("Server having
problem!");
}
}
========TESTCODE==========
DB db = new DB();
try
{
db.DbStuff(6);
}
catch(TurabsCriticalException ex)
{
// EXCEPTION HAS BEEN WRITTEN TO FILE
}