locked
How to log an exception into database (SQL server/express) using exception handling application block RRS feed

  • Question

  • User-566454918 posted

    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

    Friday, December 23, 2011 10:10 AM

All replies

  • User1508394307 posted

    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.

    Friday, December 23, 2011 10:18 AM
  • User-37275327 posted

    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.

    Wednesday, January 11, 2012 12:24 AM
  • User-566454918 posted

    Thank You. I have it working now.

    Wednesday, January 18, 2012 7:35 AM
  • User263675842 posted

    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.

     

    Monday, February 13, 2012 7:58 AM