Friday, October 14, 2011

Difference between ASP.NET HttpHandler and HttpModule

In this blog we will see what are http handlers and modules. And in which condition which one is suitable. For that first we need to understand when user request application resource to web server, what happened? The user requests for a resource on web server. The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension. For example when an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse are created. Then instance of HttpApplication is created and also instance of any configured modules. One can register different events of HttpApplication class like BeginRequest, AuthenticateRequest, AuthorizeRequest, ProcessRequest etc.
HTTP Handler
HTTP Handler is the process which runs in response to a HTTP request. So whenever user requests a file it is processed by the handler based on the extension. So, custom http handlers are created when you need to special handling based on the file name extension. Let’s consider an example to create RSS for a site. So, create a handler that generates RSS-formatted XML. Now bind the .rss extension to the custom handler.
HTTP Modules
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is passed through all the modules in the pipeline of the request. So generally http modules are used for:
Security: For authenticating a request before the request is handled.
Statistics and Logging: Since modules are called for every request they can be used for gathering statistics and for logging information.
Custom header:  Since response can be modified, one can add custom header information to the response.

Thursday, October 13, 2011

Logging Application Block

First we need to know what application blocks are?
Application Blocks are reusable software components and are part of Microsoft’s Enterprise Library. Enterprise library provides configurable features to manage crosscutting concerns like validation, logging, exception management etc. Here I elaborate on one of the feature of Enterprise Library- Logging Application Block. One can use the Logging Application Block to log information to:
1)      Event logs
2)      A text file
3)      An email message
4)      A database
5)      A message queue
6)      Windows Management Instrumentation (WMI) event
7)      Customize the logging location
In this article we will see how to configure the logging. One need to install the Enterprise Library.
Create a windows form application. Now add a reference of Enterprise Library Logging Application Block to the solution.

Now add a configuration file for configuring the log. Now edit it with Enterprise Library Configuration.


It opens a window. In that select Blocks -> Add Logging Settings


It creates a default logging settings as follows:


It contains a category named “General”, which is using an “Event Log Listener” with a “Text Formatter”. For now we can leave Special Categories. So, what all these are doing?

Let’s understand it with an example. There may be some logs which you need to write in event log and a text file, and for some log you need to mail also. So you create one category which writes all logs to event log and text file, and another category for sending mail of only error logs. Then you have to create 3 listeners, for logging in event log, text file and mailing. You can also define different formatters for different type of listeners. For example you need a detailed log for logging in events log and precise log for mailing. All these settings are defined in the configuration. All you need to do is when you write a log just defines the category of the log and rest is done by the application block.

Categories:
Category can filter the log entry and route it to different listeners. The filter can be based on the severity, i.e., Critical, Error, Warning or Information.

Logging Target Listeners:
Different listeners can be defined here. We only need to do configuration and these listeners can write to event logs, a flat file, xml file, to database, send mail etc.

Log Message formatters:
This is used to define a format in which log is written. Text formatter is the default formatter, which writes all the information. For customizing the formatter, define a template.
A sample project is uploaded. This contains a general category which logs to event logs and a flat file using text formatter. And mail category which sends mail using mail formatter. And depending on the conditions it logs to general category, or sends mail, or can do both.

So now you can see a configuration section “loggingConfiguration” in the app.config file.
This configuration section contains listeners, formatters, categorysources and specialSources. One can configure listener, formatter, categories here.

To write a log you just need to create a LogEntry and specify the category. Of category is not specified, log will be written to default category.
So, now you can see logging so easy using Application blocks.