Easiest best way to get Elmah going on MVC

Posted on: Thu Jan 19 19:35:14 -0800 2012. Updated on: Thu Jan 19 19:46:20 -0800 2012.
Category: .NET C#

ELMAH is a great error logging app, that can be installed on any MVC app. I choose to use SQL compact so as not to bloat my main app's sql server DB with garbage and over XML because it otherwise you end up with a million files.

Step 1: Go to nuget, search elmah, and install package that says Elmah on SQL compact.

Step 2: Right click on project, "add deployable dependencies", check the box that says SQL compact. If this box is greyed, you need to install some extra stuff and VS tools.

Step 3: Secure elmah.axd to only admin roles in web.config

 
    
      
        
        
      
    
  

Step 4: Config. elmah section in web.config


    
    
    
      
        
      
    
  

Step 5: Global asax: add functions

// ELMAH Filtering
		protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
		{
			FilterError404(e);
		}

		protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
		{
			FilterError404(e);
		}

		// Dismiss 404 errors for ELMAH
		private void FilterError404(ExceptionFilterEventArgs e)
		{
			if (e.Exception.GetBaseException() is HttpException)
			{
				HttpException ex = (HttpException)e.Exception.GetBaseException();
				if (ex.GetHttpCode() == 404)
					e.Dismiss();
			}
		}

// seperate class
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
	{
		public void OnException(ExceptionContext context)
		{
			// Long only handled exceptions, because all other will be caught by ELMAH anyway.
			if (context.ExceptionHandled)
				ErrorSignal.FromCurrentContext().Raise(context.Exception);
		}
	}

global asax intercept errors:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
		{
			filters.Add(new ElmahHandledErrorLoggerFilter());
			filters.Add(new HandleErrorAttribute());
		}

Web.config transformations summary

Posted on: Tue Oct 04 14:51:52 -0700 2011. Updated on: Tue Oct 04 14:56:30 -0700 2011.
Category: .NET C#

Here is a summary of the useful web.config transformations to use.

Replace - Completely replaces the first matching element along with all of its children from the destination web.config (e.g. staging environment’s web.config file). Do note that transforms do not modify your source web.config file.

    
        
    

Remove - Removes the first matching element along with all of its children


· RemoveAll - Removes all the matching elements from the destination’s web.config (e.g. staging environment’s web.config file).


    

· Insert - Inserts the element defined in web.staging.config at the bottom of the list of all the siblings in the destination web.config (e.g. staging environment’s web.config file).


     

· SetAttributes - Takes the value of the specified attributes from the web.staging.config and sets the attributes of the matching element in the destination web.config. This Transform takes a comma separated list of attributes which need to be set. If no attributes are given to SetAttributes transform then it assumes that you would like to Set all the attributes present on the corresponding node in web.staging.config




· RemoveAttributes - Removes the specified attributes from the destination web.config (i.e. staging environment’s web.config file). The syntax example shows how multiple attributes can be removed.



InsertAfter (XPath) - Inserts the element defined in the web.staging.config exactly after the element defined by the specified XPath passed to “InsertAfter()” transform. In the syntax example the element will be exactly inserted after the element in the destinationXML.

InsertBefore (XPath) - Inserts the element defined in the web.staging.config exactly before the element defined by the specified XPath passed to “InsertBefore()” transform. In the syntax example the element will be exactly inserted before the element in the destinationXML.

Turn text http links to live html links

Posted on: Mon Sep 05 08:38:36 -0700 2011. Updated on: Mon Sep 05 08:38:36 -0700 2011.
Category: .NET C#

Very useful regex code to convert plain http links in a block of text to live HTML links.

http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx

don't forget that for https change regex to "http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"

List table size script

Posted on: Tue Jun 14 11:44:08 -0700 2011. Updated on: Tue Jun 14 11:44:44 -0700 2011.
Category: SQL Server 2005

This is a super handy script that can be found in original source here: http://www.keyboardface.com/archives/2007/06/12/mssql-table-size-for-all-tables/

It basically lists all the tables with their sizes on your db

declare @RowCount int, @tablename varchar(100)
declare @Tables table (
PK int IDENTITY(1,1),
tablename varchar(100),
processed bit
)
INSERT into @Tables (tablename)
SELECT TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE' and TABLE_NAME not like 'dt%' order by TABLE_NAME asc

declare @Space table (
name varchar(100), rows nvarchar(100), reserved varchar(100), data varchar(100), index_size varchar(100), unused varchar(100)
)
select top 1 @tablename = tablename from @Tables where processed is null
SET @RowCount = 1
WHILE (@RowCount <> 0)
BEGIN
insert into @Space exec sp_spaceused @tablename
update @Tables set processed = 1 where tablename = @tablename
select top 1 @tablename = tablename from @Tables where processed is null
SET @RowCount = @@RowCount
END

update @Space set data = replace(data, ' KB', '')
update @Space set data = convert(int, data)/1000
update @Space set data = data + ' MB'
update @Space set reserved = replace(reserved, ' KB', '')
update @Space set reserved = convert(int, reserved)/1000
update @Space set reserved = reserved + ' MB'

select * from @Space order by convert(int, replace(data, ' MB', '')) desc

Create Membership Tables in SQL database

Posted on: Mon Apr 18 19:59:32 -0700 2011. Updated on: Mon Apr 18 19:59:32 -0700 2011.
Category: .NET C#

Run this if you want to create the membership tables for the asp.net business application or silverlight business application template

This creates the membership/provider tables and all the objects it needs to store your login and password of the forms.

Open the command prompt on your local computer, and navigate to: C:\WINDOWS\Microsoft.NET\Framework\[framework version]

Execute the command: aspnet_regsql.exe -S DBServerName -E -A all -d DBName

Note the -E is for trusted connection to databse

Telerik RadGrid filter case sensitive

Posted on: Tue Apr 12 07:40:46 -0700 2011. Updated on: Tue Apr 12 07:41:52 -0700 2011.
Category: Telerik Controls and Sitefinity

Set the Rad Grid filtering to be case sensitive or insensitive with this easy setting


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.