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.
Add an project for exception, add a class file to the project
there you can write below code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
public class ApplicationException
{
public static Exception WriteToDB(Exception ex)
{
if (ex.InnerException == null)
{
return new Exception("Stack Trace:" + ex.StackTrace + " Message: " + ex.Message, ex);
}
else
{
return ex;
}
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.
vaibhav.1701...
Member
4 Points
10 Posts
How to log an exception into database (SQL server/express) using exception handling application b...
Dec 23, 2011 02:10 PM|LINK
Hi All,
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.
Vaibhav
smirnov
All-Star
24624 Points
4192 Posts
Re: How to log an exception into database (SQL server/express) using exception handling applicati...
Dec 23, 2011 02:18 PM|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.
cnranasinghe
Star
8899 Points
1800 Posts
Re: How to log an exception into database (SQL server/express) using exception handling applicati...
Jan 11, 2012 04:24 AM|LINK
Add an project for exception, add a class file to the project
there you can write below code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
public class ApplicationException
{
public static Exception WriteToDB(Exception ex)
{
if (ex.InnerException == null)
{
return new Exception("Stack Trace:" + ex.StackTrace + " Message: " + ex.Message, ex);
}
else
{
return 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[0]);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage2", DbType.String, formatMessge[1]);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage3", DbType.String, formatMessge[2]);
db.AddInParameter(dbCommandInsertEventLog, "@sMessage4", DbType.String, formatMessge[3]);
db.AddOutParameter(dbCommandInsertEventLog, "@iLogId", DbType.Int32, 4);
db.ExecuteNonQuery(dbCommandInsertEventLog);
return db.GetParameterValue(dbCommandInsertEventLog, "@iLogId").ToString();
}
}
Write following in your methods inside the catch
Ex:
private void BtnSaveClick_Click(object sender, EventArgs e)
{
try
{
}
catche(Exception ex)
{
throw Exceptions.ApplicationExceptionAssetValuationException.WriteToDB(ex);
}
}
this will write exception information to DB.
vaibhav.1701...
Member
4 Points
10 Posts
Re: How to log an exception into database (SQL server/express) using exception handling applicati...
Jan 18, 2012 11:35 AM|LINK
Thank You. I have it working now.
castlehills
Member
259 Points
171 Posts
Re: How to log an exception into database (SQL server/express) using exception handling applicati...
Feb 13, 2012 11:58 AM|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.