GetRequestUrl() static private method

static private GetRequestUrl ( System.Web.HttpContextBase context ) : Uri
context System.Web.HttpContextBase
return System.Uri
コード例 #1
0
ファイル: ErrorRssHandler.cs プロジェクト: yornstei/Elmah
        public static void ProcessRequest(HttpContextBase context)
        {
            const int pageSize = 15;
            var       entries  = new List <ErrorLogEntry>(pageSize);
            var       log      = ErrorLog.GetDefault(context);

            log.GetErrors(0, pageSize, entries);

            var response = context.Response;

            response.ContentType = "application/xml";

            var title = string.Format(@"Error log of {0} on {1}",
                                      log.ApplicationName, Environment.MachineName);

            var link    = ErrorLogPageFactory.GetRequestUrl(context).GetLeftPart(UriPartial.Authority) + context.Request.ServerVariables["URL"];
            var baseUrl = new Uri(link.TrimEnd('/') + "/");

            var items =
                from entry in entries
                let error = entry.Error
                            select RssXml.Item(
                    error.Message,
                    "An error of type " + error.Type + " occurred. " + error.Message,
                    error.Time,
                    baseUrl + "detail?id=" + Uri.EscapeDataString(entry.Id));

            var rss = RssXml.Rss(title, link, "Log of recent errors", items);

            response.Write(XmlText.StripIllegalXmlCharacters(rss.ToString()));
        }
コード例 #2
0
            public override IEnumerable <string> Entries(IList <ErrorLogEntry> entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                if (count == 0)
                {
                    yield break;
                }

                //
                // Setup to emit CSV records.
                //

                var writer = new StringWriter {
                    NewLine = "\r\n"
                };
                var csv = new CsvWriter(writer);

                var culture = CultureInfo.InvariantCulture;
                var epoch   = new DateTime(1970, 1, 1);

                //
                // For each error, emit a CSV record.
                //

                for (var i = index; i < count; i++)
                {
                    var entry      = entries[i];
                    var error      = entry.Error;
                    var time       = error.Time.ToUniversalTime();
                    var query      = "?id=" + Uri.EscapeDataString(entry.Id);
                    var requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                    csv.Field(error.ApplicationName)
                    .Field(error.HostName)
                    .Field(time.ToString("yyyy-MM-dd HH:mm:ss", culture))
                    .Field(time.Subtract(epoch).TotalSeconds.ToString("0.0000", culture))
                    .Field(error.Type)
                    .Field(error.Source)
                    .Field(error.User)
                    .Field(error.StatusCode.ToString(culture))
                    .Field(error.Message)
                    .Field(new Uri(requestUrl, "detail" + query).ToString())
                    .Field(new Uri(requestUrl, "xml" + query).ToString())
                    .Field(new Uri(requestUrl, "json" + query).ToString())
                    .Record();
                }

                yield return(writer.ToString());
            }
コード例 #3
0
            public override void Entries(IList entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                if (count == 0)
                {
                    return;
                }

                //
                // Setup to emit CSV records.
                //

                StringWriter writer = new StringWriter();

                writer.NewLine = "\r\n";
                CsvWriter csv = new CsvWriter(writer);

                CultureInfo culture = CultureInfo.InvariantCulture;
                DateTime    epoch   = new DateTime(1970, 1, 1);

                //
                // For each error, emit a CSV record.
                //

                for (int i = index; i < count; i++)
                {
                    ErrorLogEntry entry      = (ErrorLogEntry)entries[i];
                    Error         error      = entry.Error;
                    DateTime      time       = error.Time.ToUniversalTime();
                    string        query      = "?id=" + HttpUtility.UrlEncode(entry.Id);
                    Uri           requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                    csv.Field(error.ApplicationName)
                    .Field(error.HostName)
                    .Field(time.ToString("yyyy-MM-dd HH:mm:ss", culture))
                    .Field(time.Subtract(epoch).TotalSeconds.ToString("0.0000", culture))
                    .Field(error.Type)
                    .Field(error.Source)
                    .Field(error.User)
                    .Field(error.StatusCode.ToString(culture))
                    .Field(error.Message)
                    .Field(new Uri(requestUrl, "detail" + query).ToString())
                    .Field(new Uri(requestUrl, "xml" + query).ToString())
                    .Field(new Uri(requestUrl, "json" + query).ToString())
                    .Record();
                }

                Context.Response.Output.Write(writer.ToString());
            }
コード例 #4
0
        public static void ProcessRequest(HttpContextBase context)
        {
            var log = ErrorLog.GetDefault(context);

            var request  = context.Request;
            var response = context.Response;

            response.ContentType = "application/xml";

            var title = string.Format(@"Daily digest of errors in {0} on {1}",
                                      log.ApplicationName, Environment.MachineName);

            var link    = ErrorLogPageFactory.GetRequestUrl(context).GetLeftPart(UriPartial.Authority) + request.ServerVariables["URL"];
            var baseUrl = new Uri(link.TrimEnd('/') + "/");

            var items = GetItems(log, baseUrl, 30, 30).Take(30);
            var rss   = RssXml.Rss(title, link, "Daily digest of application errors", items);

            context.Response.Write(XmlText.StripIllegalXmlCharacters(rss.ToString()));
        }
コード例 #5
0
        private void Render()
        {
            Response.ContentType = "application/xml";

            ErrorLog log = ErrorLog.GetDefault(_context);

            //
            // We'll be emitting RSS vesion 0.91.
            //

            RichSiteSummary rss = new RichSiteSummary();

            rss.version = "0.91";

            //
            // Set up the RSS channel.
            //

            Channel channel  = new Channel();
            string  hostName = Environment.TryGetMachineName(_context);

            channel.title = "Daily digest of errors in "
                            + log.ApplicationName
                            + (hostName.Length > 0 ? " on " + hostName : null);
            channel.description = "Daily digest of application errors";
            channel.language    = "en";

            Uri baseUrl = new Uri(ErrorLogPageFactory.GetRequestUrl(_context).GetLeftPart(UriPartial.Authority) + Request.ServerVariables["URL"]);

            channel.link = baseUrl.ToString();

            rss.channel = channel;

            //
            // Build the channel items.
            //

            const int pageSize       = 30;
            const int maxPageLimit   = 30;
            ArrayList itemList       = new ArrayList(pageSize);
            ArrayList errorEntryList = new ArrayList(pageSize);

            //
            // Start with the first page of errors.
            //

            int pageIndex = 0;

            //
            // Initialize the running state.
            //

            DateTime       runningDay        = DateTime.MaxValue;
            int            runningErrorCount = 0;
            Item           item   = null;
            StringBuilder  sb     = new StringBuilder();
            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb));

            do
            {
                //
                // Get a logical page of recent errors and loop through them.
                //

                errorEntryList.Clear();
                log.GetErrors(pageIndex++, pageSize, errorEntryList);

                foreach (ErrorLogEntry entry in errorEntryList)
                {
                    Error    error = entry.Error;
                    DateTime time  = error.Time.ToUniversalTime();
                    DateTime day   = new DateTime(time.Year, time.Month, time.Day);

                    //
                    // If we're dealing with a new day then break out to a
                    // new channel item, finishing off the previous one.
                    //

                    if (day < runningDay)
                    {
                        if (runningErrorCount > 0)
                        {
                            RenderEnd(writer);
                            item.description = sb.ToString();
                            itemList.Add(item);
                        }

                        runningDay        = day;
                        runningErrorCount = 0;

                        if (itemList.Count == pageSize)
                        {
                            break;
                        }

                        item         = new Item();
                        item.pubDate = time.ToString("r");
                        item.title   = string.Format("Digest for {0} ({1})",
                                                     runningDay.ToString("yyyy-MM-dd"), runningDay.ToLongDateString());

                        sb.Length = 0;
                        RenderStart(writer);
                    }

                    RenderError(writer, entry, baseUrl);
                    runningErrorCount++;
                }
            }while (pageIndex < maxPageLimit && itemList.Count < pageSize && errorEntryList.Count > 0);

            if (runningErrorCount > 0)
            {
                RenderEnd(writer);
                item.description = sb.ToString();
                itemList.Add(item);
            }

            channel.item = (Item[])itemList.ToArray(typeof(Item));

            //
            // Stream out the RSS XML.
            //

            Response.Write(XmlText.StripIllegalXmlCharacters(XmlSerializer.Serialize(rss)));
        }
コード例 #6
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/xml";

            //
            // Get the last set of errors for this application.
            //

            const int pageSize       = 15;
            ArrayList errorEntryList = new ArrayList(pageSize);
            ErrorLog  log            = ErrorLog.GetDefault(context);

            log.GetErrors(0, pageSize, errorEntryList);

            //
            // We'll be emitting RSS vesion 0.91.
            //

            RichSiteSummary rss = new RichSiteSummary();

            rss.version = "0.91";

            //
            // Set up the RSS channel.
            //

            Channel channel  = new Channel();
            string  hostName = Environment.TryGetMachineName(context);

            channel.title = "Error log of " + log.ApplicationName
                            + (hostName.Length > 0 ? " on " + hostName : null);
            channel.description = "Log of recent errors";
            channel.language    = "en";
            channel.link        = ErrorLogPageFactory.GetRequestUrl(context).GetLeftPart(UriPartial.Authority) +
                                  context.Request.ServerVariables["URL"];

            rss.channel = channel;

            //
            // For each error, build a simple channel item. Only the title,
            // description, link and pubDate fields are populated.
            //

            channel.item = new Item[errorEntryList.Count];

            for (int index = 0; index < errorEntryList.Count; index++)
            {
                ErrorLogEntry errorEntry = (ErrorLogEntry)errorEntryList[index];
                Error         error      = errorEntry.Error;

                Item item = new Item();

                item.title       = error.Message;
                item.description = "An error of type " + error.Type + " occurred. " + error.Message;
                item.link        = channel.link + "/detail?id=" + HttpUtility.UrlEncode(errorEntry.Id);
                item.pubDate     = error.Time.ToUniversalTime().ToString("r");

                channel.item[index] = item;
            }

            //
            // Stream out the RSS XML.
            //

            context.Response.Write(XmlText.StripIllegalXmlCharacters(XmlSerializer.Serialize(rss)));
        }
コード例 #7
0
            public override IEnumerable <string> Entries(IList <ErrorLogEntry> entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                var writer = new StringWriter {
                    NewLine = "\n"
                };

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                var json = new JsonTextWriter(writer);

                json.Object()
                .Member("total").Number(total)
                .Member("errors").Array();

                var requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                for (var i = index; i < count; i++)
                {
                    var entry = entries[i];
                    writer.WriteLine();
                    if (i == 0)
                    {
                        writer.Write(' ');
                    }
                    writer.Write("  ");

                    var urlTemplate = new Uri(requestUrl, "{0}?id=" + Uri.EscapeDataString(entry.Id)).ToString();

                    json.Object();
                    ErrorJson.EncodeMembers(entry.Error, json);
                    json.Member("hrefs")
                    .Array()
                    .Object()
                    .Member("type").String("text/html")
                    .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                    .Object()
                    .Member("type").String("aplication/json")
                    .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                    .Object()
                    .Member("type").String("application/xml")
                    .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                    .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                {
                    writer.WriteLine();
                }

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                    {
                        writer.WriteLine(@"</body></html>");
                    }
                }

                yield return(writer.ToString());
            }
コード例 #8
0
            public override void Entries(IList entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                StringWriter writer = new StringWriter();

                writer.NewLine = "\n";

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                JsonTextWriter json = new JsonTextWriter(writer);

                json.Object()
                .Member("total").Number(total)
                .Member("errors").Array();

                Uri requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                for (int i = index; i < count; i++)
                {
                    ErrorLogEntry entry = (ErrorLogEntry)entries[i];
                    writer.WriteLine();
                    if (i == 0)
                    {
                        writer.Write(' ');
                    }
                    writer.Write("  ");

                    string urlTemplate = new Uri(requestUrl, "{0}?id=" + HttpUtility.UrlEncode(entry.Id)).ToString();

                    json.Object();
                    ErrorJson.Encode(entry.Error, json);
                    json.Member("hrefs")
                    .Array()
                    .Object()
                    .Member("type").String("text/html")
                    .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                    .Object()
                    .Member("type").String("aplication/json")
                    .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                    .Object()
                    .Member("type").String("application/xml")
                    .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                    .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                {
                    writer.WriteLine();
                }

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                    {
                        writer.WriteLine(@"</body></html>");
                    }
                }

                Context.Response.Output.Write(writer);
            }