Saturday, November 30, 2013

Getting started with ELMAH and ASP.NET MVC

We all need a framework to log exceptions in our web application. Today, We are going to learn about same. ELMAH(Error Logging Modules and Handlers) is an application wide error logging framework. How we can install it in ASP.NET MVC.

What is ELMAH:

ELMAH(Error logging Modules and Handlers) is an application wide error logging facility that is an pluggable framework. It can dynamically added to running an ASP.NET application. You also have Nuget package for the same.
You can find more information about on following link.

Once you install ELMAH, You can have following advantages
  1. You can log almost all unhandled exceptions in the system.
  2. An RSS feed of last 15 errors from the log
  3. An email notification of each error occurs.
  4. A remote web page to log all the exceptions.
  5. You can insert log a various locations like files text file, SQL Server, Oracle etc.

ELMAH NuGet Package:

There is a nuget package also available for ELMAH. You can find at following link.

and Following Command you have to run for installing NuGet Pacakge.


Getting Started with ASP.NET MVC and ELMAH:

So Let’s first create a new project with ASP.NET MVC  from Visual Studio via File->New Project –>ASP.NET MVC 4 Application.


Once you click OK it will ask you for type of application like following. We are going to select internet application here.


Once you click on OK it will create new ASP.NET MVC 4 Application. Once our ASP.NET MVC 4 application is ready. It’s time to add ELMAH to ASP.NET MVC4 Application. The best way to add ELMAH is nuget package so we don’t have to worry about the configuration settings in web.config. So Let’s go to Tools-> Library Package Manager-> Package Manger Console and run the NuGet Command like following.


So now if you see in web.config all configuration is added.
Now let’s run our application via pressing f5.


Now let’s create some unhandled errors for example test Action Result does not exist in our ASP.NET MVC application. So Let’s write that in URL and it will give error like following.


Now let’s see whether this error is logged by ELMAH or not. Here you go


Once you click on details. It will show details error like following.


That’s it. You can see now it’s very easy to log errors and unhandled exceptions in ELMAH. There are various options to log errors in ELMAH we will see that in future posts.

Hope you like it. Stay tuned for more..

How to find days in year except Saturday and Sunday in C#

Before 2 years I have written a blog post to find Saturday and Sunday in given date range.  It has gain a lot of popularity and on that blog post one user ask about how we can find days in year except Saturday and Sunday in C#. This post is a in reply for that. Following is a code for that.
using System;

namespace DatimeApplication
    class Program
        static void Main(string[] args)
            DateTime startDate = new DateTime(2013, 1, 1);
            DateTime endDate = new DateTime(2013, 12, 31);

            TimeSpan diff = endDate - startDate;
            int days = diff.Days;
            int j=0;
            for (var i = 0; i <= days; i++)
                var testDate = startDate.AddDays(i);
                if (testDate.DayOfWeek != DayOfWeek.Saturday && testDate.DayOfWeek != DayOfWeek.Sunday)
                    j = j + 1;
            Console.WriteLine(string.Format("Total Days:{0}",j));

If you see above code,  Its very easy to find all days except Saturday and Sunday in a year. I have created two dates with Start date and end date and then I found a difference between those two days. And then in for loop I have checked whether they are weekdays then I have printed a date for that. I have calculated all days except Saturday and Sunday for year 2013. At the end I have printed total days also.

Once you run the code following is a output as expected.


That’s it. Hope you like it. Stay tuned for more…
Thursday, November 14, 2013

My visual studio settings

Lots of people send me email about what kind of visual studio settings I am using and I am replying them again and again so I thought it will be good idea to write a blog post about it.

Yes, Since last 2 months I have been using dark theme of Visual Studio 2012 but I am not using default dark theme color settings of visual studio. I’m using dark theme color settings created by Keith Elder. You can find more information about color settings of this theme from following link.

I simply love this one and its works on all flavours of Visual Studio starting from Visual Studio 2008 to 2012. Below are few screenshots from that.


Here is another screenshot.


It’s been great for eyes and I can work with very easily and as you may notice my default font for visual studio is Consolas and size is 11.

Now lots of people ask me whether its working fine with resharper or not and Its works like a charm. See following screenshot.


And this is how intellisense look with Visual studio 2012 dark theme.


And this how HTML part of ASP.NET MVC looks in the dark theme.


If you still never try of any dark theme for visual studio then try it and it is great for your eyes. I would like to say big thanks to  Keith Elder for creating a great black theme. If you want to try same theme then you can download that from above link I have mentioned.
Hope you like it. Stay tuned for more..Happy Programming Smile
Friday, November 8, 2013

How to find space used by table in SQL Server

Recently there was requirement for a client where we need to find space occupied/used  by particular table in SQL Server. I did some research a found a very cool way to get space used information from SQL Server.

With SQL server 2005 and higher version you can use ‘sp_spaceused’ to find space used by table in SQL Server.

Following is a syntax for that.
sp_spaceused N'YourTableName'

You can find more information about it from the following link.

When you run this in SQL Server, Its displays Number of Rows, disk space served and disk space used by a table or indexed view and also displays disk space reserved and used by database.


Here in the above example, You have seen that I have used to find space for employee table in my blog sample database and its providing information about rows, reserved space, data size, index_size space and unused space.

That’s it. Hope you like it. Stay tuned for more.
Thursday, November 7, 2013

How to find path of database data files and log files in SQL Server

In this post, We will learn about different technique to find path of data files and log files on SQL Server. Let’s go through one by one of them.

The first technique, We are going to use is very simple way to finding file path for database. But with this you can find path for one database only. It’s very simple, Select database on object explorer and right click and select properties.


Now once you click on properties a dialog box appear like following. Select files and go to path column it will show path of database data files and log files.

Now we will learn another technique where we can find database data file and log file path from query. Below is query for that.

SELECT name, physical_name FROM sys.master_files where database_id= db_id('blog')

Now once you run this query it will load result like following.


That’s it you can find you database file path very easily. You can use above query to find files path for multiple databases also without using where clause. Hope you like it. Stay tuned for more.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews