Best Practice: which database should I use when creating an audit table for a SQL SERVER LOGON TRIGGER

Abrams, Dan 86 Reputation points
2020-12-16T19:03:09.453+00:00

Hi All,

I have a requirement to create a LOGON TRIGGER to monitor logins for auditing purposes. The requirement is to create a table and capture all known logins. I am using SQL Server 2017, enterprise edition.

I'm curious which database I should use to store this information. If login attempts are made before my user database is online, how does the trigger behave?

My trigger is designed to only monitor sys.dm_exec_sessions.is_user_process = 1 processes, and capture certain identifying information. At the moment, I am storing the results in a table I created in master.

All inserts/updates are wrapped inside of TRY/CATCH logic

Which database should I use to create the audit table?

Thank you!

--Dan

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


5 answers

Sort by: Most helpful
  1. Abrams, Dan 86 Reputation points
    2020-12-16T22:52:41.147+00:00

    Thank you very much Erland!

    This is exactly what I was looking for!

    --Dan

    Was this answer helpful?

    0 comments No comments

  2. Abrams, Dan 86 Reputation points
    2020-12-16T22:34:53.11+00:00

    Thank you Erland,

    I am also finding that because I am reference sys.dm_exec_sessions and sys.dm_exec_connections I am running into VIEW SERVER STATE permission problems.

    If I implement your example, should I assign "VIEW SERVER STATE" permission to 'LoginTriggerUser'?

    Thanks again!

    --Dan

    Was this answer helpful?


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Abrams, Dan 86 Reputation points
    2020-12-16T21:44:49.843+00:00

    Hi GuoxiongYuan-7218,

    Thank you for your response. This is what I was thinking as well.

    One other question: For some users, my trigger is properly logging the information to the table, but not for all users. In some cases, it is preventing them from logging into SQL Server! (This is still a development project!).

    What privs do I need to assign to my users, target table, and trigger in order to get this to work. Prior to installing the trigger, all users were able to connect to the server successfully.

    Thank you!

    --Dan

    Was this answer helpful?

    0 comments No comments

  5. Guoxiong 8,146 Reputation points
    2020-12-16T20:43:11.127+00:00

    I would suggest to create the audit table in the master database since it always exists if the SQL server is online.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.