Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Jun 13 '12 at 11:03

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

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.

share|improve this answer
DAC does not fire logon trigger, so can use that in case of problems. – Jānis Jun 12 '12 at 15:39
Thanks Levi. I created a trigger. Quick and easy. Much appreciated. – Sabrish Ayyappan Jun 13 '12 at 9:53

Have you configured login auditing?

share|improve this answer
Yes. But that doesnt log datbasename. – Sabrish Ayyappan Jun 12 '12 at 14:45
Hmmm... How are you reading the audit log? fn_get_audit_file() may be helpful. database_name is listed as part of the data that is returned. – Rick Liddle Jun 12 '12 at 14:49
I was just using the default log viewer. However I tried using the fn_get_audit_file() function, but it returned no rows for the file. (it gets logged to the error file. link) – Sabrish Ayyappan Jun 12 '12 at 15:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.