Пример #1
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");
                        }
        }
    }
Пример #2
0
        public async Task RouteAsync([NotNull] RouteContext context)
        {
            var services = context.HttpContext.RequestServices;

            // Verify if AddMvc was done before calling UseMvc
            // We use the MvcMarkerService to make sure if all the services were added.
            MvcServicesHelper.ThrowIfMvcNotRegistered(services);
            EnsureServices(context.HttpContext);

            var actionDescriptor = await _actionSelector.SelectAsync(context);

            if (actionDescriptor == null)
            {
                _logger.LogVerbose("No actions matched the current request.");
                return;
            }

            // Replacing the route data allows any code running here to dirty the route values or data-tokens
            // without affecting something upstream.
            var oldRouteData = context.RouteData;
            var newRouteData = new RouteData(oldRouteData);

            if (actionDescriptor.RouteValueDefaults != null)
            {
                foreach (var kvp in actionDescriptor.RouteValueDefaults)
                {
                    if (!newRouteData.Values.ContainsKey(kvp.Key))
                    {
                        newRouteData.Values.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            try
            {
                context.RouteData = newRouteData;

                if (_telemetry.IsEnabled("Microsoft.AspNet.Mvc.BeforeAction"))
                {
                    _telemetry.WriteTelemetry(
                        "Microsoft.AspNet.Mvc.BeforeAction",
                        new { actionDescriptor, httpContext = context.HttpContext, routeData = context.RouteData });
                }

                using (_logger.BeginScope("ActionId: {ActionId}", actionDescriptor.Id))
                {
                    _logger.LogVerbose("Executing action {ActionDisplayName}", actionDescriptor.DisplayName);

                    await InvokeActionAsync(context, actionDescriptor);

                    context.IsHandled = true;
                }
            }
            finally
            {
                if (_telemetry.IsEnabled("Microsoft.AspNet.Mvc.AfterAction"))
                {
                    _telemetry.WriteTelemetry(
                        "Microsoft.AspNet.Mvc.AfterAction",
                        new { actionDescriptor, httpContext = context.HttpContext });
                }

                if (!context.IsHandled)
                {
                    context.RouteData = oldRouteData;
                }
            }
        }