示例#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);
        }
示例#2
0
        public MoesifMiddlewareNetCore(Dictionary <string, object> _middleware)
        {
            moesifOptions = _middleware;

            try
            {
                // Initialize client
                client = new MoesifApiClient(moesifOptions["ApplicationId"].ToString());
                debug  = LoggerHelper.GetConfigBoolValues(moesifOptions, "LocalDebug", false);
            }
            catch (Exception e)
            {
                throw new Exception("Please provide the application Id to send events to Moesif");
            }
        }
        public MoesifMiddlewareNetFramework(OwinMiddleware next, Dictionary <string, object> _middleware) : base(next)
        {
            moesifOptions = _middleware;

            try
            {
                // Initialize client
                debug                    = LoggerHelper.GetConfigBoolValues(moesifOptions, "LocalDebug", false);
                client                   = new MoesifApiClient(moesifOptions["ApplicationId"].ToString(), "moesif-netframework/1.3.8", debug);
                logBody                  = LoggerHelper.GetConfigBoolValues(moesifOptions, "LogBody", true);
                isBatchingEnabled        = LoggerHelper.GetConfigBoolValues(moesifOptions, "EnableBatching", true);         // Enable batching
                disableStreamOverride    = LoggerHelper.GetConfigBoolValues(moesifOptions, "DisableStreamOverride", false); // Reset Request Body position
                batchSize                = LoggerHelper.GetConfigIntValues(moesifOptions, "BatchSize", 25);                 // Batch Size
                queueSize                = LoggerHelper.GetConfigIntValues(moesifOptions, "QueueSize", 1000);               // Event Queue Size
                batchMaxTime             = LoggerHelper.GetConfigIntValues(moesifOptions, "batchMaxTime", 2);               // Batch max time in seconds
                appConfigSyncTime        = LoggerHelper.GetConfigIntValues(moesifOptions, "appConfigSyncTime", 300);        // App config sync time in seconds
                appConfig                = new AppConfig();                                                                 // Create a new instance of AppConfig
                userHelper               = new UserHelper();                                                                // Create a new instance of userHelper
                companyHelper            = new CompanyHelper();                                                             // Create a new instane of companyHelper
                clientIpHelper           = new ClientIp();                                                                  // Create a new instance of client Ip
                authorizationHeaderName  = LoggerHelper.GetConfigStringValues(moesifOptions, "AuthorizationHeaderName", "authorization");
                authorizationUserIdField = LoggerHelper.GetConfigStringValues(moesifOptions, "AuthorizationUserIdField", "sub");
                samplingPercentage       = 100;                   // Default sampling percentage
                configETag               = null;                  // Default configETag
                lastUpdatedTime          = DateTime.UtcNow;       // Default lastUpdatedTime

                MoesifQueue = new ConcurrentQueue <EventModel>(); // Initialize queue

                new Thread(async() =>                             // Create a new thread to read the queue and send event to moesif
                {
                    Thread.CurrentThread.IsBackground = true;
                    if (isBatchingEnabled)
                    {
                        ScheduleWorker();
                    }
                    else
                    {
                        ScheduleAppConfig(); // Scheduler to fetch application configuration every 5 minutes
                    }
                }).Start();
            }
            catch (Exception e)
            {
                throw new Exception("Please provide the application Id to send events to Moesif");
            }
        }
        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);
        }