Color ConsoleTraceListener

I was watching trace output flow across a console window when I had the thought that it would be much easier to see warnings and errors if they were color coded. I started off to write a color version of ConsoleTraceListener but decided a quick Google search was in order to see if this had been done. Sure enough, I quickly found a clean implementation from Joshua Flanagan. His site has a good usage example with screenshots....

March 3, 2011 · Joshua Marble

Formatted XmlWriterTraceListener

I was wanting the output of the XmlWriterTraceListener to be formatted, so I figured I could just adjust some settings similar to the XmlWriter class and get formatting, indentation, etc. However, upon viewing the XmlWriterTraceListener code in Reflector, I found that it writes out its XML using a plain TextWriter, which means no intelligent XML formatting. I imagine this is for performance reasons or some other good reason, but if you really want or need formatted log output, you’re out of luck....

February 25, 2011 · Joshua Marble

Viewing Exceptions In The Immediate Window

I showed a coworker the other day how you can view exception objects even when you don’t have an exception variable. For those that don’t know this, it is a useful debugging tip to remember. When handling exceptions, you can use the automatically declared $exception variable in the immediate window or watch windows to access properties of the most recent exception. This can be a handy tip when you need a call stack or any exception type and don’t know from the code....

January 31, 2011 · Joshua Marble

Visual Studio Error E_INVALIDARG

I was trying to run some unit tests using MSTest but it could not load the test assemblies, reporting error code E_INVALIDARG. Google pointed me to a bunch of people talking about an ASP.Net web exception and clearing ASP.Net temporary files. Needless to say, this did not fix my problem since I am simply trying to run tests. After repairing Visual Studio and exhausting all other efforts, I realized that the only related thing I had done lately was install Gallio to try out its test runner and MbUnit....

June 18, 2010 · Joshua Marble

Studio Styles

A friend showed me a website today called studiostyles that is a collection of user-submitted Visual Studio color schemes. The site is nice and simple, and it provides download links for Visual Studio settings files for each Visual Studio version. I went ahead and entered the theme I use plus some modifications. I called it Dark Bliss. Check it out and let me know what you think. http://studiostyles.info/schemes/dark-bliss

June 16, 2010 · Joshua Marble

Enumerating IDataReader With LINQ

Today a coworker was trying to read an IDataReader into an anonymous type for an internal tool. This got me thinking about how LINQ methods could be useful when working with a DataReader since it’s basically a collection of records that you enumerate. In thinking about this, I came up with the DataReaderEnumerable class. The DataReaderEnumerable is a simple class that wraps an IDataReader and implements IEnumerable. IEnumerable is a very simple interface, and the guts of the enumerating actually happen in the IEnumerator that is returned from the GetEnumerator() method....

April 8, 2010 · Joshua Marble

How To Really Cancel TreeNode Check

The TreeView control can support checkboxes for nodes by setting the CheckBoxes property to true. In my custom TreeView with checkboxes, I need to “disable” some nodes and not allow them to be checked. At quick glance, the OnBeforeCheck virtual method seems to be the correct place to cancel the checking using the TreeViewCancelEventArgs .Cancel property. If you have come to the same conclusion then let me confirm that you are indeed correct that this is the place to cancel node checking....

March 16, 2010 · Joshua Marble

Filter Stacking LINQ Expressions

Recently I was attempting to devise a way to filter down a large list with user generated conditions. These could be handled different ways depending on user input, so I couldn’t use a predefined lambda expression to filter the collection. What I wanted was a way to build an unknown number of lambda expressions from the user input and use them to filter the collection. I ended up parsing the user input and adding each search expression to a List>....

December 23, 2009 · Joshua Marble

A Catch in the Conditional Operator

Have you ever seen a conditional (ternary) operator seemingly evaluate the condition incorrectly? Take a method that returns a display string using an object as input. static string GetPersonSentence(Person p) { return "The person's name is " + p != null ? p.Name : "Unknown"; } Now consider calling this method with an instance of a person and with a null value. GetPersonSentence(new Person { Name = "Bob" }) GetPersonSentence(null) It seems like it should output fine both ways, but it breaks on the null value call....

November 2, 2009 · Joshua Marble

Anonymous Delegates During Enumeration

I had no idea what to title this post, but that’s the best I could come up with. I want to highlight an interesting scenario that can occur when trying to use anonymous delegates while enumerating a collection. Consider the code below that creates buttons in a FlowLayoutPanel and adds a click event handler as an anonymous delegate. (For the purposes of the example, I’m not using the sender Button text, which would be a simple way to handle this scenario....

September 30, 2009 · Joshua Marble