Sql-server – way to query SQL Server 2005/2008 for location of its log files

logssql serversql-server-2005sql-server-2008

I there a way to query SQL Server 2005+ for the file system location of its various log files. By that I mean the text log files, not database transaction log files?

For example my SQL Agent, Error Logs and maintenance plans all write to a folder called:

D:\MSSQLData\MSSQL.1\MSSQL\LOG

enter image description here

It's this folder I need to find the name of.

Updated:

As well as hunting SQL Books I had a poke around the registry under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer

and

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQLSERVER` 

but nothing jumps out at me.

I'd also like an "official" way to achieve this and would rather not rely on undocumented features.

Best Answer

SELECT REPLACE(CAST(SERVERPROPERTY('ErrorLogFileName') AS nvarchar(512)), 'ERRORLOG', '')

EDIT: Cleaned it up a bit with a little more googling to verify; Also to note that this particular argument "ErrorLogFileName" to the SERVERPROPERTY function is undocumented and therefore unsupported by Microsoft. Use at your own risk. etc. etc.