%3CLINGO-SUB%20id%3D%22lingo-sub-1229920%22%20slang%3D%22en-US%22%3ESQL%20Server%20Alwayson%20primary%20replica%20failed%20to%20create%20diagnostics%20log.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1229920%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%20Alwayson%20diagnostics%20log%20is%20a%20useful%20tool%20to%20troubleshoot%20SQL%20Server%20Alwayson%20related%20issue.%20It%20saves%20the%20result%20of%20stored%20procedure%20system%20stored%20procedure%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-server-diagnostics-transact-sql%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esp_server_diagnostics%20(Transact-SQL)%3C%2FA%3E%26nbsp%3B%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CI%3ESQLDIAG%3C%2FI%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efile%2C%20the%20naming%20convention%20is%20serverName_instanceName_SQLDIAG_xxx.xel.%3C%2FP%3E%0A%3CP%3EThis%20log%20is%20only%20created%20in%20SQL%20Server%20cluster%20active%20node%20or%20SQL%20Server%20Alwayson%20Availability%20group%20primary%20replica.%20Please%20note%2C%20it%E2%80%99s%20created%20by%20RHS.exe%20instead%20of%20SQL%20Server.%3C%2FP%3E%0A%3CP%3EFollowing%20screenshot%20of%20process%20monitor%20shows%20callstack%20of%20creating%20the%20diagnostic%20log%20by%20RHS.exe.%20As%20you%20see%2C%20RHS.exe%2C%20hadrres.dll%20and%20xe.dll%20are%20used.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorLiwei_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22processMonitor.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F177073iF9CC7834332AEFB8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22processMonitor.png%22%20alt%3D%22processMonitor.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWhen%20SQL%20Server%20is%20installed%2C%20it%20creates%20hadrres.dll%20in%20MSSQL%5CBinn%20folder%20and%20creates%20xe.dll%20in%20C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5Cversion%5CShared%5C%20folder%20by%20default%20%2C%20it%20also%20registers%20the%20hadrres.dll%20in%20c%3A%5Cwindows%5Csystem32.%3C%2FP%3E%0A%3CP%3EIf%20more%20than%20one%20SQL%20Server%20versions%20are%20installed%2C%20the%20highest%20version%20of%20hadrres.dll%20is%20registered%20in%20in%20c%3A%5Cwindows%5Csystem32%2C%20it%E2%80%99s%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EBackwards%20compatible%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20if%20you%20have%20following%20topology.%3C%2FP%3E%0A%3CP%3ESQL%20Box1%3A%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%201%3ASQL%20server%202012%20(Primary%20replica)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%202%3A%20SQL%20server%202014%20(this%20is%20a%20standalone%20instance%2C%20AG%20is%20not%20used)%3CBR%20%2F%3E%26nbsp%3B%20SQL%20Box2%3A%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%201%3ASQL%20server%202012%20(Secondary%20replica)%3C%2FP%3E%0A%3CP%3ESQL%202014%20version%20of%20hadrres.dll%20registered%20in%20c%3A%5Cwindows%5Csystem32%20in%20box1.%3C%2FP%3E%0A%3CP%3ESQL%202012%20version%20of%20hadrres.dll%20registered%20in%20c%3A%5Cwindows%5Csystem32%20in%20box2.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20box1%2C%20the%20RHS.exe%20will%20load%20the%20SQL%202014%20version%20hadrres.dll%2C%20which%20will%20loads%20the%20SQL%20Server%202014%20xe.dll%2C%20when%20the%20instance%201%20becomes%20primary%20replica.%3C%2FP%3E%0A%3CP%3EIn%20box2%2C%20the%20RHS.exe%20will%20load%20the%20SQL%202012%20version%20hadrres.dll%2C%20which%20will%20loads%20the%20SQL%20Server%202012%20xe.dll%2C%20when%20the%20instance%201%20becomes%20primary%20replica.%3C%2FP%3E%0A%3CP%3EThe%20location%20of%20xe.dll%20is%20saved%20in%20register%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5Cversion%5CSharedCode%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorLiwei_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20id%3D%22tinyMceEditorLiwei_2%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sharedCode.png%22%20style%3D%22width%3A%20874px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F177076i552CC7461EC157E2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22sharedCode.png%22%20alt%3D%22sharedCode.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHadrres.dll%20retrieves(hardcoded)%20the%20location%20of%20xe.dll%20by%20reading%20the%20register%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3Eversion%3C%2FSTRONG%3E%5CSharedCode%2C%20then%20load%20the%20xe.dll.%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3ESQL%202019%20%3A%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3E150%3C%2FSTRONG%3E%5CSharedCode%3C%2FP%3E%0A%3CP%3ESQL%202017%20%3A%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3E140%3C%2FSTRONG%3E%5CSharedCode%3C%2FP%3E%0A%3CP%3ESQL%202016%20%3A%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3E130%3C%2FSTRONG%3E%5CSharedCode%3C%2FP%3E%0A%3CP%3ESQL%202014%20%3A%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3E120%3C%2FSTRONG%3E%5CSharedCode%3C%2FP%3E%0A%3CP%3ESQL%202012%20%3A%20HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5C%3CSTRONG%3E110%3C%2FSTRONG%3E%5CSharedCode%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EAfter%20xe.dll%20is%20loaded%2C%20it%20will%20create%20the%20diagnostic%20log%20based%20on%20the%20configuration(check%20sys.dm_os_server_diagnostics_log_configurations)%3C%2FP%3E%0A%3CP%3EHere%20are%20some%20scenarios%20cause%20the%20diagnostic%20fail%20to%20be%20created%3A%3C%2FP%3E%0A%3CP%3E1.Diagnostic%20log%20is%20off.%3C%2FP%3E%0A%3CPRE%20class%3D%22code%22%3E%3CSPAN%3EALTER%20SERVER%20CONFIGURATION%20SET%20%3C%2FSPAN%3E%3CSPAN%3EDIAGNOSTICS%20%3C%2FSPAN%3E%3CSPAN%3ELOG%20%3C%2FSPAN%3E%3CSPAN%3Eoff%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E2.HKEY_LOCAL_MACHINE%5CSOFTWARE%5CMicrosoft%5CMicrosoft%20SQL%20Server%5Cversion%5CSharedCode%20does%20not%20exist%20or%20have%20invalid%20value.%3C%2FP%3E%0A%3CP%3E3.The%20file%20xe.dll%20retrieved%20from%20the%20register%20of%20step%202%20does%20not%20exist.%3C%2FP%3E%0A%3CP%3E4.Version%20of%20xe.dll%20does%20not%20match%20the%20version%20of%20hadrres.dll(it%20may%20cause%20issue%20or%20may%20not%2C%20depends%20on%20SQL%20version).%3C%2FP%3E%0A%3CP%3E5.RHS.exe%20does%20not%20have%20permission%20write%20file%20in%20the%20target%20location(check%20the%20DMV%20sys.dm_os_server_diagnostics_log_configurations).%3C%2FP%3E%0A%3CP%3E%E2%80%98Diagnostics%20log%E2%80%99%20related%20logs%3A%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EIf%20the%20diagnostic%20log%20is%20disabled%2C%20you%20will%20get%20below%20info%20from%20cluster%20log.%3C%2FP%3E%0A%3CP%3EINFO%20%5BRES%5D%20SQL%20Server%20Availability%20Group%3A%20%5Bhadrag%5D%20Extended%20Event%20logging%20is%20disabled%3C%2FP%3E%0A%3CP%3EIf%20the%20path%20changes%2C%20you%20will%20get%20below%20info%20from%20cluster%20log.%3C%2FP%3E%0A%3CP%3EINFO%20%5BRES%5D%20SQL%20Server%20Availability%20Group%3A%20%5Bhadrag%5D%20The%20property%20LogPath%20was%20changed%20from%20%E2%80%98%3F%3F%3F%E2%80%99%20to%20%E2%80%98%3F%3F%3F%3F%E2%80%99%3C%2FP%3E%0A%3CP%3EOnce%20the%20log%20file%20is%20created%2C%20you%20will%20get%20below%20info%20from%20cluster%20log.%3C%2FP%3E%0A%3CP%3EINFO%20%5BRES%5D%20SQL%20Server%20Availability%20Group%3A%20%5Bhadrag%5D%20Extended%20Event%20logging%20is%20started%3C%2FP%3E%0A%3CP%3EIf%20the%20log%20fails%20to%20created%2C%20you%20will%20get%20below%20info%20from%20cluster%20log.%3C%2FP%3E%0A%3CP%3EERR%26nbsp%3B%26nbsp%3B%20%5BRES%5D%20SQL%20Server%20Availability%20Group%3A%20%5Bhadrag%5D%20Failed%20to%20initialize%20Extended%20Event%20logging%20engine%20with%20error%20%5BErrorCode%5D%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%3CSTRONG%3EHere%20is%20the%20real%20case%20%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ECustomer%20had%20a%20SQL%202012%20Alwayson%20Availability%20group%20consisted%20of%20with%20three%20replicas.%20One%20of%20the%20replicas%20failed%20to%20create%20diagnostic%20log%20when%20it%20became%20primary%20replica.%20Other%20two%20replicas%20were%20able%20to%20create%20diagnostic%20logs.%3C%2FP%3E%0A%3CP%3EI%20launched%20process%20monitor%20against%20the%20RHS.exe%20in%20the%20replica%20in%20question.%20I%20found%20that%20the%20xe.dll%20was%20not%20loaded%2C%20and%20the%20hadrres.dll%20loaded%20was%20SQL%20Server%202014%20version%20instead%20of%20SQL%20Server%202012.%3C%2FP%3E%0A%3CP%3EDiscussed%20with%20customer%2C%20I%20found%20that%20there%20was%20SQL%20Server%202014%20was%20installed%2C%20then%20it%E2%80%99s%20removed.%20That%E2%80%99s%20the%20cause!%3C%2FP%3E%0A%3CP%3EI%E2%80%99m%20able%20to%20reproduce%20the%20issue%20in%20my%20box.%3C%2FP%3E%0A%3CP%3ESQL%20Box1%3A%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%201%3ASQL%20server%202012%20(Primary%20replica)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%202%3A%20SQL%20server%202014%20(this%20is%20a%20standalone%20instance%2C%20AG%20is%20not%20used)%3CBR%20%2F%3E%26nbsp%3B%20SQL%20Box2%3A%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Instance%201%3ASQL%20server%202012%20(Secondary%20replica)%3C%2FP%3E%0A%3CP%3ERemove%20the%20instance%202%2C%20then%20instance%201%20on%20box1%20can%E2%80%99t%20create%20diagnostic%20log.%3C%2FP%3E%0A%3CP%3EThe%20issue%20happens%20because%20uninstalling%20SQL%20Server%20instance%20removes%20the%20xe.dll%20in%20C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5C120%5CShared%2C%20but%20still%20leaves%20the%20SQL%202014%20hadrres.dll%20in%20c%3A%5Cwindows%5Csystem32.%3C%2FP%3E%0A%3CP%3EThis%20behavior%20causes%20hadrres.dll%20fail%20to%20load%20xe.dll%20because%20xe.dll%20is%20removed%20during%20uninstallation.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWorkaround%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3D%3D%3D%3C%2FP%3E%0A%3CP%3ECopy%20the%20same%20version%20SQL%202014%20xe.dll%20back%20to%20C%3A%5CProgram%20Files%5CMicrosoft%20SQL%20Server%5Cversion%5CShared.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1229920%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHigh%20Availability%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

SQL Server Alwayson diagnostics log is a useful tool to troubleshoot SQL Server Alwayson related issue. It saves the result of stored procedure system stored procedure sp_server_diagnostics (Transact-SQL)  to SQLDIAG file, the naming convention is serverName_instanceName_SQLDIAG_xxx.xel.

This log is only created in SQL Server cluster active node or SQL Server Alwayson Availability group primary replica. Please note, it’s created by RHS.exe instead of SQL Server.

Following screenshot of process monitor shows callstack of creating the diagnostic log by RHS.exe. As you see, RHS.exe, hadrres.dll and xe.dll are used.

 

processMonitor.png

When SQL Server is installed, it creates hadrres.dll in MSSQL\Binn folder and creates xe.dll in C:\Program Files\Microsoft SQL Server\version\Shared\ folder by default , it also registers the hadrres.dll in c:\windows\system32.

If more than one SQL Server versions are installed, the highest version of hadrres.dll is registered in in c:\windows\system32, it’s Backwards compatible.

For example, if you have following topology.

SQL Box1:
           Instance 1:SQL server 2012 (Primary replica)
           Instance 2: SQL server 2014 (this is a standalone instance, AG is not used)
  SQL Box2:
           Instance 1:SQL server 2012 (Secondary replica)

SQL 2014 version of hadrres.dll registered in c:\windows\system32 in box1.

SQL 2012 version of hadrres.dll registered in c:\windows\system32 in box2.

 

In box1, the RHS.exe will load the SQL 2014 version hadrres.dll, which will loads the SQL Server 2014 xe.dll, when the instance 1 becomes primary replica.

In box2, the RHS.exe will load the SQL 2012 version hadrres.dll, which will loads the SQL Server 2012 xe.dll, when the instance 1 becomes primary replica.

The location of xe.dll is saved in register HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\version\SharedCode

 
 

sharedCode.png

 

Hadrres.dll retrieves(hardcoded) the location of xe.dll by reading the register HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\version\SharedCode, then load the xe.dll.

SQL 2019 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\150\SharedCode

SQL 2017 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\140\SharedCode

SQL 2016 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\130\SharedCode

SQL 2014 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\120\SharedCode

SQL 2012 : HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\110\SharedCode

After xe.dll is loaded, it will create the diagnostic log based on the configuration(check sys.dm_os_server_diagnostics_log_configurations)

Here are some scenarios cause the diagnostic fail to be created:

1.Diagnostic log is off.

ALTER SERVER CONFIGURATION SET DIAGNOSTICS LOG off

2.HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\version\SharedCode does not exist or have invalid value.

3.The file xe.dll retrieved from the register of step 2 does not exist.

4.Version of xe.dll does not match the version of hadrres.dll(it may cause issue or may not, depends on SQL version).

5.RHS.exe does not have permission write file in the target location(check the DMV sys.dm_os_server_diagnostics_log_configurations).

‘Diagnostics log’ related logs:

If the diagnostic log is disabled, you will get below info from cluster log.

INFO [RES] SQL Server Availability Group: [hadrag] Extended Event logging is disabled

If the path changes, you will get below info from cluster log.

INFO [RES] SQL Server Availability Group: [hadrag] The property LogPath was changed from ‘???’ to ‘????’

Once the log file is created, you will get below info from cluster log.

INFO [RES] SQL Server Availability Group: [hadrag] Extended Event logging is started

If the log fails to created, you will get below info from cluster log.

ERR   [RES] SQL Server Availability Group: [hadrag] Failed to initialize Extended Event logging engine with error [ErrorCode]

Here is the real case :

Customer had a SQL 2012 Alwayson Availability group consisted of with three replicas. One of the replicas failed to create diagnostic log when it became primary replica. Other two replicas were able to create diagnostic logs.

I launched process monitor against the RHS.exe in the replica in question. I found that the xe.dll was not loaded, and the hadrres.dll loaded was SQL Server 2014 version instead of SQL Server 2012.

Discussed with customer, I found that there was SQL Server 2014 was installed, then it’s removed. That’s the cause!

I’m able to reproduce the issue in my box.

SQL Box1:
           Instance 1:SQL server 2012 (Primary replica)
           Instance 2: SQL server 2014 (this is a standalone instance, AG is not used)
  SQL Box2:
           Instance 1:SQL server 2012 (Secondary replica)

Remove the instance 2, then instance 1 on box1 can’t create diagnostic log.

The issue happens because uninstalling SQL Server instance removes the xe.dll in C:\Program Files\Microsoft SQL Server\120\Shared, but still leaves the SQL 2014 hadrres.dll in c:\windows\system32.

This behavior causes hadrres.dll fail to load xe.dll because xe.dll is removed during uninstallation.

Workaround

===

Copy the same version SQL 2014 xe.dll back to C:\Program Files\Microsoft SQL Server\version\Shared.