Pages

Men

rh

7/21/2014

How to log errors to data base in asp.net with c#


Please add the code into class file calles utility.cs



 public string LogErrorIntoDB(Exception ex)
        {
            Exception exBase = ex.GetBaseException();

            SqlParameter[] param = new SqlParameter[] {
                new SqlParameter(PARAM_ERROR_MESSAGE, ((exBase.Message==null)||(exBase.Message==string.Empty))?null:exBase.Message),
                new SqlParameter(PARAM_ERROR_SOURCE, ((exBase.Source==null)||(exBase.Source==string.Empty))?null:exBase.Source),
                new SqlParameter(PARAM_ERROR_STACK_TRACE, ((exBase.StackTrace==null)||(exBase.StackTrace==string.Empty))?null:exBase.StackTrace),
                new SqlParameter(PARAM_ERROR_MEMBER_NAME, ((exBase.TargetSite.Name==null)||(exBase.TargetSite.Name==string.Empty))?null:exBase.TargetSite.Name),
                new SqlParameter("@Out_msg",System.Data.SqlDbType.BigInt)};

            param[0].SqlDbType = System.Data.SqlDbType.VarChar;
            param[1].SqlDbType = System.Data.SqlDbType.VarChar;
            param[2].SqlDbType = System.Data.SqlDbType.VarChar;
            param[3].SqlDbType = System.Data.SqlDbType.VarChar;
            param[4].Direction = System.Data.ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(Statics.connectionString, System.Data.CommandType.StoredProcedure, "UDP_ERROR_LOG", param);
            return param[4].Value.ToString();
        }



How to call the above method in asp.net using c#

utility objutility = new utility();
 String strMessage = String.empty;

        public void UserLogininformation(Int64 intUserId, String strUserStatus)
        {
            try
            {

            -----------------
             ------
             ----------------
            }
            catch (Exception ex)
            {
              strMessage = objUtility.LogErrorIntoDB(ex);
            }
        }


Please find the Table as well as procedure in below.


Table structure :-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ErrorLogInfo](
    [ErrorID] [bigint] IDENTITY(1,1) NOT NULL,
    [ErrorMessage] [nvarchar](max) NULL,
    [ErrorSource] [nvarchar](max) NULL,
    [Error_Stack_Trace] [nvarchar](max) NULL,
    [MemberName] [nvarchar](max) NULL,
    [OutErrorMessage] [bigint] NULL,
    [TimeStamp] [datetime] NULL,
 CONSTRAINT [PK_ErrorLogInfo] PRIMARY KEY CLUSTERED
(
    [ErrorID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


Stored Procedure :-



GO

/****** Object:  StoredProcedure [dbo].[UDP_ERROR_LOG]    Script Date: 07/21/2014 10:12:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[UDP_ERROR_LOG]
     @in_message nvarchar(250),
    @in_source    nvarchar(250),
    @in_stack_trace nvarchar(250),
    @in_member_name nvarchar(250),
    @Out_msg BIGINT OUTPUT

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    INSERT INTO dbo.ErrorLogInfo
    (
        ErrorMessage,
        ErrorSource,
        Error_Stack_Trace,
        MemberName,
        [TimeStamp]
    )
    VALUES
    (
     @in_message,
     @in_source,
     @in_stack_trace,
     @in_member_name,
     GETDATE()
    )
       
    SET @Out_msg = (SELECT MAX(ErrorID) FROM dbo.ErrorLogInfo)
    SELECT @Out_msg
   
END

GO


 






 




No comments :

Post a Comment