Skip to content

Instantly share code, notes, and snippets.

@marcusholmgren
Created February 25, 2020 19:51
Show Gist options
  • Save marcusholmgren/0b89eec65eff3fd4ddff24d6cf1068ca to your computer and use it in GitHub Desktop.
Save marcusholmgren/0b89eec65eff3fd4ddff24d6cf1068ca to your computer and use it in GitHub Desktop.
Trigger firing when users log on to the server
-- Create a trigger firing when users log on to the server
CREATE TRIGGER LogonAudit
-- Use ALL SERVER to create a server-level trigger
ON ALL SERVER WITH EXECUTE AS 'sa'
-- The trigger should fire after a logon
AFTER LOGON
AS
-- Save user details in the audit table
INSERT INTO ServerLogonLog (LoginName, LoginDate, SessionID, SourceIPAddress)
SELECT ORIGINAL_LOGIN(), GETDATE(), @@SPID, client_net_address
FROM SYS.DM_EXEC_CONNECTIONS WHERE session_id = @@SPID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment