Thursday, January 12, 2006

Write Error Message in Log File (C# DOTNET).

Write Error Message in Log/Text File (C# DOTNET).


1) Write in Code behind


private void Page_Load(object sender, System.EventArgs e)
{

try
{
int k = i/0; // Zero
}
catch (Exception ex)
{
writeLog("Error Found on " + DateTime);
writeLog("=================");
writeLog(ex.Message);
}

}

private void writeLog(string message)
{
try
{
string logFile = String.Empty;
StreamWriter logWriter;
logFile = ConfigurationSettings.AppSettings["LogFilePath"];
if (File.Exists(logFile))
{
logWriter = File.AppendText(logFile);
}
else
{
logWriter = File.CreateText(logFile);
}
logWriter.WriteLine(message);
logWriter.Close();
}
catch(Exception e)
{
Console.Out.WriteLine("Error in writing log :" + e.Message);
}
}

2) Write in the Config File





Ritesh Kumar Kesharwani
A D I T I , B A N G A L O R E
Software Professional
Cell : 91-9845657636
Page:
www.Riteshk.blogspot.com

Yahoo! Photos Got holiday prints?
See all the ways to get quality prints in your hands ASAP.