예제 #1
0
        private async Task <(EventRequestModel, String)> FormatRequest(HttpRequest request, string transactionId)
        {
            // Request headers
            var reqHeaders = LoggerHelper.ToHeaders(request.Headers, debug);

            // RequestBody
            request.EnableBuffering(bufferThreshold: 1000000);
            string bodyAsText = null;

            string contentEncoding     = "";
            string contentLength       = "";
            int    parsedContentLength = 100000;

            reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
            reqHeaders.TryGetValue("Content-Length", out contentLength);
            int.TryParse(contentLength, out parsedContentLength);

            bodyAsText = await LoggerHelper.GetRequestContents(bodyAsText, request, contentEncoding, parsedContentLength, debug);

            // Add Transaction Id to the Request Header
            bool disableTransactionId = LoggerHelper.GetConfigBoolValues(moesifOptions, "DisableTransactionId", false);

            if (!disableTransactionId)
            {
                transactionId = LoggerHelper.GetOrCreateTransactionId(reqHeaders, "X-Moesif-Transaction-Id");
                reqHeaders    = LoggerHelper.AddTransactionId("X-Moesif-Transaction-Id", transactionId, reqHeaders);
            }

            // Serialize request body
            var bodyWrapper = LoggerHelper.Serialize(bodyAsText, request.ContentType, logBody, debug);

            // Client Ip Address
            string ip  = clientIpHelper.GetClientIp(reqHeaders, request);
            var    uri = new Uri(request.GetDisplayUrl()).ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time             = DateTime.UtcNow,
                Uri              = uri,
                Verb             = request.Method,
                ApiVersion       = apiVersion,
                IpAddress        = ip,
                Headers          = reqHeaders,
                Body             = bodyWrapper.Item1,
                TransferEncoding = bodyWrapper.Item2
            };

            return(eventReq, transactionId);
        }
        private async Task <(EventRequestModel, String)> ToRequest(IOwinRequest request, string transactionId)
        {
            // Request headers
            var reqHeaders = LoggerHelper.ToHeaders(request.Headers, debug);

            // RequestBody
            string contentEncoding     = "";
            string contentLength       = "";
            int    parsedContentLength = 100000;

            string body = null;

            reqHeaders.TryGetValue("Content-Encoding", out contentEncoding);
            reqHeaders.TryGetValue("Content-Length", out contentLength);
            int.TryParse(contentLength, out parsedContentLength);
            try
            {
                body = await LoggerHelper.GetRequestContents(request, contentEncoding, parsedContentLength, disableStreamOverride);
            }
            catch
            {
                LoggerHelper.LogDebugMessage(debug, "Cannot read request body.");
            }

            var bodyWrapper = LoggerHelper.Serialize(body, request.ContentType);

            // Add Transaction Id to the Request Header
            bool disableTransactionId = LoggerHelper.GetConfigBoolValues(moesifOptions, "DisableTransactionId", false);

            if (!disableTransactionId)
            {
                transactionId = LoggerHelper.GetOrCreateTransactionId(reqHeaders, "X-Moesif-Transaction-Id");
                reqHeaders    = LoggerHelper.AddTransactionId("X-Moesif-Transaction-Id", transactionId, reqHeaders);
            }

            string ip  = clientIpHelper.GetClientIp(reqHeaders, request);
            var    uri = request.Uri.ToString();

            string apiVersion     = null;
            var    apiVersion_out = new object();
            var    getApiVersion  = moesifOptions.TryGetValue("ApiVersion", out apiVersion_out);

            if (getApiVersion)
            {
                apiVersion = apiVersion_out.ToString();
            }

            var eventReq = new EventRequestModel()
            {
                Time             = DateTime.UtcNow,
                Uri              = uri,
                Verb             = request.Method,
                ApiVersion       = apiVersion,
                IpAddress        = ip,
                Headers          = reqHeaders,
                Body             = bodyWrapper.Item1,
                TransferEncoding = bodyWrapper.Item2
            };

            return(eventReq, transactionId);
        }