Thursday, 12 March 2009

yield and iterators

I've just implemented an iterator using the new c#2 yield keyword, it took me about 5 mins and worked so easy. I love this new feature. It makes things so easy.

I had a DateTimeRange class and I needed to get the first day of every month that was covered by the range.

Here is the code:

   49 public IEnumerable<DateTime> MonthStarts

   50 {

   51     get

   52     {

   53         var current = new DateTime(Start.Year, Start.Month, 1);

   54 

   55         while (current <= End)

   56         {

   57             yield return current;

   58             current = current.AddMonths(1);

   59         }

   60     }

   61 }



This can then be used in simple foreach statements.

Submit this story to DotNetKicks Shout it

Tuesday, 10 March 2009

Blogging and including code

Being new to the blogging world, my first attempt took rather longer than I thought as I was having to format all of my code in my previous blog.

After starting to do this by hand I knew that there must be an easier way but couldn't see it in the built in creator.

After a simple google search I came across this really helpful tool:

Copy Source as HTML

So, I installed this and away I went. More minutes of my life saved.

Thanks to Colin Coller for doing the work up front for this.

Submit this story to DotNetKicks Shout it

log4net logging causing the computer to beep

Whilst working on my current project, I came across a rather annoying problem:

During service calls, we log the input and output params. When we started to integration test the system and a user performed a search, a beeping could be heard from the computer.
This, obviously caused some distress to anyone near the PC as it was constant and annoying.

As this was only being heard during a search service call the first, and very incorrect thought, was that the search was very poor and sql server was struggling.

After 5 seconds of debugging I found the following line to be the cause of the problem:

logger.Info(GetExitMessage(invocation, returnValue, executionTimeSpan));

We were using reflection to generate our message and log4net as the logger. I was still unsure of what the problem was.

I looked at the message that was being generated and replicated the problem in a simple console application. I eventually got the following piece of code to reproduce the mysterious beep:

    1 using System;

    2 using log4net;

    3 

    4 namespace log4netProblem

    5 {

    6     class Program

    7     {

    8         static ILog _logger = LogManager.GetLogger(typeof(Program));

    9 

   10         static void Main(string[] args)

   11         {

   12             try

   13             {

   14                 log4net.Config.BasicConfigurator.Configure();

   15                 string s = "•";

   16                 _logger.Info(s);

   17             }

   18             catch (Exception e)

   19             {

   20                 Console.Write(e);

   21             }

   22             finally

   23             {

   24                 Console.WriteLine("Press any key to exit...");

   25                 Console.ReadKey();

   26             }

   27         }

   28     }

   29 }



Yes yes yes, I see the problem now. "•" was the shortcut for making a console beep.

It turns out that in my haste of setting up the appenders in the web application, I had left the console appender in there and the PC wasn't about to catch fire at all.

All I can say is oops.

Submit this story to DotNetKicks Shout it

Monday, 2 March 2009

Debugging web apps with Visual Studio and logging with log4net

If like myself you use log4net and develop in Visual Studio, you may want to see what is being logged without having to leave the comfort of the IDE and look at a text file.

To get your logged output to appear in the output window of visual studio add this appender to your log4net configuration:

<appender name="ASPNETDebugger" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.SimpleLayout" />
</appender>

I've often found that I forget this little snippet of code but find it very useful when debugging a web app.

Submit this story to DotNetKicks Shout it