Last post Feb 13, 2012 07:58 AM by castlehills
Dec 23, 2011 10:10 AM|vaibhav.1701sh|LINK
Please help if You can. I need a very simple sample application (or just the code is enough) just to know if an exception occurs in my code, How will I target it to the database. I mean how should I log it to the databse. How should my web.config look like..I
mean the <exceptionHandling> and <loggingConfiguration>. Also, How should I use the try-catch so that my exception gets logged into the database.
Thanks a lot in advance.
Dec 23, 2011 10:18 AM|smirnov|LINK
Take a look at ELMAH. It's a module that you can plugin into your application to log errors either in SQL Server or another database.
Jan 11, 2012 12:24 AM|cnranasinghe|LINK
Add an project for exception, add a class file to the project
there you can write below code
public class ApplicationException
public static Exception WriteToDB(Exception ex)
if (ex.InnerException == null)
return new Exception("Stack Trace:" + ex.StackTrace + " Message: " + ex.Message, ex);
//// Write to DB
public static string WriteEventLogs(Exception ex, string className, string functionName, string userName)
Database db = DatabaseFactory.CreateDatabase(your DB connectionStringname);
//For Insert Event data.
DbCommand dbCommandInsertEventLog = db.GetStoredProcCommand(Constants.SP_LogMessages_Ins);
db.AddInParameter(dbCommandInsertEventLog, "@sUserName", DbType.String, userName);
db.AddInParameter(dbCommandInsertEventLog, "@sClassName", DbType.String, className);
db.AddInParameter(dbCommandInsertEventLog, "@sFunctionName", DbType.String, functionName);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage1", DbType.String, formatMessge);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage2", DbType.String, formatMessge);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage3", DbType.String, formatMessge);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage4", DbType.String, formatMessge);
db.AddOutParameter(dbCommandInsertEventLog, "@iLogId", DbType.Int32, 4);
return db.GetParameterValue(dbCommandInsertEventLog, "@iLogId").ToString();
Write following in your methods inside the catch
private void BtnSaveClick_Click(object sender, EventArgs e)
this will write exception information to DB.
Jan 18, 2012 07:35 AM|vaibhav.1701sh|LINK
Thank You. I have it working now.
Feb 13, 2012 07:58 AM|castlehills|LINK
I would recommend the logging application block from the Microsoft Enterprise Library. I don't think you can use just ELMAH to log exceptions. You may have to use either log4net or similar loggers. I used ELMAH and log4net before and it
is useless after the exception store grows. ELMAH can't perform very well. I had to then save the log in a text file. The log is saved as chunks and it was easy to access that way.