Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Created November 12, 2024 23:23
Show Gist options
  • Save nanoDBA/16d017b23f017e2ae76dd42704d339bb to your computer and use it in GitHub Desktop.
Save nanoDBA/16d017b23f017e2ae76dd42704d339bb to your computer and use it in GitHub Desktop.
Creates SQL Server Service Broker endpoint on port 4022 to reduce noise in the SQL Server error log (ERRORLOG). Includes port availability checks, conditional creation, logging, and optional code to stop and drop endpoint if needed.
/* Add script to create Service Broker endpoint for log noise reduction
source idea: https://slavasql.blogspot.com/2019/11/errorlog-flooded-with-service-broker.html
- Creates ServiceBroker_LogNoiseReduction endpoint on port 4022
- Includes checks for port availability and existing endpoint
- Adds conditional logging for successful creation, start, and errors
- Optional code to stop and drop endpoint if needed
50001: Used for notifying if port 4022 is already in use.
50002: Indicates the endpoint was created and started successfully.
50003: Logs a failure message if the endpoint creation or start process fails.
50004: Confirms that the endpoint already exists.
50005: Logs that the endpoint was successfully dropped (in the optional drop section).
*/
-- Check if port 4022 is already in use by an endpoint
IF EXISTS (
SELECT 1
FROM sys.tcp_endpoints
WHERE protocol_desc = 'TCP'
AND port = 4022
AND name <> 'ServiceBroker_LogNoiseReduction'
)
BEGIN
RAISERROR('Port 4022 is already in use by another endpoint or service.', 16, 1) WITH NOWAIT
EXEC xp_logevent 50001, 'Port 4022 is already in use by another endpoint or service.', error
RETURN
END
-- Check if endpoint already exists
IF NOT EXISTS (
SELECT 1
FROM sys.tcp_endpoints
WHERE name = 'ServiceBroker_LogNoiseReduction'
)
BEGIN
CREATE ENDPOINT ServiceBroker_LogNoiseReduction
STATE = STARTED
AS TCP (LISTENER_PORT = 4022, LISTENER_IP = ALL)
FOR SERVICE_BROKER (AUTHENTICATION = WINDOWS)
-- Check that endpoint was successfully created and started
IF EXISTS (
SELECT 1
FROM sys.tcp_endpoints
WHERE name = 'ServiceBroker_LogNoiseReduction'
AND state_desc = 'STARTED'
)
BEGIN
RAISERROR('Endpoint ServiceBroker_LogNoiseReduction successfully created and started.', 0, 1) WITH NOWAIT
EXEC xp_logevent 50002, 'Endpoint ServiceBroker_LogNoiseReduction successfully created and started.', informational
END
ELSE
BEGIN
RAISERROR('Failed to create and start the endpoint ServiceBroker_LogNoiseReduction.', 0, 1) WITH NOWAIT
EXEC xp_logevent 50003, 'Failed to create and start the endpoint ServiceBroker_LogNoiseReduction.', error
END
END
ELSE
BEGIN
RAISERROR('Endpoint ServiceBroker_LogNoiseReduction already exists.', 0, 1) WITH NOWAIT
EXEC xp_logevent 50004, 'Endpoint ServiceBroker_LogNoiseReduction already exists.', informational
END
-- Optional: Commented code to drop endpoint if needed
/*
IF EXISTS (
SELECT 1
FROM sys.tcp_endpoints
WHERE name = 'ServiceBroker_LogNoiseReduction'
)
BEGIN
ALTER ENDPOINT ServiceBroker_LogNoiseReduction
STATE = STOPPED
DROP ENDPOINT ServiceBroker_LogNoiseReduction
-- Confirm successful drop if endpoint no longer exists
IF NOT EXISTS (
SELECT 1
FROM sys.tcp_endpoints
WHERE name = 'ServiceBroker_LogNoiseReduction'
)
BEGIN
RAISERROR('Endpoint ServiceBroker_LogNoiseReduction has been dropped.', 0, 1) WITH NOWAIT
EXEC xp_logevent 50005, 'Endpoint ServiceBroker_LogNoiseReduction has been dropped.', informational
END
END
-- Query existing Service Broker endpoints
SELECT
name
, protocol_desc
, type_desc
, state_desc
, port
FROM sys.tcp_endpoints
WHERE type_desc = 'SERVICE_BROKER'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment