示例#1
0
        public void Initialize(ITelemetry telemetry)
        {
            var owinRequestId = OwinRequestIdContext.Get();

            if (owinRequestId != null)
            {
                telemetry.Context.Operation.Id = owinRequestId;
            }
        }
        public override async Task Invoke(IOwinContext context)
        {
            var requestId = context.Get <string>(OwinRequestIdKey);

            var requestMethod = context.Request.Method;
            var requestPath   = context.Request.Path.ToString();
            var requestUri    = context.Request.Uri;

            var requestStartDate = DateTimeOffset.Now;
            var stopWatch        = new Stopwatch();

            stopWatch.Start();

            var requestFailed = false;

            try
            {
                OwinRequestIdContext.Set(requestId);

                if (Next != null)
                {
                    await Next.Invoke(context);
                }
            }
            catch (Exception ex)
            {
                requestFailed = true;

                _telemetryClient.TrackException(ex);

                throw;
            }
            finally
            {
                stopWatch.Stop();

                TrackRequest(requestId, requestMethod, requestPath, requestUri,
                             context.Response?.StatusCode ?? 0, requestFailed, requestStartDate, stopWatch.Elapsed);

                OwinRequestIdContext.Clear();
            }
        }