2012-04-10

To log or not to log?

To log or not to log?

You see, to log is mandatory if your software does more than a hello world. But the thing is, I've always thought it wasn't cool to stuff chunks of code all over the code, it makes the code ugly, and less readable.
I was using Log4Net, and it is not all bad, but as I said before, I don't like my code "dirty".

So today a friend of mine told me about the ELMAH, and I thought it is amazing. You might say it isn't that clean, if you want to log something you have to write code to log.
Of course you have, how else it is supposed to guess what you want to log???
But that's not even the most important part, the log caught errors and log them in a very nice way.

As I was trying to set aside a few hours to install and configure it, I've remembered one old Scott Hanselman's video about the .net gems nugets, and how easy it was to set up the addons using nugets.


This works like a charm, you can have it all in 3 simple steps:
  1. Install the ELMAH nuget.
  2. Create a database for it and run the sql file App_Readme/Elmah.SqlServer.sql
  3. Update the ELMAH connection string  in your web.config, just look for the already created key: <add name="elmah-sqlserver" connectionString="Data Source=****;User ID=****;Password=****;Initial Catalog=****;" providerName="System.Data.SqlClient" /> and then insert values about the database you just created.
And that's it, done, Easy Peasy...

Ah, and No, you don't have to look database records, or a shitty unformated text file...
Just access yourapp/elmah.axd and you will see something like this:


And clicking details, will be something like this:


Go ahead and start feeling guilty for logging all your mistakes. Forever. Because it doesn't forgive, and it never forgets...