How to log exception into database using C#.Net




In this article, I am going to explain you how to log exception into database using C#.Net. We will be using C# console application to log exception into database.

Creating Database Table

We will be using tblArticles table to log exception details. Use below script to create table.

create table tbl_ErrorLog
(
[Id] int primary key identity(1,1),
[LoggedDate] DateTime,
[ExceptionMessage] nvarchar(max)
)

Creating Procedure

Use below script to create procedure. Here, procedure takes only one parameter exception message.

create procedure usp_ErrorLog
@ExceptionMessage nvarchar(max)
as
begin
 insert into tbl_ErrorLog([LoggedDate], [ExceptionMessage]) values (Getdate(), @ExceptionMessage)
end

Creating Console Application

Create a console application. LogError method is used to log exception into database. We are intentionally throwing exception in main method.


using System;
using System.Collections.Generic;
using System.Configuration; //add namespace
using System.Data; //add namespace
using System.Data.SqlClient; //add namespace
using System.Linq;
using System.Text; //add namespace
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main( string[] args)
    {
        try
        {
            // throw exception intentionally
            throw new DivideByZeroException();
        }
        catch (Exception ex)
        {
            LogError(ex);
        }
    }

    public static void LogError( Exception exception)
    {
        string conString = @"Data Source=.\;Initial Catalog=Demo;User ID=id;Password=pwd" ;
        SqlConnection con = null;
        try
        {
            StringBuilder sbExceptionMessage = new StringBuilder();
            do
            {
                sbExceptionMessage.Append( "Exception Type" + Environment.NewLine);
                sbExceptionMessage.Append(exception.GetType().Name);
                sbExceptionMessage.Append(Environment .NewLine + Environment.NewLine);
                sbExceptionMessage.Append("Message" + Environment.NewLine);
                sbExceptionMessage.Append(exception.Message + Environment .NewLine + Environment.NewLine);
                sbExceptionMessage.Append( "Stack Trace" + Environment.NewLine);
                sbExceptionMessage.Append(exception.StackTrace + Environment .NewLine + Environment.NewLine);
                exception = exception.InnerException;
            }
            while (exception != null);

            con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("usp_ErrorLog" , con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter = new SqlParameter("@ExceptionMessage" , sbExceptionMessage.ToString());
            cmd.Parameters.Add(parameter);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }
}
}

After running application, exception will be logged as shown below.

How to log exception into database using C#.Net


Share This


blog comments powered by Disqus