Skip to content

Instantly share code, notes, and snippets.

Created January 27, 2012 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4e13cd84e5c9aa9b9310 to your computer and use it in GitHub Desktop.
Save anonymous/4e13cd84e5c9aa9b9310 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using AzureLogViewer.Models;
using AzureLogViewer.Tools;
using AzureLogViewer.ViewModel;
using AzureLogViewer.ViewModel.Log;
namespace AzureLogViewer.Views.Log
{
/// <summary>
/// View for ShowLog controller.
/// </summary>
public class ShowLogView : AbstractXmlViewPage<ShowLogViewModel>
{
private static readonly List<LogEntryLevel> s_knownLevels = new List<LogEntryLevel>
{
LogEntryLevel.Error,
LogEntryLevel.Warning,
LogEntryLevel.Information,
LogEntryLevel.None,
};
private static LogEntryLevel GetKnownLevel(LogEntryLevel level)
{
return s_knownLevels.First(x => (level & x) == x);
}
private object ShowLevelIcon(LogEntryLevel level)
{
return Html.XCreateSpan(
Html.CreateClassAttribute(String.Format("log-level-{0}", GetKnownLevel(level).ToString().ToLower())),
new XAttribute("title", level.ToString()));
}
private Tuple<XAttribute[], IEnumerable<object>> ShowLogEntry(LogEntry entry)
{
return new Tuple<XAttribute[], IEnumerable<object>>(
new[]
{
Html.CreateClassAttribute(
"log-record",
string.Format("log-record-{0}", GetKnownLevel(entry.Level).ToString().ToLower())),
new XAttribute("entry-partitionkey", entry.PartitionKey),
new XAttribute("entry-rowkey", entry.RowKey),
},
new object[]
{
new[]
{
Html.CreateClassAttribute("log-level-icon"),
ShowLevelIcon(entry.Level),
},
Html.XMultiline(
ShowDate(entry.Timestamp),
entry.Role,
string.Format("Instance {0}", entry.ShortInstance)),
Html.XCreateDiv(Html.CreateClassAttribute("plaintext"), entry.Message),
});
}
/// <summary>
/// Returns the page title.
/// </summary>
protected override string GetTitle()
{
return String.Format("Log '{0}'", Model.CurrentLogName);
}
/// <summary>
/// Returns the logs selection menu (for header).
/// </summary>
private XElement GetPeriodsList()
{
return Html.XCreatePageOuter(
PeriodExtensions.AvailablePeriods,
Model.CurrentPeriod,
period => Url.RouteUrl("ShowLog", new { logName = Model.CurrentLogName, period }),
period => String.Format("Last {0}", period.ToString().ToLower()));
}
/// <summary>
/// Returns the page main content (excuding headers/footers/etc).
/// </summary>
protected override XElement[] GetSpecificBodyContent()
{
return new[]
{
Html.XCreateHeader(2, GetTitle()),
GetPeriodsList(),
Model.HasRemainingLogEntries
? ShowWarning("Some entries might not be shown. Total {0} entries shown.", Model.LogEntries.Count)
: null,
Model.LogEntries.Any()
? Html.XCreateTable(
attributes: new[] { Html.CreateClassAttribute("list") },
tHeadData: new[]
{
Html.XCreateTableHeadRow(
tCellsContent: new object[]
{
new object[]
{
new XAttribute("colspan", "2"),
Html.CreateClassAttribute("loghead-information"),
"Information"
},
"Message",
}),
},
tRowsContent: Model.LogEntries.OrderByDescending(entry => entry.Timestamp).Select(ShowLogEntry),
tFootData: new[]
{
Html.XCreateTableHeadRow(
tCellsContent: new object[]
{
new object[]
{
new XAttribute("colspan", "3"),
string.Format("Total {0} entries", Model.LogEntries.Count)
},
}),
})
: ShowWarning("No entries found for your request."),
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment