How to save Request and Response

Опубликовано: 21 Июль 2026
на канале: TechTutorHub
48
0

In this video we learn how to add HttpLogging middleware and what it does in terms of logging the request and response information in the SQL Server Database.

1) Database code;

CREATE TABLE [dbo].[tblServiceAccessLog](
[ID] [int] IDENTITY(-2147483648,1) NOT NULL,
[MessageType] [varchar](10) NULL,
[MessageTime] [smalldatetime] NULL,
[Message] [varchar](500) NULL,
[MessageContent] [varchar](2000) NULL,
[RemoteHost] [varchar](100) NULL,
[Duration] [int] NULL,
CONSTRAINT [PK_tblServiceAccessLog] 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



Create PROCEDURE [dbo].[uspServiceAccessLog]
@messageType VARCHAR(MAX),
@message varchar(MAX),
@messageContent varchar(MAX),
@RemoteHost varchar(MAX),
@ResponseTime int,
@responseMessage varchar(250) OUTPUT
AS
BEGIN
SET NOCOUNT ON

BEGIN TRY

INSERT INTO dbo.[tblServiceAccessLog] (MessageType, MessageTime, [Message], MessageContent, RemoteHost, Duration)
VALUES(@messageType, GETDATE(), @message, @messageContent, @RemoteHost, @ResponseTime)

SET @responseMessage = 'Success'

END TRY
BEGIN CATCH
SET @responseMessage = ERROR_MESSAGE()
END CATCH

END