Posts Tagged ‘web’

Health Monitoring in ASP.NET

August 22nd, 2009

Unless your code is perfect, you’re bound to get runtime errors. Exceptions may not get caught, connections may not get closed off, and eventually, at some point, you’ll divide by 0. While you can’t prevent them, you can be aware of them using ASP.NET’s built in health monitoring provider. Health monitoring is configured in the web.config and can be set to use various types of logging, from writing to a SQL server instance or generating an email, for specific types of errors.

There are several pre-defined events, providers, and rules inside the root web.config file in the same directory as machine.config (not to be confused with the web.config in your application). In the healthMonitoring section you will see several groupings for providers, rules, profiles, and event mappings. For right now, we’ll focus on providers, rules, and eventMappings. I’ll discuss how these play into logging events.

Providers (<providers>) essentially tell where the log information is going. For example, the provider named EventLogProvider maps to the EventLogWebEventProvider class. As you’d expect, this provides all those error events you see in your Event Viewer. Similarly, the SqlWebEventProvider allows you to log events to the built in ASP.NET database (ASPNETDB.mdf in your app_data folder or your own SQL Server instance configured using aspnet_regsql.exe). Web can define a custom provider in your application web.config file (or in the root if you want to apply it to all your sites) to, for example, generate and send an email to a recipient containing logged event information. The SimpleMailWebEventProvider does just this. We will define this provider and use this provider in our example below.

Rules (<rules>) dictate which event type will use a provider. For example, you can have application errors write to the SQL provider or generate an email using your the email provider mentioned above. The eventName attribute, when adding a rule, specifies the name of the web event to use. Basically, for each rule, you specify the eventName and the provider. Simple as that.

Event Mappings (<eventMappings>) are used for mapping event names to provider classes. In the root web.config, you will notice several pre-defined provider names associated with event classes (the type attribute). If you are creating your own web event class (we’ll cover this in a later post), you may create a name for it and give it a type (your new class). By default, you can leave these alone, as the web events shipped with the framework cover most cases.

There are several event mappings listed, but there are usually one or two which, I feel, would be used in most conditions. First, the All Errors event mapping (using WebBaseErrorEvent) is used for all compilation, runtime, and configuration errors. I have found this to be the most handy. The second is the Failure Audits event (using the WebFailureAuditEvent class), which raises a an application page is accessed using improper credentials. By default, ASP.NET uses the NETWORK SERVICE account to access the file system, the registry, any databases, etc. If incorrect permissions are set in the file system, or malicious code is attempting to access the file system, this event will be raised. One other potentially useful web event is the Infrastructure Error mapping. This web event is raised only for compilation and configuration errors. However, I personally find it less useful, as the All Errors web event covers this.

In this post, I will create a basic health monitoring event handler using the SimpleMailWebEventProvider class to generate an email whenever any runtime error is found on the site. The email, by default, will provide all the information I need to diagnose the error and make corrections. You may customize this email template, but that is another topic.

First, since we’re using an email provider, we need to configure SMTP settings in the web.config. The SimpleMailWebEventProvider class will use these settings for sending the email. Configure your SMTP settings as follows:

<configuration>
    ...
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="Network">
                <network host="SERVER" userName="USERNAME" password="PASSWORD" />
             </smtp>
        </mailSettings>
    </system.net>
    ...
</configuration>

Once that is entered, we can begin enabling the health monitoring features and chose our provider and rules. Again, the purpose is to create an email for all site errors and send them to my email address.

<system.web>
    ...
    <healthMonitoring enabled="true">
        <providers>
            <add name="EmailSiteErrors" type="System.Web.Management.SimpleMailWebEventProvider" to="ryan@marriedgeek.com" from="noreply@marriedgeek.com" buffer="false"/>
        </providers>
        <rules>
            <add name="Email Errors" eventName="All Errors" provider="EmailSiteErrors"/>
        </rules>
    </healthMonitoring>
    ...
</system.web>

That’s pretty much it. The ease of configuration and separation of providers and rules makes it a breeze to configure monitoring for your website. If you chose to use a SQL event provider, your logs will be written in the  provider database configured in your maching.config’s connection string. You may even have both or as many provider/rule sets as you like.

This is an overview of how to get started with using health monitoring in ASP.NET. To read more about health monitoring, check out the following links:

Create Custom Event Handlers & Arguments For Controls

July 28th, 2009

I was hoping to get this post up earlier, but got distracted by a couple of things. First, I jacked up my hand trying to separate my dogs from fighting to the point I couldn’t even move my ring finger (long story about the dogs, but I love ‘em). Second, I started a new job at Groove Commerce on Monday. So far, so good. I feel it’ll keep me on my game and give me some nice challenges ahead.

A few entries ago we talked about custom server controls and mentioned how to structure a composite control to allow the ViewState to “pick up” on it’s post back content so that data isn’t re-binded with every call. It was a very basic composite control, but it showed how to add native WebControl objects to the Controls tree to create a basic layout without using a .ASCX design file. This type of control can be added to your app_code directory or manually compiled as an assembly and placed into your bin directory. The advantage of the latter is to feasibly add it to your Visual Studio toolbar by loading the assembly. This time, we’ll add a custom event to our control using a delegate for an event, as well as a custom event argument class to pass along with it.

We’ll take the code from last time, but add several things which are highlighted in green.

  • A line to declare our event delegate variable and the delegate itself.
  • A class which will be used for our arguments.
  • A binded event for the button Click.
  • A line in the button Click handler to invoke the event delegate.
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace RyanControls
{
    public class MyCompositeControl : CompositeControl
    {
        public MyCompositeControl() { }

        // Declare the Updating event of delegate type CustomControlUpdateHandler
        public event CustomControlUpdateHandler Updating;

        protected override void CreateChildControls()
        {
            TextBox txtMyData = new TextBox();
            DropDownList drpMyList = new DropDownList();
            Button btnUpdate = new Button() { Text = "Update!" };

            Controls.Add(drpMyList);
            Controls.Add(txtMyData);
            Controls.Add(btnUpdate);

            if (!Page.IsPostBack)
            {
                drpMyList.Items.Add(new ListItem("item 1"));
                drpMyList.Items.Add(new ListItem("item 2"));
                drpMyList.Items.Add(new ListItem("item 3"));
            }

            // Add a handler for the button click to launch the Updating event
            btnUpdate.Click += new EventHandler(FireEvent);
        }

        // The method that handles the Click event from the button
        // and triggers the Updating event by calling the delegate
        protected void FireEvent(Object s, EventArgs e)
        {
            Updating(s, new CustomControlUpdateEventArgs() { SomeData = "The control was updated!" });
        }
    }

    // Our custom event class that inherits EventArgs. You may use
    // EventArgs as the class if you don't have additional data to pass
    public class CustomControlUpdateEventArgs : EventArgs
    {
        public CustomControlUpdateEventArgs() { }
        public String SomeData;
    }

    // The delegate declaration (or template, as I call it) for the handler
    public delegate void CustomControlUpdateHandler(Object s, CustomControlUpdateEventArgs e);
}

There you have it. If you place this code into a .CS file inside your app_code directory, you’ll be able to include the control on your pages. If you compiled it into an assembly, toss it into your bin directory and feel free to add it to your toolbar in VS or VWD.

<asp:MyCompositeControl runat="server" ID="cntMyControl" OnUpdating="DisplayUpdate" />

By assigning an event handler for the Updating event (the page automatically prepends the “On” for “OnUpdating”) to call a method to handle the update, you can use data contained in the CustomControlUpdateEventArgs object. In this case, there’s only a public string called SomeData.

protected void DisplayUpdate(Object s, RyanControls.CustomControlUpdateEventArgs e)
{
    Response.Write(String.Format("<div>From the event handler: {0}</div>", e.SomeData));
}

When you run the code and click the button, the method will be invoked and the message displayed. Similarly, you can bind the handler programmatically by adding cntMyControl.Updating += new RyanControls.CustomControlUpdateHandler(DisplayUpdate) in your code-behind.

custom_event

So again, while the code isn’t really useful, it shows how to create your own event handlers and custom argument class(es) for a composite server control. The point is to be able to create one or more event handlers, as well as combine those with one or more types of event argument classes. The fields of that particular argument class should be specific to that type of event and it is best not to use one custom event args class to satisfy many types of event argsuments, unless the data being passed into your hander functions generic enough to do so.

Happy coding!

Home Server How-to: Introduction

July 22nd, 2009

I’m going to begin talking a little about setting up your own home server and discuss not only some cool stuff you can do, but how to get the most use out of it. When I mention to people that I have a home server, they snicker. And that’s fine considering that the thought of having a server in your home usually connotates that you’re some hacker that orders pizza online and doesn’t leave the house for days. However, yes, having a some server in their house is kind of a geeky thing to do, but if used effectively, can be really beneficial to those actually using the network. All you need is a spare PC, some ethernet cable, and a spare port on your router.

You can do a lot with a home server. Some things may you may find more interesting depending what stuff you’re into, but here is a broad list:

  • Backups – This is by far the biggest benefit. If you’re not using automated backup software on your computers, you run the risk of a catostrophic failure (usually in the form of a dead or dying hard disk) and losing all your files. With a server, you run scheduled backups one or more times per day to grab those files off your PC or laptop and save them to your server.
  • Media storage – Have a lot of music, videos, or photos? Have a spouse or kids who want access to that content? Instead of sharing one computer, store all your media on a drive connected to the server and allow for anyone to access that content any time from their own computer.
  • FTP server – If you’re on the road a lot or just want access to your files, you can set up an FTP (file transfer protocol) server, allowing you to grab and/or save files to or from your server from anywhere you can steal an internet connection.
  • Remote desktop – If you need to connect to the desktop of your server while you’re away, you can connect to it and view any documents or backups.
  • Web server – While the first two are pretty common, running your own web server involes a bit of skill, including setup of your web application software (e.g., IIS, Apache), configuring your router and firewall, installing databases, etc. This can be a daunting task to someone new, but you can host your own websites, even your WordPress blog, from your server for free! With free DNS services out there, such as Afraid.org, as long as your ISP lets you hang onto your IP, all you have to pay for is the domain name.
  • Host your own email – This one can be tricky, especially when dealing with blacklists. With free email tools out there for Windows, such as hMailServer, you can setup POP or IMAP email for your domain. You can even run backups of your email.
  • Gaming server – Yes, you can have a wicked LAN party and have your server host all the games. Ask your wife about having the party first.
  • Streaming webcam – I used this idea when we got our new dog and I wanted to keep an eye on her while at work. By hooking a webcam, and using some streaming software, I was able to connect to and stream video from the camera.

The concept of having a home server is pretty simple. You have a computer running 24/7 on your home network. It’s connected to your router and can talk to other machines on your network. By “talking” to other computers on your network, it simply mean that it can view their folder contents as well as allow its contents to be viewed by other computers on your network.

For this intro, I just wanted to cover the basics and give an overview of what a home server can do for you. For next time, I’ll discuss how to pick a computer, connect it to your network, and set up shared folders.

Extending A Native Web Control

July 9th, 2009

Some may not know the difference but user controls are different from server controls. User controls inherit from the UserControl class while server controls can inherit from, say, the Button, DropDownList, GridView, or, well, any Control or WebControl object. Using a server control is faster than using a user control since there is no designer UI to be compiled.

The main drawback from creating a server control versus a user control is the lack of building a rich UI in most respects. Because there isn’t a .ascx page to load existing controls onto, you don’t have this ability as easily as you do with a server control (unless you programmatically add controls to a placeholder object in a composite control).

The main difference is that your control must inherit from an existing Control or WebControl object, or one that inherits from these, such as a Button.

Here’s a quick example of a DropDownList that is pre-populated with data essential to determining sex – “Male” and “Female”. You can save this as a .CS file into your app_code directory. In this example, we are essentially extending the existing DropDownList control to add additional functionality.

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;

namespace CustomControls
{
    public class GenderList : DropDownList
    {
        public GenderList()
        {
            System.Collections.Generic.Dictionary<String, String> dctGender = new System.Collections.Generic.Dictionary<String, String>();

            dctGender.Add("MALE", "Male");
            dctGender.Add("FEMALE", "Female");
            dctGender.Add("NA", "No response");

            base.DataSource = dctGender;
            base.DataTextField = "value";
            base.DataValueField = "key";
            base.DataBind();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}

Now, to use it on your page, you’ll need to either Register the control using the <%@ Register %> declaration or add a reference in the <pages><controls> … </controls></pages> section of your web.config.

<%@ Register TagPrefix="asp" Namespace="CustomControls" %>

…or in your web.config…

<pages>
    <controls>
        ....
        <add tagPrefix="asp" namespace="CustomControls"/>
    </controls>
</pages>

Notice we did not include the assembly. This is because it was not compiled into a .dll. If it is compiled into a .dll, you must include the Assembly name.

genderlist

After that, if you don’t feel like waiting for your intellisense to update, you can simply insert the control onto your page. The only difference is, you do not need to include the src attribute, since the control will be compiled (just the TagPrefix, and Namespace).

The class above is very simple. It inherits from DropDownList, so it has all the functionality of the native DropDownList class, but binds some data in the constructor. Pretty simple, but will save a lot of time when working with forms. If this seem a bit boring, another idea could be to have a StateList control that is populated with states from an XML file. Or, perhaps you find yourself always setting default attributes in your GridViews, such as GridLines=”none” or AutoGenerateColumns=”false”. Make your own and have these defaults set for you.

Add some jQuery animation to UpdatePanel postbacks

July 4th, 2009

Here’s a little eye-candy trick if you’re one of those using UpdatePanel  in ASP.NET (and not caring about performance, ahem) that will allow for a quick animation (e.g., fade, slide) on any data getting refreshed from the server. The method uses jQuery to handle the animation and a few lines of JavaScript to inject the animation into the “click” event of a button (or any other element!).

A neat use of this would be for, say, a GridView of data. You may have a series of filters for that data and instead of having that data (asynchronously) refresh with little notice from the user, you add a slideIn()/slideOut() animation so that the user can visually see exactly what is being refreshed.

The process involves 1) grabbing whatever the current onclick handler is for each element, 2) initializing the new “click” handlers for the buttons incorporating the new fadeOut or slideUp effect, and 3) binding your final effect for when the AJAX response is received.

The following code will invoke a function, InitBrowseButtons(), when the DOM is ready when the page is initially loaded (if your buttons invoking the “asynchronicity” are inside the update panel, you’ll need to do run ScriptManager.RegisterStartupScript(…) to re-bind the handlers). For this code, I bound a checkbox control that updates the data in the UpdatePanel when checked or unchecked. This can easily be substituted for a button or other element by changing the jQuery selector.

$(document).ready(function()
{
    InitBrowseButtons();
});

// round up the elements using jQuery selectors, grab the current onclick and insert your own
function InitBrowseButtons()
{
    $("input[type='checkbox'][name$='chkBrowseAll']").each(function()
    {
        var currAction = $(this).attr("onclick");

        $(this).attr("onclick", "").click(function()
        {
            $("div[id$='upMessages']").slideUp(500, function() { currAction(); });
        });
    });
}

This function will cycle through all the elements grabbed by the selector (only one in this case) and, for each, save the current onclick handler to a variable. It will then reassign a click event to that element, in thise case, a jQuery animation to slideUp an UpdatePanel named upMessages. On the callback event for the animation, it will then invoke the previous click handler (the parenthesis will invoke the variable as a function).

This function will handle the slideUp and the subsequent postback event, but what about when the data is posted back? The following function is invoked by static method ScriptManager.RegisterStartupScript(…) from the code-behind file. This will invoke a script each time a postback is complete.

The C# script (I called it from Page_Load, but adjust as necessary)…

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "InitFadeData", "InitFadeData();", true);

… will invoke the following JavaScript…

// function called when post back is completed
function InitFadeData()
{
    $("div[id$='upMessages']").slideDown(500);
}

This script grabs the same UpdatePanel (which is just a DIV) and slides the content open once it is repopulated with server-side data.

A demo can be found here. Source code here. All scripts were inline for the purposes of viewing.

And just to note about the performance of UpdatePanels; yes, they’re great for quick-and-dirty partial updates, but should mainly be used for low traffic sites or for administratitive sites (CMSes and other private web sites). Otherwise, use the built-in page methods or use jQuery’s AJAX methods and render your own UIs.