Take the 2-minute tour ×
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. It's 100% free, no registration required.

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 stackoverflow.com Jun 13 '12 at 11:03

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

add comment

2 Answers 2

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()

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
add comment

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
add comment

Your Answer

 
discard

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.