0
votes

I am attempting to write a simple sync tool service using the microsoft sync framework. I am able to synchronize two locations but I would like to be able to output the files that are being synced using log4net. Does anyone know how I can do this? here is the code I using:

            Try

            Using _sourceProvider As FileSyncProvider = New FileSyncProvider(_sourceId.GetGuidId, _sourceDir)

                Using _destinationProvider As FileSyncProvider = New FileSyncProvider(_destId.GetGuidId, _destDir)

                    Dim _syncAgent As SyncOrchestrator = New SyncOrchestrator()

                    _syncAgent.LocalProvider = _sourceProvider
                    _syncAgent.RemoteProvider = _destinationProvider
                    _syncAgent.Direction = SyncDirectionOrder.Upload
                    _syncAgent.Synchronize()

                End Using

            End Using

        Catch ex As Exception
            Logger.WriteLog(ElogLevel.INFO, "" & ex.Message & "")
        End Try

Thanks in advance.


Thanks for the help, I've implemented the ApplyChange event Handler, but I get the following error when I use the EventHandler code:

_sourceProvider.ApplyingChange += New EventHandler(Of ApplyingChangeEventArgs)(fileSyncProvider_ApplyingChange)
_destinationProvider.ApplyingChange += New EventHandler(Of ApplyingChangeEventArgs)(fileSyncProvider_ApplyingChange)

'Public EventApplyingChange(sender As Object, e As Microsoft.Syncronization.Files.ApplyingChangeEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

1

1 Answers

0
votes

You can utilize the ApplyingChange event of the FileSyncProvider.

Setup:

Public Event ApplyingChange As EventHandler(Of ApplyingChangeEventArgs)
Dim handler As EventHandler(Of ApplyingChangeEventArgs)

AddHandler _sourceProvider.ApplyingChange, handler
AddHandler _destinationProvider.ApplyingChange, handler

Implement the ApplyingChange event handler:

Private Sub fileSyncProvider_ApplyingChange(sender As Object, e As ApplyingChangeEventArgs)
    Logger.WriteLog(ElogLevel.INFO, "FileSyncProvider > Applying Change")
    Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & "Change type: {0}", e.ChangeType))

    If e.CurrentFileData IsNot Nothing Then
        Logger.WriteLog(ElogLevel.INFO, "Current Data:")
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "{0} Name: {1}", If(e.CurrentFileData.IsDirectory, "Folder", "File"), e.CurrentFileData.Name))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Relative Path: {0}", e.CurrentFileData.RelativePath.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Creation Time: {0}", e.CurrentFileData.CreationTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Last Write Time: {0}", e.CurrentFileData.LastWriteTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Last Access Time: {0}", e.CurrentFileData.LastAccessTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Size: {0}", e.CurrentFileData.Size.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Attributes: {0}", e.CurrentFileData.Attributes.ToString()))
    End If

    If e.NewFileData IsNot Nothing Then   
        Logger.WriteLog(ElogLevel.INFO, "New Data:")
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "{0} Name: {1}", If(e.NewFileData.IsDirectory, "Folder", "File"), e.NewFileData.Name))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Relative Path: {0}", e.NewFileData.RelativePath.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Creation Time: {0}", e.NewFileData.CreationTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Last Write Time: {0}", e.NewFileData.LastWriteTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Last Access Time: {0}", e.NewFileData.LastAccessTime.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Size: {0}", e.NewFileData.Size.ToString()))
        Logger.WriteLog(ElogLevel.INFO, String.Format(vbLf & vbTab & vbTab & "Attributes: {0}", e.NewFileData.Attributes.ToString()))
    End If
End Sub