Example #1
0
        public async Task Invoke(HttpContext httpContext)
        {
            // Initialize Transaction Id
            string            transactionId = null;
            EventRequestModel request;

            (request, transactionId) = await FormatRequest(httpContext.Request, transactionId);

            // Add Transaction Id to the Response Header
            if (!string.IsNullOrEmpty(transactionId))
            {
                httpContext.Response.Headers.Add("X-Moesif-Transaction-Id", transactionId);
            }

            // Buffering Owin response
            var          owinResponse      = httpContext.Response;
            StreamHelper outputCaptureOwin = new StreamHelper(owinResponse.Body);

            owinResponse.Body = outputCaptureOwin;

            await _next(httpContext);

            // Get Skip
            var skip_out = new object();
            var getSkip  = moesifOptions.TryGetValue("Skip", out skip_out);

            // Check to see if we need to send event to Moesif
            Func <HttpRequest, HttpResponse, bool> ShouldSkip = null;

            if (getSkip)
            {
                ShouldSkip = (Func <HttpRequest, HttpResponse, bool>)(skip_out);
            }

            if (ShouldSkip != null && ShouldSkip(httpContext.Request, httpContext.Response))
            {
                LoggerHelper.LogDebugMessage(debug, "Skipping the event");
            }
            else
            {
                var response = FormatResponse(httpContext.Response, outputCaptureOwin, transactionId);

                // UserId
                string userId = httpContext?.User?.Identity?.Name;
                userId = LoggerHelper.GetConfigValues("IdentifyUser", moesifOptions, httpContext.Request, httpContext.Response, debug, userId);
                if (string.IsNullOrEmpty(userId))
                {
                    // Fetch userId from authorization header
                    userId = userHelper.fetchUserFromAuthorizationHeader(request.Headers, authorizationHeaderName, authorizationUserIdField);
                }

                // CompanyId
                string companyId = LoggerHelper.GetConfigValues("IdentifyCompany", moesifOptions, httpContext.Request, httpContext.Response, debug);
                // SessionToken
                string sessionToken = LoggerHelper.GetConfigValues("GetSessionToken", moesifOptions, httpContext.Request, httpContext.Response, debug);
                // Metadata
                Dictionary <string, object> metadata = LoggerHelper.GetConfigObjectValues("GetMetadata", moesifOptions, httpContext.Request, httpContext.Response, debug);

                LoggerHelper.LogDebugMessage(debug, "Calling the API to send the event to Moesif");
                //Send event to Moesif async
                await Task.Run(async() => await LogEventAsync(request, response, userId, companyId, sessionToken, metadata));
            }
        }
        public async override Task Invoke(IOwinContext httpContext)
        {
            // Buffering mvc reponse
            StreamHelper outputCaptureMVC = null;
            HttpResponse httpResponse     = HttpContext.Current?.Response;

            if (httpResponse != null)
            {
                outputCaptureMVC    = new StreamHelper(httpResponse.Filter);
                httpResponse.Filter = outputCaptureMVC;
            }

            // Buffering Owin response
            IOwinResponse owinResponse      = httpContext.Response;
            StreamHelper  outputCaptureOwin = new StreamHelper(owinResponse.Body);

            owinResponse.Body = outputCaptureOwin;

            // Initialize Transaction Id
            string            transactionId = null;
            EventRequestModel request;

            // Prepare Moeif Event Request Model
            (request, transactionId) = await ToRequest(httpContext.Request, transactionId);

            // Add Transaction Id to the Response Header
            if (!string.IsNullOrEmpty(transactionId))
            {
                httpContext.Response.Headers.Append("X-Moesif-Transaction-Id", transactionId);
            }

            await Next.Invoke(httpContext);

            // Get Skip
            var skip_out = new object();
            var getSkip  = moesifOptions.TryGetValue("Skip", out skip_out);

            // Check to see if we need to send event to Moesif
            Func <IOwinRequest, IOwinResponse, bool> ShouldSkip = null;

            if (getSkip)
            {
                ShouldSkip = (Func <IOwinRequest, IOwinResponse, bool>)(skip_out);
            }

            if (ShouldSkip != null && ShouldSkip(httpContext.Request, httpContext.Response))
            {
                LoggerHelper.LogDebugMessage(debug, "Skip sending event to Moesif");
            }
            else
            {
                // Select stream to use
                StreamHelper streamToUse = (outputCaptureMVC == null || outputCaptureMVC.CopyStream.Length == 0) ? outputCaptureOwin : outputCaptureMVC;

                // Prepare Moesif Event Response model
                var response = ToResponse(httpContext.Response, streamToUse, transactionId);

                // UserId
                string userId = httpContext?.Authentication?.User?.Identity?.Name;
                userId = LoggerHelper.GetConfigValues("IdentifyUser", moesifOptions, httpContext.Request, httpContext.Response, debug, userId);
                if (string.IsNullOrEmpty(userId))
                {
                    // Fetch userId from authorization header
                    userId = userHelper.fetchUserFromAuthorizationHeader(request.Headers, authorizationHeaderName, authorizationUserIdField);
                }
                // CompanyId
                string companyId = LoggerHelper.GetConfigValues("IdentifyCompany", moesifOptions, httpContext.Request, httpContext.Response, debug);
                // SessionToken
                string sessionToken = LoggerHelper.GetConfigValues("GetSessionToken", moesifOptions, httpContext.Request, httpContext.Response, debug);
                // Metadata
                Dictionary <string, object> metadata = LoggerHelper.GetConfigObjectValues("GetMetadata", moesifOptions, httpContext.Request, httpContext.Response, debug);

                LoggerHelper.LogDebugMessage(debug, "Calling the API to send the event to Moesif");
                await Task.Run(async() => await LogEventAsync(request, response, userId, companyId, sessionToken, metadata));
            }
        }