Example #1
0
        public void Completed()
        {
            var result       = new List <KeyValuePair <string, object> >();
            var observer     = new ObserverToList <TelemData>(result);
            var listener     = new TelemetryListener("MyListener");
            var subscription = listener.Subscribe(observer);

            listener.WriteTelemetry("IntPayload", 5);
            Assert.Equal(1, result.Count);
            Assert.Equal("IntPayload", result[0].Key);
            Assert.Equal(5, result[0].Value);
            Assert.False(observer.Completed);

            // The listener dies
            listener.Dispose();
            Assert.True(observer.Completed);

            // confirm that we can unsubscribe without crashing
            subscription.Dispose();

            // If we resubscribe after dispose, but it does not do anything.
            subscription = listener.Subscribe(observer);

            listener.WriteTelemetry("IntPayload", 5);
            Assert.Equal(1, result.Count);
        }
Example #2
0
        public void DoubleDisposeOfListener()
        {
            var listener        = new TelemetryListener("MyListener");
            int completionCount = 0;

            IDisposable subscription = listener.Subscribe(MakeObserver <KeyValuePair <string, object> >(_ => { }, () => completionCount++));

            listener.Dispose();
            listener.Dispose();

            subscription.Dispose();
            subscription.Dispose();

            Assert.Equal(1, completionCount);
        }
Example #3
0
    static void Main()
    {
        // This allows EventSources to listen to notifications.
        // TODO this seems counterintuitive/unnecessary.
        TelemetryEventSource.LogForDefaultNotificationHub.Touch();

        // To set up a sink for notifications IObserver and hook up.
        var consoleObserver = new ConsoleObserver("Default");

        using (var subscription = TelemetryListener.Default.Subscribe(consoleObserver, consoleObserver.IsEnabled))
        {
            // This simulates an application that is creating Dispatcher/Notifier pairs for instantiable in-process contexts.
            // The child hubs each cross-talk with the default hub, but do not cross-talk with each other
            using (var island1 = new TelemetryListener("Island1"))
                using (var island2 = new TelemetryListener("Island2"))
                    using (island1.Subscribe(new ConsoleObserver(island1.Name)))
                        using (island2.Subscribe(new ConsoleObserver(island2.Name)))
                        {
                            // Here we simulate what might happen in the class library where we don't use dependency injection.
                            // You can also get you iNotifier by asking IServiceProvider which might make one per tenant.
                            TelemetrySource defaultSource = TelemetrySource.Default;
                            TelemetrySource islandSource1 = island1;
                            TelemetrySource islandSource2 = island2;

                            // Normally this would be in code that was receiving the HttpRequestResponse
                            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "http://localhost/");

                            // Here we log for simple cases as show below we don't need to do the ShuldNotify, but we are
                            // showing the general case where there might be significant work setting up the payload.
                            if (defaultSource.IsEnabled("OutgoingHttpRequestReturns"))
                            {
                                // Here we are assuming we would like to log both to EventSource and direct subscribers to
                                // NotificationHub.   Because of this the payload class contains both serializable fields (like
                                // ReqeustUri which we resolve to a string), as well as rich objects (like Message) that are
                                // stripped from EventSource serialization.
                                defaultSource.WriteTelemetry("OutgoingHttpRequestReturns", new { RequestUri = message.RequestUri.ToString(), Message = message });
                            }

                            islandSource1.WriteTelemetry("TalkingOnIsland", "Island One");
                            islandSource2.WriteTelemetry("TalkingOnIsland", "Island Two");
                        }
        }
    }