Error Logging ASP.Net with Elmah

Posted by beakersoft | Posted in Programming | Posted on 23-05-2008

1

I’ve been in the situation a few times with ASP.Net applications where someone says to you ‘I got this error on screen when I did x, I don’t know what it said but i couldn’t work.’

Odds are you have got an unhanded exception in your web application, but how are you meant to debug it when the error has long gone? Elmah (error logging and error handling modules for asp.net) may well be your saviour. Download it out now from http://code.google.com/p/elmah/

So, what does it do?

Well, once you have it configured on your application or server, every time something causes an unhanded exception it will log it into a database, send you an email alert and fix the code for you (last part was a joke!). So, now when the user tells you they have had an problem you can look in the system and see exactly what happened.

elmah example

The application is open source, and is based on the article at http://msdn.microsoft.com/en-us/library/aa479332.aspx, check that article out if you want to know more about how it was written

Installing it

You can run it in two ways, on a particular web application or at the server level. As I was a bit confused at first as to how to set it up, here is a quick guide to installing it for an individual application and log the errors onto a SQL 2000 server.

  1. Download the zip file from the website, if you are using dotnet 2.0 you will need the latest beta version.
  2. Extract the files, in the src/Elmah/ folder find the Database.sql script. This is the script that will build the tables it needs. Create a new database and run this script on the database
  3. Next, go into the bin/net-2.0/Release/ folder, and copy the Elmah.dll and Elmah.xml files into you applications /bin folder
  4. Once you have the assembly, you need to configure your application to use it. You do this by adding some sections into your applications web.config file. In the /samples folder there is an example web.config file you can get all the information out of to put in your file. For the most part it is easy to follow, but I had a couple of problems.
  5. The first one was the database connection, make sure you add a new connection to your <connectionStrings> section that points at the database you have created.
  6. You then need to point to this connection in the connectionStringName section of the elmah errorLog.
  7. Once you have added the other sections into your config file you should just be able to browse to your site /elmah.axd to see your errors

You can also configure options such as getting it to send an email when an exception occurs, subscribe to the log as an rss feed etc.

So now you should have a handy reference point for all your exemptions. You can tell the user you are looking into there problem before they even report it to you!

Webslices in WordPress

Posted by beakersoft | Posted in Programming | Posted on 24-03-2008

18

Wordpress and Webslices header

One of the new features in the beta of Internet Explorer 8 is something called Webslices. These work in a similar way to RSS feeds, but allow you to subscribe to a section of a web page, as opposed to just a feed. The section of the page you subscribe to is a div with a class name of hsclice.

If a page contains webslice’s that can be subscribed to, a little purple icon shows in the IE tool bar, along with any rss feed options for the page. If you click on the webslice option a new link appears in the favorites bar, when you click on this you get the contents of the div. If the content of the div is updated, the item in the favorites bar shimmers and goes bold to inform you of the update.

Webslice

So, I have written a small php script that can be used on a WordPress blog to subscribe to the 7 last posts as a webslice. At the moment all I have is a script, but if people are interested I can turn it into a WordPress plugin. You can download all the parts as a .zip file at the end of the post, but here is what it contains

  • readme.txt – This file contains the bit of HTML code you need to insert in your index.php file (or where ever you want the webslice subscription option to show) and more detailed install information.
  • webslice.php – The actual file that contains a div with the last 7 posts
  • webslice.css – Style sheet for the php file

When you have downloaded the files, change the css to suite you, insert the div code into your page, upload to the server and away you go.

More Info on the script

When I first looked at adding a webslice to my WordPress blog, I thought i would just place the hslice div around the existing previous posts section in my side bar. I went away from that idea though as I wanted the ability to format it differently, maybe add a graphic to the bottom etc so i went with an external file.

To make the page flag as having a webslice, but re-direct it to another page for the actual div, I used the class=”feedurl” option in the div. This just tells IE to look in a different place for the actual content. You can tell it to look at a page or an rss feed. The div containing all this info is hidden so it doesn’t interfere with any of the existing front page content.

The script it’s self is very straight forward. It just looks in your WordPress DB for the last 7 (you can easily change this) posts , and puts them into n unordered list on the page, the formating is controlled by an external style sheet.

Please note this might not be 100% reliable. IE 8 is still in beta so its possible (but unlikely) the way it handles Webslices could change. I have also only tested this on one machine running the new IE, so it might not always work right. If you have problems get in contact and ill try and iron them out.

As I mentioned before if there is enough interest I might turn it into a plug-in

Edit: Since upgrading to the release version of Internet Explorer 8, any web slices I had suddenly stopped working. All I kept getting was ‘page cannot be displayed’ error in the space where the slice was. After much searching and head scratching I finally found the solution at http://support.microsoft.com/kb/969213

It turns out that Google Gears was stopping them from working, just disable the add on in IE and they start to work fine again

Download The Zip Archive

Download the script: wordpress_webslice.zip

Flickr, Google Maps and Classic ASP

Posted by beakersoft | Posted in Programming | Posted on 03-01-2008

0

Header

I went on Holiday to the German Beer festival, Oktoberfest and wanted to be able to upload photos from my camera phone onto Flickr, so I can put them onto a website I run. I also wanted to put the pictures onto a Google map. There were quite a few things that would allow me to do this, but none met my criteria:

  • Most of the scrips i found were PHP or .net, I needed an classic asp one
  • I needed to be able to tag the GPS co-ords into a tag on the photo. My camera phone did not have GPS built in, so I wanted to add two tags to them containing the info, called geo:lat=[xxx] and geo:lon=[xxx]

So, I came up with this script, you can see it in action at http://www.oldmanales.co.uk/feat_OktoberFest07.asp

In order to use the script, you will need a couple of things:

Once you have these, change the API key values to yours in the script, then upload it to your server. Now, call the script something like this:

FlickrTags2Google.asp?lat=11.511955&long=48.129893&tags=Oktoberfest_07&flickrID=7389734@N03

The lat and long parts of the string are where in the world the map will be placed when the pages loads, the tags are a comma separate list of tags to show on the map, in this case everything I have tagged as Oktoberfest_07. The last parameter is your flickrID.

Hopefully, when your call the page now you should get something like the screen shots below, the photos are indicated by the little camera icon, when you click on them you should get a popup preview of the image along with some info on the photo:

Preview1

Download The ASP Script

Download the file :FlickrTags2Google.asp
(and dont forget to rename it to a .asp extension otherwise it wont run.)

Hopefully when I get time I will document the code a bit better. If you find problems with it, or have any good ideas for enhancements let me know.

Going Green – Creating a PC Shutdown Script

Posted by beakersoft | Posted in Programming | Posted on 15-11-2007

0

There’s been a lot of talk throughout the IT industry recently about being more ‘Green’. Well, being all for that and also sick of users who leave there pc’s turned on all night for no good reason I have come up with a script to turn of pc’s.

The script looks into Active Directory for computer objects in a specific OU, loops through all of the computers and creates a batch file that calls the windows script Shutdown.vbs. This is a script that comes bundled with XP, and allows an administrator to shutdown a remote machine from the command line.

You can download the script from http://www.beakersoft.oldmanales.co.uk/ShutDownPCs.txt

Rename the file to a .vbs file, change the 3 variables at the top of the script to match your environment and it should create your file. A couple of things to note before using this script:

BE CAREFUL! You could end up shutting down servers or client machines that are being used. I would make sure first the pc’s are ok to shutdown, and only run the script after you know the users aren’t likely to be using them.

This script will not forcefully close open applications, so not to lose any users work. If you really want to force the machine down (even if the user has un-saved open work) you can add a ‘-f’ switch to the script on the line where it writes the computer name out to the file.

You will need to be a Domain admin or similar to shutdown the remote machines, or you’ll just get an access denied errors.

As I mentioned before this could be dangerous so make sure the pc’s are ok to shut down before running the script.