I was currently going through one process in which i need to fix some urgent issues. As a developer we normally use error log in our server side code but what if there some Database logging as well.
let suppose your end user face any issue then you need to replicate the same scenario again by connecting you code with database and passing the same arguments it is time consuming obviously.
So i was thinking if i know the exact parameter list of user and at what line database error occur so I can fix easily and quickly .
So I create a table structure like this you can add more columns if you like but for me its quite enough to rectify the issue.
Table Structure:
CREATE TABLE [StoreProcedureLog](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[EntryDatetime] [datetime] NULL,
[ErrorLine] [int] NULL,
[ErrorMessage] [nvarchar](1000) NULL,
[ProcedureName] [varchar](1000) NULL,
[AdditionalInfo] [varchar](1000) NULL,
[Status] [int] NULL,
CONSTRAINT [PK_StoreProcedureLog] PRIMARY KEY CLUSTERED
(
[ID] 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
ALTER TABLE [StoreProcedureLog] ADD CONSTRAINT [DF_StoreProcedureLog_EntryDatetime] DEFAULT (getdate()) FOR [EntryDatetime]
GO
ALTER TABLE [StoreProcedureLog] ADD CONSTRAINT [DF_StoreProcedureLog_Status] DEFAULT ((1)) FOR [Status]
GO
let me explain column one by one
ID = Auto Increment Column
EntryDatetime = It give you idea at what time error occur and no need to write insert for this as i set default value is Getdate()
ErrorLine = It will tell you the exact line number of error
ErrorMessage = It will tell you the error message
ProcedureName = It will tell you the name of procedure
AdditionalInfo = Pass extra information for this I normally pass my parameters list so I can know what was the exact scenario
Status= I set default value is 1 ( I use this when I fix the issue I marked that status =0 Its mean i solve that issue)
Store Procedure :
CREATE PROCEDURE Insertsplog @ERROR_PROCEDURE NVARCHAR(max) = NULL,
@AdditionalInfo NVARCHAR(max) = NULL
AS
BEGIN
--SET NOCOUNT ON;
IF EXISTS (SELECT 1
FROM information_schema.tables
WHERE table_name = 'StoreProcedureLog')
BEGIN
INSERT INTO storeprocedurelog
(errorline,
errormessage,
procedurename,
additionalinfo)
VALUES (Error_line(),
Error_message(),
@ERROR_PROCEDURE,
@AdditionalInfo);
END
END
Calling SP:
CREATE PROC Sp
AS
BEGIN
BEGIN try
-- Your Sp logic here
END try
BEGIN catch
IF EXISTS (SELECT 1
FROM information_schema.routines
WHERE routine_name = 'InsertSpLog')
BEGIN
DECLARE @ERROR_PROCEDURE NVARCHAR(1000);
DECLARE @Comments NVARCHAR(1000)
SET @Comments='Id: ' +CONVERT(varchar(10), @Id) -- Passing Information so easy to find the root cause for crashing the store procedure
SET @ERROR_PROCEDURE=Error_procedure(); -- it will pass the Store Proc name
--EXEC Insertsplog @ERROR_PROCEDURE -- Passing without Comments
EXEC Insertsplog @ERROR_PROCEDURE,@Comments -- Passing with Comments
END
END catch
END
This was the following result
let suppose your end user face any issue then you need to replicate the same scenario again by connecting you code with database and passing the same arguments it is time consuming obviously.
So i was thinking if i know the exact parameter list of user and at what line database error occur so I can fix easily and quickly .
So I create a table structure like this you can add more columns if you like but for me its quite enough to rectify the issue.
Table Structure:
CREATE TABLE [StoreProcedureLog](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[EntryDatetime] [datetime] NULL,
[ErrorLine] [int] NULL,
[ErrorMessage] [nvarchar](1000) NULL,
[ProcedureName] [varchar](1000) NULL,
[AdditionalInfo] [varchar](1000) NULL,
[Status] [int] NULL,
CONSTRAINT [PK_StoreProcedureLog] PRIMARY KEY CLUSTERED
(
[ID] 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
ALTER TABLE [StoreProcedureLog] ADD CONSTRAINT [DF_StoreProcedureLog_EntryDatetime] DEFAULT (getdate()) FOR [EntryDatetime]
GO
ALTER TABLE [StoreProcedureLog] ADD CONSTRAINT [DF_StoreProcedureLog_Status] DEFAULT ((1)) FOR [Status]
GO
let me explain column one by one
ID = Auto Increment Column
EntryDatetime = It give you idea at what time error occur and no need to write insert for this as i set default value is Getdate()
ErrorLine = It will tell you the exact line number of error
ErrorMessage = It will tell you the error message
ProcedureName = It will tell you the name of procedure
AdditionalInfo = Pass extra information for this I normally pass my parameters list so I can know what was the exact scenario
Status= I set default value is 1 ( I use this when I fix the issue I marked that status =0 Its mean i solve that issue)
Store Procedure :
CREATE PROCEDURE Insertsplog @ERROR_PROCEDURE NVARCHAR(max) = NULL,
@AdditionalInfo NVARCHAR(max) = NULL
AS
BEGIN
--SET NOCOUNT ON;
IF EXISTS (SELECT 1
FROM information_schema.tables
WHERE table_name = 'StoreProcedureLog')
BEGIN
INSERT INTO storeprocedurelog
(errorline,
errormessage,
procedurename,
additionalinfo)
VALUES (Error_line(),
Error_message(),
@ERROR_PROCEDURE,
@AdditionalInfo);
END
END
Calling SP:
CREATE PROC Sp
AS
BEGIN
BEGIN try
-- Your Sp logic here
END try
BEGIN catch
IF EXISTS (SELECT 1
FROM information_schema.routines
WHERE routine_name = 'InsertSpLog')
BEGIN
DECLARE @ERROR_PROCEDURE NVARCHAR(1000);
DECLARE @Comments NVARCHAR(1000)
SET @Comments='Id: ' +CONVERT(varchar(10), @Id) -- Passing Information so easy to find the root cause for crashing the store procedure
SET @ERROR_PROCEDURE=Error_procedure(); -- it will pass the Store Proc name
--EXEC Insertsplog @ERROR_PROCEDURE -- Passing without Comments
EXEC Insertsplog @ERROR_PROCEDURE,@Comments -- Passing with Comments
END
END catch
END
This was the following result
No comments:
Post a Comment