Ejemplo n.º 1
0
        public override async Task Invoke(IOwinContext context)
        {
            // following request properties have to be accessed before other middlewares run
            // otherwise access could result in ObjectDisposedException
            var method = context.Request.Method;
            var path   = context.Request.Path.ToString();
            var uri    = context.Request.Uri;

            var requestId       = _configuration.RequestIdFactory(context);
            var requestParentId = OperationContext.Get()?.ParentOperationId;

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

            using (new OperationContextScope(
                       operationId: OperationContext.Get()?.OperationId ?? requestId,
                       parentOperationId: requestId))
                using (new OperationContextStoredInOwinContextScope(context))
                {
                    stopWatch.Start();

                    try
                    {
                        await Next.Invoke(context);

                        stopWatch.Stop();

                        string statusCode = context.Response.StatusCode.ToString();
                        var    success    = context.Response.StatusCode < 400;

                        if (await _configuration.ShouldTrackRequest(context))
                        {
                            await TrackRequest(requestId, requestParentId, method, path, uri, context,
                                               statusCode, success, requestStartDate, stopWatch.Elapsed);
                        }
                    }
                    catch (Exception e)
                    {
                        stopWatch.Stop();

                        if (await _configuration.ShouldTrackException(context, e))
                        {
                            TraceException(requestId, e);
                        }

                        if (await _configuration.ShouldTrackRequest(context))
                        {
                            await TrackRequest(requestId, requestParentId, method, path, uri, context,
                                               "500", false, requestStartDate, stopWatch.Elapsed);
                        }

                        throw;
                    }
                }
        }
Ejemplo n.º 2
0
        public override async Task Invoke(IOwinContext context)
        {
            // following request properties have to be accessed before other middlewares run
            // otherwise access could result in ObjectDisposedException
            var method = context.Request.Method;
            var path   = context.Request.Path.ToString();
            var uri    = context.Request.Uri;

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

            stopWatch.Start();

            try
            {
                await Next.Invoke(context);

                stopWatch.Stop();

                if (await _configuration.ShouldTrackRequest(context))
                {
                    await TrackRequest(method, path, uri, context, context.Response.StatusCode, requestStartDate, stopWatch.Elapsed);
                }
            }
            catch (Exception e)
            {
                stopWatch.Stop();

                TraceException(e);

                if (await _configuration.ShouldTrackRequest(context))
                {
                    await TrackRequest(method, path, uri, context, (int)HttpStatusCode.InternalServerError, requestStartDate, stopWatch.Elapsed);
                }

                throw;
            }
        }