In one of my previous post, I talked about what all test management related events are being published by the server. In this post, I want to share a sample code snippet that you can use to listen to one such event (TestRunChangedNotification).  To make sense of the code snippet and how to use it, please go through this great blog post by Esteban.

using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.TestManagement.Server;
using System;
using System.Diagnostics;

namespace Sample
{
    class MySubscriber : ISubscriber
    {
        string ISubscriber.Name
        {
            get { return "TestRunChangedSubscriber"; }
        }

        SubscriberPriority ISubscriber.Priority
        {
            get { return SubscriberPriority.Normal; }
        }

        EventNotificationStatus ISubscriber.ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType, object notificationEventArgs, out int statusCode, out string statusMessage, out Microsoft.TeamFoundation.Common.ExceptionPropertyCollection properties)
        {
            statusCode = 0;
            statusMessage = string.Empty;
            properties = null;

            if (notificationType != NotificationType.Notification)
            {
                return EventNotificationStatus.ActionPermitted;
            }

            TestRunChangedNotification runChangedNotification = notificationEventArgs as TestRunChangedNotification;
            if (runChangedNotification == null)
            {
                return EventNotificationStatus.ActionPermitted;
            }

            string message = "Test run  " + runChangedNotification.TestRunId + " changed in project " + runChangedNotification.ProjectName;
            TeamFoundationApplication.Log(message, 123, EventLogEntryType.Information);

            return EventNotificationStatus.ActionPermitted;
        }

        Type[] ISubscriber.SubscribedTypes()
        {
            return new Type[] { typeof(TestRunChangedNotification) };
        }
    }

}