跟踪 Microsoft Sync Framework 中的更改

Tracking changes in Microsoft Sync Framework

下面是我的代码,它在两个 2008 SQL 服务器之间执行同步。该程序成功地在两台服务器之间同步数据而没有问题。然而,问题是,在我在此期间生成的日志中,我注意到从 Server2 到 Server1 的交易量非常大 - 总是朝这个方向,而且数量总是非常相似。为了帮助追踪发生这种情况的原因,我想创建另一个日志文件,记录每次脚本同步两台服务器时从一台服务器复制到另一台服务器的实际行数据。有没有办法使用 Sync Framework 来执行此操作,或者使用 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 创建处理程序,那里将有一个数据集,其中包含已选择或应用的实际数据。