Skip to content

Commit

Permalink
Capture trace output during start-up and display in HUD page
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdavey committed Dec 18, 2011
1 parent 59e01d9 commit ea21684
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Cassette.Web/Cassette.Web.csproj
Expand Up @@ -56,6 +56,7 @@
<Link>SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="CassetteApplication.cs" />
<Compile Include="StringBuilderTraceListener.cs" />
<None Include="CassetteConfiguration.cs.pp" />
<Compile Include="CassetteHttpModule.cs" />
<Compile Include="AssetRequestHandler.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/Cassette.Web/HudRequestHandler.cs
Expand Up @@ -50,7 +50,8 @@ string CreateJson()
{
Scripts = scripts.Select(ScriptData),
Stylesheets = stylesheets.Select(StylesheetData),
HtmlTemplates = htmlTemplates.Select(HtmlTemplateData)
HtmlTemplates = htmlTemplates.Select(HtmlTemplateData),
StartupTrace = StartUp.TraceOutput
};
var json = new JavaScriptSerializer().Serialize(data);
return json;
Expand Down
2 changes: 2 additions & 0 deletions src/Cassette.Web/Resources/hud.htm
Expand Up @@ -63,6 +63,8 @@ <h1>Cassette</h1>
</li>
</ul>

<pre data-bind="text: StartupTrace"></pre>

<script type="text/javascript" src="?knockout.js"></script>
<script type="text/javascript">
var data = $json$;
Expand Down
21 changes: 19 additions & 2 deletions src/Cassette.Web/StartUp.cs
Expand Up @@ -55,7 +55,15 @@ public static class StartUp
static IsolatedStorageFile _storage;
static CassetteApplicationContainer<CassetteApplication> _applicationContainer;
static Stopwatch _startupTimer;
readonly static object CreationLock = new object();
static readonly object CreationLock = new object();
/// <summary>
/// Collects all of Cassette's trace output during start-up.
/// </summary>
static readonly TraceListener StartupTraceListener = new StringBuilderTraceListener
{
TraceOutputOptions = TraceOptions.DateTime,
Filter = new EventTypeFilter(SourceLevels.All)
};

/// <summary>
/// The function delegate used to create Cassette configuration objects for the application.
Expand All @@ -71,11 +79,12 @@ static StartUp()
CreateConfigurations = CreateConfigurationsByScanningAssembliesForType;
}


// ReSharper disable UnusedMember.Global
public static void PreApplicationStart()
// ReSharper restore UnusedMember.Global
{
Trace.Source.Listeners.Add(StartupTraceListener);

_startupTimer = Stopwatch.StartNew();
Trace.Source.TraceInformation("Registering CassetteHttpModule.");
DynamicModuleUtility.RegisterModule(typeof(CassetteHttpModule));
Expand All @@ -102,6 +111,9 @@ public static void PostApplicationStart()

Trace.Source.TraceInformation("Cassette startup completed. It took " + _startupTimer.ElapsedMilliseconds + "ms.");
_startupTimer.Stop();

StartupTraceListener.Flush();
Trace.Source.Listeners.Remove(StartupTraceListener);
}

// ReSharper disable UnusedMember.Global
Expand All @@ -113,6 +125,11 @@ public static void ApplicationShutdown()
_applicationContainer.Dispose();
}

static internal string TraceOutput
{
get { return StartupTraceListener.ToString(); }
}

static IEnumerable<ICassetteConfiguration> CreateConfigurationsByScanningAssembliesForType()
{
Trace.Source.TraceInformation("Creating CassetteConfigurations by scanning assemblies.");
Expand Down
50 changes: 50 additions & 0 deletions src/Cassette.Web/StringBuilderTraceListener.cs
@@ -0,0 +1,50 @@
using System.Diagnostics;
using System.Text;

namespace Cassette.Web
{
class StringBuilderTraceListener : TraceListener
{
readonly StringBuilder builder = new StringBuilder();

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
{
if (eventType == TraceEventType.Information)
{
WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
}
else
{
base.TraceEvent(eventCache, source, eventType, id, message);
}
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
var message = args == null ? format : string.Format(format, args);
if (eventType == TraceEventType.Information)
{
WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
}
else
{
base.TraceEvent(eventCache, source, eventType, id, message);
}
}

public override void Write(string message)
{
builder.Append(message);
}

public override void WriteLine(string message)
{
builder.AppendLine(message);
}

public override string ToString()
{
return builder.ToString();
}
}
}

0 comments on commit ea21684

Please sign in to comment.