View history of preferred replica in Availability Group - when
... the preferred replica in Availability Group changed?


Availability Groups don't have preferred replica. They have primary and secondary replicas, but not preferred. Windows Server Failover Clustering has preferred owners. Are you looking for who was the primary replica or who was a preferred owner?

If it's the WSFC preferred owner you are looking for, then you'll have to scrape the cluster log.

If it's the primary and secondary replicas, then nothing exists out of the box - you'll need to write your own. This data can be gathered from a few different sources:


AlwaysOn Health extended event session
SP_Server_Diagnostics output
Polling the DMVs and recording changes
Scraping the cluster log


You don't need all of those, it's a mix and match sort of thing. Basically you'll want to find out who was primary at the moment the logs started and then look for replica state changes. You'll need all of the logs from all of the replicas to get the most complete picture - but having a single log from a single replica can give you all the data needed as well, just takes a bit more data manipulation to get there.