Better Workaround for AppFabric Cache’s Tracing Hatred

As a follow up from my last post, here’s a simple extension method to forcibly restore the default trace listener every time you invoke an AppFabric Caching Administration Management cmdlet. It’s not the prettiest code, but it’s functional:

public static class PowershellExtensions
{
public static Collection<PSObject> InvokeCommandAndRestoreDefaultTraceListener(this Pipeline pipeline)
{
bool hasDefaultTraceListener = PowershellExtensions.IsDefaultTraceListenerPresent();
Collection<PSObject> results = pipeline.Invoke();
if(hasDefaultTraceListener && !PowershellExtensions.IsDefaultTraceListenerPresent())
{
Trace.Listeners.Add(new DefaultTraceListener());
}
return results;
}
private static bool IsDefaultTraceListenerPresent()
{
foreach(TraceListener listener in Trace.Listeners)
{
if(listener is DefaultTraceListener)
{
return true;
}
}
return false;
}
}

This entry was posted in Work and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s