Exemplo n.º 1
0
        public HttpResponseMessage Browse(int pageIndex = 0, int pageSize = 30, string eventType = null, int severity = -1)
        {
            EventLogSeverity? actualSeverity = null;

            // Is there a severity set?
            if (severity >= 0)
            {
                actualSeverity = (EventLogSeverity)severity;
            }

            // Get event logs.
            IEnumerable<EventLog> eventLogs = EventLogManager.Browse(pageIndex, pageSize, eventType, actualSeverity);

            // Work out pagination details.
            int rowCount = EventLogManager.BrowseCount(pageIndex, pageSize, eventType, actualSeverity);
            int pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);

            // Start building meta.
            Dictionary<string, dynamic> pagination = new Dictionary<string, dynamic>();

            // Add basics.
            pagination.Add("Records", rowCount);
            pagination.Add("Pages", pageCount);
            pagination.Add("CurrentPage", pageIndex);

            // Build navigation.
            Dictionary<string, string> navigation = new Dictionary<string, string>();

            // Parameters passed in not changed by pagination.
            string fixedParams = "";

            // Page size.
            if (pageSize != 30)
            {
                fixedParams += string.Format("pageSize={0}", pageSize);
            }

            // Event type.
            if (eventType != null)
            {
                fixedParams += string.Format("eventType={0}", eventType);
            }

            // Severity.
            if (severity != -1)
            {
                fixedParams += string.Format("eventType={0}", severity);
            }

            // Is there a next page?
            if (pageIndex < pageCount)
            {
                string nextLink = string.Format("Browse?pageIndex={0}", pageIndex + 1);

                if (!string.IsNullOrEmpty(fixedParams))
                {
                    nextLink = string.Format("{0}&{1}", nextLink, fixedParams);
                }

                navigation.Add("Next", nextLink);
            }
            
            // Is there a previous page?
            if (pageIndex > 0)
            {
                string prevLink = string.Format("Browse?pageIndex={0}", pageIndex - 1);

                if (!string.IsNullOrEmpty(fixedParams))
                {
                    prevLink = string.Format("{0}&{1}", prevLink, fixedParams);
                }

                navigation.Add("Previous", prevLink);
            }

            // Add navigation.
            pagination.Add("Navigation", navigation);

            Dictionary<string, dynamic> payload = new Dictionary<string, dynamic>();

            payload.Add("Data", eventLogs);
            payload.Add("Pagination", pagination);

            string json = JsonConvert.SerializeObject(payload);

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(json, Encoding.UTF8, "application/json");

            return response;
        }