Tuesday, June 10, 2008

Use Exception.ToString() instead of Exception.Message

All too often I see code that tries to log exceptions to some type of persistent storage for and later reference and error/bug tracking. Some might not be aware, but the second worst thing you can log is Exception.Message. (The first being "An error occurred.") What should you use instead? Exception.ToString()

There is a world of difference between the two. Here's an example:

   1: public partial class Default : System.Web.UI.Page
   2: {
   3:     protected void Page_Load(object sender, EventArgs e)
   4:     {
   5:         try
   6:         {
   7:             int zero = 0;
   8:             int divisionbyZero = 42 / zero;
   9:         }
  10:         catch (Exception caughtException)
  11:         {
  12:             System.Console.WriteLine(caughtException.Message);
  13:             System.Console.WriteLine(caughtException.ToString());
  14:         }
  15:     }
  16: }

When I debug, here is what I get from line 12:

Attempted to divide by zero.

And from line 13:

System.DivideByZeroException: Attempted to divide by zero.
   at Demo.Web.Default.Page_Load(Object sender, EventArgs e) in C:\Working Copies\Demo\Demo.Web\Default.aspx.cs:line 23

Which would you rather see in a system error log?

My recommendation when writing to an error log "Use Exception.ToString() instead of Exception.Message."

Saturday, April 5, 2008

Recap: Brian H Prince on "Soft Skillz" for developers

Recently at the Dayton .NET Developers Group Brian H. Prince gave a presentation titled ""

Got to say, I thought the presentation was great. Had good humor, and Brian's passion for the subject matter really shines thru.

What did I get out of it?

One of the biggest things to stick is not all mentors have to be technical mentors. You can learn a lot from managers, sales reps, even from your clients.

He also mentioned you need to keep reading. Read at least one book a month, and mix it up a bit. Read about other subjects, not just technical. Exploring these other areas will help you think of new ways to write your code.

I could keep going but I don't want to spoil too much for developer's attending the CODODN.

If you're coming to the Central Ohio Day of .NET (CODODN) make sure to set some time aside for Brian's "Soft Skillz" presentation.

Tuesday, April 1, 2008

.NET 3.5 LINQ and Inner, Outer Joins

During my Visual Studio 2008 Install Party presentation at the Dayton .NET Developers Group. Someone asked the question

"Can you do joins with LINQ?"

I thought I would share a great blog post from K. Scott Allen titled: Inner, Outer, Let's All Join Together With LINQ.

Sunday, February 24, 2008

Using Windows Live Writer for Blogging

Some of you may have noticed my blog posts have actually increased as of late. Why? I used to use Blogger's online editor and the trouble I had with it really pushed me away from writing posts. I want my posts to look just the way I want, and the editor kept taking liberties with my visual formatting. SO FRUSTRATING!

So I posted the query to Twitter:
"I'm taking a poll: What is your favorite blog editor?"

Since then I've started using, you guessed it, Windows Live Writer.

So far I'm loving it, every post I've made matches the preview. Just like a WYSIWYG should. I've also started playing with the "Plug-ins."

I've recently added Insert Code Plug-in. Awesome! No more images of code for me. Here's a sample of what it does:

Inserting HTML Code (but I'm using XML):

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <services>

Inserting C#:

public static void SomeMethod(string someString)
{
Console.WriteLine(someString);
}

Inserting TSQL:

CREATE PROCEDURE someStoredProcedure
@someMsg varchar(256) = NULL
AS
BEGIN

I can only imagine what other tools will be coming out in the future. So for right now Windows Live Writer is for me.

Tuesday, February 19, 2008

Cincinnati Users Group and the Version Control Panel

Well I just finished up being part of the Source Control Panel at the Cincinnati Users Group.

I was very enjoyable. I think I did a fine job representing Subversion. Those of you that read my blog know that I got a good understanding of the system and methodology.

For those that are interested, I tend to tag allot of my research

Actually, I was quite surprised there weren't more questions about "Version Control Best Practices," "Branching Options," and "Database Versioning"

At the end there was much discussion about the "additional features" of TFS. I thought that was a bit unfair to SVN. SVN was designed to be nothing more then Version Control. Do one thing, and one thing well, right?

<PersonalOpinion>

Sure SVN doesn't have the "baked in" features like TFS does (and I'll admit TFS' integration is awesome) but with a few extra steps you can have work item tracking with trac. If you want Continual Integration you can always run CI Factory. If you want a better repo viewing experience you can install Fisheye. If you want code review you can run Crucible. With Subversion you have so many options.

Actually, I think being able to "choose" which tools you want to mix and match is a huge perk of using SVN. However, you do have to write the "glue" that holds it all together.

</PersonalOpinion>

All in all I had a really great time. There were a flurry of good questions and conversation. In the end, I just hope I helped someone learn something.

Remember,

Any version control is better then no version control.

P.S. If you have any additional questions about version control, ask away. I would be happy to address them.

Regular Expression Negation for Required Field Validators

Out of the box ASP.NET regular expression validators have the following behavior:

Does control value match regular expression? If yes validation passes, else validation fails.

But what if you wanted the following:

Does control value NOT match regular expression? If yes validation passes, else validation fails.

Turns out there is no "If expression not match" property on a regular expression validator. Now, you can make your own custom regular expression control, or you can buy a toolset. But it turns out you might be able to do it in your regular expression itself.

This can be accomplished by using something called a Negative lookahead assertion. It succeeds if the contained expression doesn't match at the current position in the string.

The syntax is

(?!&lt;value>)

For example, say you wanted to prevent users from using an email address that contained "@spam.com". You could use the following RegEx:

*@(?![Ss][Pp][Aa][Mm][.][Cc][Oo][Mm]$).*

Now your regular expression validator will pass if their value does NOT match the regular expression.

Regular Expression are extremely powerful, and I encourage everyone to learn about them. If you can master them, consider yourself a superhero.

Sunday, February 17, 2008

Visual Studio 2008 Tour and Install Party

This month (February 27, 2008 18:00) I'll be speaking at the Dayton .NET Developers Group about Visual Studio 2008 and .NET 3.5.

The official blub is:

Visual Studio 2008 and .NET 3.5 are soon to be released, after a very public and open beta process. We will run around the products, and discuss what the new features are. This will briefly cover VS, WCF, WF, C# and VB.NET features.

<RandomSideNote>
I'm actually filling in for Brian Prince, who was filling in for Jeff Blankenburg. Both of whom are very busy men. Being that I'm a single guy, with no kids, or pets I figured I could jump in and help out. People seemed to be excited about this upcoming meeting, I just couldn't let it be canceled.
</RandomSideNote>