I like to fill my test code with TONS of logging. I log damn near everything because I hate it when the team has to waste time answering, “Can you get me a repro?” when we can just peruse the logs to see what went wrong. I also like to watch the traces get logged in real time using DebugView as my tests execute. So after I made some recent changes in my code to programmatically manage our AppFabric cache, I was super bummed to see my real time logging completely disappear.
The following code shows a snippet of what my test code is doing to repro the issue:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InitialSessionState state = InitialSessionState.CreateDefault(); | |
state.ImportPSModule(new string[] { "DistributedCacheAdministration", "DistributedCacheConfiguration" }); | |
state.ThrowOnRunspaceOpenError = true; | |
using(Runspace rs = RunspaceFactory.CreateRunspace(state)) | |
{ | |
rs.Open(); | |
using(Pipeline pipeline = rs.CreatePipeline()) | |
{ | |
Command command = new Command("Use–CacheCluster"); | |
command.Parameters.Add(new CommandParameter("Provider", "System.Data.SqlClient")); | |
command.Parameters.Add(new CommandParameter("ConnectionString", "Data Source=.\\SQLEXPRESS;Initial Catalog=AppFabric;Integrated Security=True")); | |
pipeline.Commands.Add(command); | |
Trace.WriteLine(string.Format("Invoking: {0} {1}", | |
command.CommandText, | |
string.Join(" ", command.Parameters.Select(p => string.Format("-{0} \"{1}\"", p.Name, p.Value))))); | |
pipeline.Invoke(); | |
} | |
Trace.WriteLine("I expect to see this line and all other traces after this."); | |
} | |
Trace.WriteLine("Ending program"); |
The traces simply no longer get output to the default trace listener. Sure, if I setup a text file listener in my app.config, the traces are properly logged to the file. But what fun is that? I need this stuff in real time now, not after my 100 or so tests run.
So I posed this question to @drub0y, Mimeo’s Chief Software Architect. Sure enough minutes later he responded with:
Well damn. JustDecompile to the rescue. Let’s see how we could have done this ourselves.
- Fire up JustDecompile, and then load %PROGRAMFILES%\AppFabric 1.1 for Windows Server\Microsoft.ApplicationServer.Caching.Core.dll to the assembly list.
- Click on the Search button
- Go to the Full Text tab and enter: “Listeners”
You should then get a single hit:
And sure enough, when you double click on that, you get the code that @drub0y sent me in the mail above.
The fix? Easy. I just had to add a simple Trace.Listeners.Add(“DefaultTraceListener”) in my code after invoking the powershell pipeline.
Pingback: Better Workaround for AppFabric Cache’s Tracing Hatred | Nithin's Blog