Engine ItemsChanged Event

On Windows the Engine provides ItemsChanged event that is fired on every item creation, update, delete and move operation, for both incoming and outgoing updates. Inside this event you can obtain information about each affected item, direction of the synchronization and result status of the operation. You can use this event for multiple purposes, such as:

The ItemsChanged event provides a list of items in the ItemsChangeEventArgs.Items property. The direction is provide in the ItemsChangeEventArgs.Direction property, which can be incoming or outgoing sync. The detailed result of the operation is provided in the ItemsChangeEventArgs.Result property.

The OperationResult.Status describes if the item was successfully updated/created/deleted/moved or the reason for the failure, such as exception, item being not found (for example because of the on-demand population), being in-use or filtered inside the Engine.FilterAsync() method.

Below is a logging code that you can put ingo ItemsChanged event to collect information about affected items and status of the operation:

private void Engine_ItemsChanged(Engine sender, ItemsChangeEventArgs e)
{
    foreach (ChangeEventItem item in e.Items)
    {
        ILogger logger = Logger.CreateLogger(e.ComponentName);
        string msg = $"{e.Direction} {e.OperationType}: {e.Result.Status}";
        switch (e.Result.Status)
        {
            case OperationStatus.Success:
            case OperationStatus.Conflict:
                logger.LogMessage(msg, item.Path, item.NewPath, e.OperationContext);
                break;
            case OperationStatus.Exception:
                logger.LogError(msg, item.Path, item.NewPath, e.Result.Exception);
                break;
            case OperationStatus.Filtered:
                msg = $"{msg} by {e.Result.FilteredBy.GetType().Name}";
                logger.LogDebug(msg, item.Path, item.NewPath, e.OperationContext);
                break;
            default:
                msg = $"{msg}. {e.Result.Message}";
                logger.LogDebug(msg, item.Path, item.NewPath, e.OperationContext);
                break;
        }
    }
}

 

Next Article:

Managing Custom Properties