跟踪微软同步框架的变化

本文关键字:变化 框架 同步 微软 跟踪 | 更新日期: 2023-09-27 18:06:07

下面是我的代码,执行两个2008 SQL服务器之间的同步。该程序成功地在两个服务器之间同步数据,没有问题。然而,问题是,在我在此期间生成的日志中,我注意到从Server2到Server1的事务量非常高——总是在这个方向上,并且总是非常相似的数量。为了帮助跟踪发生这种情况的原因,我想创建另一个日志文件,记录每次脚本同步两台服务器时从一台服务器复制到另一台服务器的实际行数据。是否有一种方法来做到这一点使用同步框架,或者它会更好地利用SQL Server内的另一个实用程序来做到这一点?我对数据库不是很精通,所以最基本、最直接的解决方案对我来说是最理想的。

//Connection string to the client (what the data flows INTO)
SqlConnection clientConnection = new SqlConnection("Data Source=OMITTED;Initial Catalog=OMITTED;Integrated Security=SSPI");
//Connection string to the database (what the data flows FROM)
SqlConnection serverConnection = new SqlConnection("Data Source=OMITTED;Initial Catalog=OMITTED;Integrated Security=SSPI");
//Create a sync orchestrator
SyncOrchestrator syncOrchestrator = new SyncOrchestrator();
//Set local provider of orchestrator to a sync provider (S2)
syncOrchestrator.LocalProvider = new SqlSyncProvider("OMITTED", clientConnection);
//Set remote provider of orchestrator to a server sync provider (S1)
syncOrchestrator.RemoteProvider = new SqlSyncProvider("OMITTED", serverConnection);
//Set the direction of sync session to UPload and Download
syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;
//Subscribe for errors that occur when applying changes to the client
((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);
//Execute the synchronization process
SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();
//Access specific information about the given file
FileInfo myFile = new FileInfo(LogFilePath);
//If the log file does not yet have any data (it's blank), include some header information
//Otherwise, just append the file with new data
if (!(myFile.Length > 0))
{
    string header = "Run Time,Changes Uploaded,Changes Downloaded";
    string data = syncStats.SyncStartTime + "," + syncStats.UploadChangesTotal + "," + syncStats.DownloadChangesTotal;
    LogFileText.Add(header);
    LogFileText.Add(data);
}
else
{
    string data = syncStats.SyncStartTime + "," + syncStats.UploadChangesTotal + "," + syncStats.DownloadChangesTotal;
    LogFileText.Add(data);
}

跟踪微软同步框架的变化

如果您为ChangesSelected或ChangesApplied创建处理程序,那里将有一个包含被选中或应用的实际数据的数据集。