Google+ All posts tagged 'EXCEPTIONS'

Peter Bromberg's .NET Blog All Things Programming

Keep It Simple Stupid Easy Database Exception Logger

12. June 2021 14:11 by admin in EXCEPTIONS, LOGGING

There are a number of very nice logging frameworks like Serilog, Log4net, ELMAH, NLog, and others. Almost without exception, they require fancy configuration setups because they were designed to log stuff in several different ways.  For beginning developers and even for some of us seasoned pros, set up and usage can be daunting. All I want is to be able to write a message, stack trace, and date of occurrence to my database Logs table and that's it. So I came up with KISSEDEL: Keep It Simple Stupid Easy Database Exception Logger. You can plug this into any app that uses a database and be going in five minutes!

First let's create a table in SQL Server:

 USE [DBName]
GO

    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE[dbo].[Logs]
    (
   [Id][int] IDENTITY(1,1) NOT NULL,
   [Date] [datetime]     NOT NULL,
  [Message] [varchar] (4000) NOT NULL,
   [Exception] [varchar] (2000) NOT NULL,
CONSTRAINT[PK_Log] PRIMARY KEY CLUSTERED
   [Id] ASC)
WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON[PRIMARY]
) ON[PRIMARY]
GO

Next configure your connection string in app.Config or web.config:

<connectionStrings>
<add name ="con" connectionString="Data Source=.;Initial Catalog=YOURDATBASE;Trusted_Connection=true"/>
</connectionStrings>

The only other thing we need is a class to handle logging:

using System;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

public static class Logger

{

    public static void Log(Exception ex)

    {

        string constr = ConfigurationManager.ConnectionStrings["con"].ToString();

        SqlConnection con;

        con = new SqlConnection(constr);

        con.Open();

        SqlCommand com = new SqlCommand("Log", con);

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.AddWithValue("@Date", DateTime.Now);

        com.Parameters.AddWithValue("@Message", ex.Message.ToString());

        com.Parameters.AddWithValue("@Exception", ex.StackTrace.ToString());

        com.ExecuteNonQuery();

        con.Close();

    }

}

Finally, we want a stored proc to make the inserts:

USE [YOURDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE Proc [dbo].[Log]
@Date DateTime,
@Message varchar(500),
@Exception varchar(8000)
AS
insert into logs(Date, Message, Exception)
VALUES (@date,@message, @exception)

 

That's all there is to it. If you want to log something "Informational", just create a new Exception object and put the information in its Message property.

usage:

 try
                {
                  // your work here

                }

                catch (Exception ex)

                {

                    Logger.Log(ex);

                } 

 Have fun!