How to Log Successful Login Attempts to a Database in SQL Server

sql serversql-server-2008

Is there a way to log database name when auditing logins in SQL Server (2005 and above)?

I basically want to log the username and the database name the user successfully connected to.

I have tried profiler, databasename column is blank.

Best Answer

Your best bet is to use a Logon Trigger

Just make sure your trigger captures SYSTEM_USER and ORIGINAL_DB_NAME() (this is only available in SQL 2008 +).

Just be careful with logon triggers. If you mess them up you can prevent any logins, because when they fail they cause the login to fail and then you have to use a workaround to get into the system and drop the trigger so you can fix it.

Don't let that hinder you though, just know you need to test them out thoroughly before you go throwing them onto a production server.