Thursday, November 15, 2012

jQuery - Differences Between .bind() vs .live() vs .delegate() vs .on()


Introduction


Before we dive into the ins and outs of these methods, let's start with some common HTML markup that we'll be using as we write sample jQuery code.

<ul id="members" data-role="listview" data-filter="true">
    <!-- ... more list items ... -->
    <li>
        <a href="detail.html?id=10">
            <h3>John Resig</h3>
            <p><strong>jQuery Core Lead</strong></p>
            <p>Boston, United States</p>
        </a>
    </li>
    <!-- ... more list items ... -->
</ul>

Using the Bind Method


The .bind() method registers the type of event and an event handler directly to the DOM element in question. This method has been around the longest and in its day it was a nice abstraction around the various cross-browser issues that existed. This method is still very handy when wiring-up event handlers, but there are various performance concerns as are listed below.


/* The .bind() method attaches the event handler directly to the DOM
element in question ( "#members li a" ). The .click() method is
just a shorthand way to write the .bind() method. */
$( "#members li a" ).bind( "click", function( e ) {} );
$( "#members li a" ).click( function( e ) {} );

The .bind() method will attach the event handler to all of the anchors that are matched! That is not good. Not only is that expensive to implicitly iterate over all of those items to attach an event handler, but it is also wasteful since it is the same event handler over and over again.

Pros
  • This methods works across various browser implementations.
  • It is pretty easy and quick to wire-up event handlers.
  • The shorthand methods (.click().hover(), etc...) make it even easier to wire-up event handlers.
  • For a simple ID selector, using .bind() not only wires-up quickly, but also when the event fires the event handler is invoked almost immediately.

Cons
  • The method attaches the same event handler to every matched element in the selection.
  • It doesn't work for elements added dynamically that matches the same selector.
  • There are performance concerns when dealing with a large selection.
  • The attachment is done upfront which can have performance issues on page load.

Using the Live Method


The .live() method uses the concept of event delegation to perform its so called "magic". The way you call .live() looks just like how you might call .bind(), which is very convenient. However, under the covers this method works much different. The .live method attaches the event handler to the root level document along with the associated selector and event information. By registering this information on the document it allows one event handler to be used for all events that have bubbled (a.k.a. delegated, propagated) up to it. Once an event has bubbled up to the document jQuery looks at the selector/event metadata to determine which handler it should invoke, if any. This extra work has some impact on performance at the point of user interaction, but the initial register process is fairly speedy. 

/* The .live() method attaches the event handler to the root level
document along with the associated selector and event information
( "#members li a" & "click" ) */
$( "#members li a" ).live( "click", function( e ) {} );
The good thing about this code as compared to the .bind() example above is that it is only attaching the event handler once to the document instead of multiple times. This not only is faster, but less wasteful, however, there are many problems with using this method and they are outlined below.

Pros
  • There is only one event handler registered instead of the numerous event handlers that could have been registered with the .bind() method.
  • The upgrade path from .bind() to .live() is very small. All you have to do is replace "bind" to "live".
  • Elements dynamically added to the DOM that match the selector magically work because the real information was registered on the document.
  • You can wire-up event handlers before the document ready event helping you utilize possibly unused time.

Cons
  • This method is deprecated as of jQuery 1.7 and you should start phasing out its use in your code.
  • Chaining is not properly supported using this method.
  • The selection that is made is basically thrown away since it is only used to register the event handler on the document.
  • Using event.stopPropagation() is no longer helpful because the event has already delegated all the way up to the document.
  • Since all selector/event information is attached to the document once an event does occur jQuery has match through its large metadata store using the matchesSelectormethod to determine which event handler to invoke, if any.
  • Your events always delegate all the way up to the document. This can affect performance if your DOM is deep.

Using the Delegate Method


The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored. Just like the .live() method, this technique uses event delegation to work correctly. 

/* The .delegate() method behaves in a similar fashion to the .live() 
method, but instead of attaching the event handler to the document,
you can choose where it is anchored ( "#members" ). The selector
and event information ( "li a" & "click" ) will be attached to the
"#members" element. */
$( "#members" ).delegate( "li a", "click", function( e ) {} );
The .delegate() method is very powerful. The above code will attach the event handler to the unordered list ("#members") along with the selector/event information. This is much more efficient than the .live() method that always attaches the information to the document. In addition a lot of other problematic issues were resolved by introducing the .delegate() method. See the following outline for a detailed list.

Pros
  • You have the option of choosing where to attach the selector/event information.
  • The selection isn't actually performed up front, but is only used to register onto the root element.
  • Chaining is supported correctly.
  • jQuery still needs to iterate over the selector/event data to determine a match, but since you can choose where the root is the amount of data to sort through can be much smaller.
  • Since this technique uses event delegation, it can work with dynamically added elements to the DOM where the selectors match.
  • As long as you delegate against the document you can also wire-up event handlers before the document ready event.

Cons
  • Changing from a .bind() to a .delegate() method isn't as straight forward.
  • There is still the concern of jQuery having to figure out, using the matchesSelectormethod, which event handler to invoke based on the selector/event information stored at the root element. However, the metadata stored at the root element should be considerably smaller compared to using the .live() method.

Using the On Method


Did you know that the jQuery .bind().live(), and .delegate() methods are just one line pass throughs to the new jQuery 1.7 .on() method? The same is true of the .unbind(),.die(), and .undelegate() methods. The following code snippet is taken from the jQuery 1.7.1 codebase in GitHub...

// ... more code ...
bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},
unbind: function( types, fn ) {
    return this.off( types, null, fn );
},
live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
},
die: function( types, fn ) {
    jQuery( this.context ).off( types, this.selector || "**", fn );
    return this;
},
delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
},
undelegate: function( selector, types, fn ) {
    return arguments.length == 1 ?
        this.off( selector, "**" ) :
        this.off( types, selector, fn );
},
// ... more code ...
With that in mind, the usage of the new .on() method looks something like the following...

/* The jQuery .bind(), .live(), and .delegate() methods are just one
line pass throughs to the new jQuery 1.7 .on() method */
// Bind
$( "#members li a" ).on( "click", function( e ) {} );
$( "#members li a" ).bind( "click", function( e ) {} );
// Live
$( document ).on( "click", "#members li a", function( e ) {} );
$( "#members li a" ).live( "click", function( e ) {} );
// Delegate
$( "#members" ).on( "click", "li a", function( e ) {} );
$( "#members" ).delegate( "li a", "click", function( e ) {} );
You'll notice that depending how I call the .on() method changes how it performs. You can consider the .on() method as being "overloaded" with different signatures, which in turn changes how the event binding is wired-up. The .on method bring a lot of consistency to the API and hopefully makes things slightly less confusing.

Pros
  • Brings uniformity to the various event binding methods.
  • Simplifies the jQuery code base and removes one level of redirection since the.bind().live(), and .delegate() call this method under the covers.
  • Still provides all the goodness of the .delegate() method, while still providing support for the .bind() method if you need it.

Cons
  • Brings confusion because the behavior changes based on how you call the method.

Conclusion (tl;dr)


If you have been confused about the various different types of event binding methods then don't worry, there has been a lot of history and evolvement in the API over time. There are many people that view these methods as magic, but once you uncover some of how they work it will help you understand how to better ode inside of your projects. 

The biggest take aways from this article are that...
  • Using the .bind() method is very costly as it attaches the same event handler to every item matched in your selector.
  • You should stop using the .live() method as it is deprecated and has a lot of problems with it.
  • The .delegate() method gives a lot of "bang for your buck" when dealing with performance and reacting to dynamically added elements.
  • That the new .on() method is mostly syntax sugar that can mimic .bind(),.live(), or .delegate() depending on how you call it. 
  • The new direction is to use the new .on method. Get familiar with the syntax and start using it on all your jQuery 1.7+ projects.

Friday, November 2, 2012

How to stop prompting Windows Service - Set Service Login


if the definition of you process installer is:

private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;


Add this code to your private void InitializeComponent() method in projectInstaller.Designer.cs file in your windows service project.


 this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

Wednesday, October 31, 2012

SQL Server Authentication enabling using Microsoft SQL Server 2008 Management Studio

Microsoft SQL Server Management Studio provides integrated environment for accessing, configuring, managing, administering and developing all components of SQL Server. There you can see two authentication modes called "Windows Authentication" and "SQL Server Authentication". The Windows authentication mode only allows you to login/connect to SQL Server with Windows authentication.


If you install the Microsoft SQL Server using Windows Authentication mode, the "sa" account is disabled by default. So if you plan to use SQL Server Authentication, you have to enable the "sa" account. This tutorial tells you how to enable the "sa" account and there you must remember that the SQL Server service needs to restart to make this change effective

1. First, Login to the SQL Server Management Studio using Windows Authentication. Right-click on the database instance, and go to Properties.


2. Then on Properties page, click on Security and select SQL Server and Windows Authentication mode, and click on OK to close the Server Properties page.


3. Now you will get dialog box saying that you should restart your SQL Server to take the changes take effect. Is is not done yet, you have to done one more thing to enable the "sa" login.

4. Now expand Security folder and go to Logins. You can see the "sa" account is disabled when you install SQL Server using Windows Authentication mode.


5. Then right-click on the "sa" account and go to Login Properties. There you can set a password for the "sa" account.



6. Click on the Status page. There you can see the "sa" account is disabled by default. Click on the Enabled button to enable it. Then click on Ok to close the "sa" Login Properties.


Now "sa" account is enabled and you can login to the SQL instance using the "sa" account after restarting the SQL Server.

Allow IIS to access Local SQL Server database


You can change the ApplicationPoolIdentity from IIS7 -> Application Pools -> Advanced Settings.
AdvancedSettings
Under ApplicationPoolIdentity you will find local system. This will make your application run under NT AUTHORITY\SYSTEM, which is an existing login for the database by default.

Tuesday, October 30, 2012

Introducing IIS Express


Developers today build and test ASP.NET sites and applications using one of two web-servers:
  • The ASP.NET Development Server that comes built-into Visual Studio
  • The IIS Web Server that comes built-into Windows
Both of the above options have their pros and cons, and many ASP.NET developers have told us: “I wish I could have the ease of use of the ASP.NET Development Server, but still have all the power and features of IIS”.  Today I’m happy to announce a new, free option that we are enabling – IIS Express - that combines the best characteristics of both, and which will make it easier to build and run ASP.NET sites and applications.
IIS Express will work with VS 2010 and Visual Web Developer 2010 Express, will run on Windows XP and higher systems, does not require an administrator account, and does not require any code changes to use.  You will be able to take advantage of it with all types of ASP.NET applications, and it enables you to develop using a full IIS 7.x feature-set.

How Things Work Today

Before I get into the details of IIS Express, let’s first quickly review how the ASP.NET Development Server and IIS options work today.
ASP.NET Development Server
Visual Studio’s built-in ASP.NET Development Server (also known as “Cassini”) has the benefit of being light-weight and easy to quickly run.  It doesn’t listen on remote ports (which makes it easier to get approved for many corporate security environments), works even when you are running under a non-administrator account, and doesn’t require a separate installation step. 
The fact that it is so easy to get running is a huge positive of it – and the reason it is the default web-server used by ASP.NET projects in Visual Studio when you press F5 to run them:
image
The downside with the ASP.NET Developer Server, though, is that it does not support a full set of web-server features.  For example, it doesn’t support SSL, URL Rewriting Rules (like the SEO URL Rewrite Rules I blogged about here), Custom Security Settings, and other richer features now offered with IIS 7.
IIS Web Server
IIS is the other option developers use when running and testing their applications with Visual Studio.  You can configure a web project within Visual Studio to use IIS by right-clicking on the project and pulling up its properties (and then by clicking on the “Web” tab within the properties window)":
image
Using IIS as your development server allows you to take full advantage of all web-server features (SSL, URL Rewrite Rules, etc).  IIS is a full-fledged web-server – which means you’ll get an experience closer to what it will work like when you deploy the application on a production server.
The downside with using the IIS option today, though, is that some companies don’t allow full web-servers to be installed on developer machines. IIS also requires administrator account access to setup and debug projects.  Different versions of Windows also support different versions of IIS.  For example, if you are running on Windows XP you have to use the IIS 5.1 web-server that comes with it – which doesn’t support all the new features of IIS 7.x.  Configuring a web project within VS to use IIS also requires some extra installation and configuration steps.

IIS Express – The Best of Both Options

We have been working on a new flavor of IIS 7.x that is optimized for developer scenarios that we are calling “IIS Express”. We think it combines the ease of use of the ASP.NET Web Server with the full power of IIS.  Specifically:
  • It’s lightweight and easy to install (less than 10Mb download and a super quick install)
  • It does not require an administrator account to run/debug applications from Visual Studio
  • It enables a full web-server feature set – including SSL, URL Rewrite, Media Support, and all other IIS 7.x modules
  • It supports and enables the same extensibility model and web.config file settings that IIS 7.x support
  • It can be installed side-by-side with the full IIS web server as well as the ASP.NET Development Server (they do not conflict at all)
  • It works on Windows XP and higher operating systems – giving you a full IIS 7.x developer feature-set on all OS platforms
IIS Express (like the ASP.NET Development Server) can be quickly launched to run a site from a directory on disk.  It does not require any registration/configuration steps. This makes it really easy to launch and run for development scenarios.
VS 2010 Integration
We are enabling IIS Express so that it can be easily used with Visual Studio 2010. You’ll be able to configure VS 2010 to use it instead of the ASP.NET Web Server as the default web-server on ASP.NET Projects.  Like the ASP.NET Development Server today, you won’t need to register a site or virtual directory to use IIS Express. It will support the same usage-model as the ASP.NET Development Server today – just with more feature support.
When you press F5 to run an ASP.NET project, Visual Studio can automatically launch IIS Express and use it to run/debug the application (no extra configuration required).  Like the ASP.NET Web Server, IIS Express will show up in your task-bar tray when running:
image
You can right-click and click “exit” on the icon above to quickly shutdown IIS Express.  You can also right-click and pull up a list of all sites running with it, as well as the directory location and .NET versions they are running under:
image
Two cool things to notice above:
1) The “Test Site” we are running, as well as IIS Express itself, live under the c:\users\[username] folder on disk. This enables non-administrator usage of IIS Express and sites – and enables a bunch of scenarios not possible with the full IIS today (including the ability to run IIS Express in both a locked-down enterprise environment as well as a locked-down school shared computer environment).
2) The “Test Site” we are running above using IIS Express supports both HTTP and HTTPS access.  IIS Express automatically installs a “self-signed certificate” and enables URL ACLs and SSL Certificates for ports so that developers (running as non-administrators on a machine) can use SSL without needing to elevate their accounts or setup any additional configuration.  This enables you to configure secure pages within your applications (like Logon forms) for SSL and run/test them at development time just like they’ll work on your real web-server.
IIS 7.x Feature Set
IIS Express is as easy to run and use as the ASP.NET Web Server you are familiar with today.  But because IIS Express is based on the IIS 7x codebase, you have a full web-server feature-set that you can use.  This means you can build and run your applications just they’ll work on a real production web-server.  In addition to scenarios like SSL, you can take advantage of the IIS 7.x URL Rewriter module, Media Extensions, Dynamic Compression, Advanced Logging, Custom Security and other rich modules now available.
In addition to supporting ASP.NET, IIS Express also supports Classic ASP and other file-types and extensions supported by IIS – which also makes it ideal for sites that combine a variety of different technologies.

Summary

We think IIS Express makes it even easier to build, run and test web applications.  It works with all versions of ASP.NET and supports all ASP.NET application types (including obviously ASP.NET Web Forms and ASP.NET MVC applications).  Best of all – you do not need to change any code to take advantage of it.  You’ll be able to optionally use it with all your current projects today.
We’ll be releasing the first public beta of IIS Express shortly. With the beta you’ll be able to right-click on a file-system folder and have IIS Express launch a web-site based on that file-system location. We’ll also be releasing a patch for VS 2010 and Visual Web Developer 2010 Express later this year that will enable you to automatically launch and use IIS Express in place of VS’s built-in ASP.NET Developer Server.  Future versions of Visual Studio will then ship with this functionality built-in.

Re-Register ASP.Net with IIS


Background

Various error conditions during installation or running the VersionOne application may be caused by issues with the ASP.Net setup within IIS. These issues can often be solved by re-registering ASP.Net with IIS to ensure the correct files are recognized by the web server. Use the Procedure below to run the IIS Registration tool.

Process

ASP.NET can be re-registered with IIS. The specific method depends on the operating system being used. For more information on ASP.Net and the IIS Registration tool, see the related links below.
Windows 2000/XP
To fix this on Windows 2000 or Windows XP, run this command from the Start>Run box or a command prompt (assuming the system directory is C:\Windows):
.Net 1.1: C:\Windows\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i
.Net 2.0: C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i
Windows 2003
On Windows 2003, ASP.NET must be both installed and enabled.  To install ASP.NET:
  1. On the taskbar, click the Start button, point to Control Panel, and then click Add or Remove Programs.
  2. In the Add or Remove Programs dialog box, click Add/Remove Windows Components.
  3. In the Components box in the Windows Components Wizard, click the Web Application Server check box, and then click Next.
  4. When the Windows Components Wizard has finished configuring Windows Server 2003, click Finish.
To enable ASP.NET, run this command from the Start>Run box or a command prompt (assuming the system directory is C:\Windows):
.Net 1.1 (32-bit): C:\Windows\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i -enable
.Net 2.0 (32-bit): C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i -enable
.Net 2.0 (64-bit): C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_regiis.exe -i -enable

How to write to an event log by using Visual C#

This step-by-step article shows you how to add your own entries to the operating system's event log by using the Microsoft .NET Framework.

Requirements

The following list describes the recommended hardware, software, network infrastructure, and service packs that you will need:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
  • Microsoft Visual Studio C#

Write to an event log

Event logging provides a standard, centralized way for your applications to record important software and hardware events. Windows supplies a standard user interface for viewing the logs, the Event Viewer. By using the common language's run-timeEventLog component, you can connect to existing event logs easily, on both local and remote computers, and write entries to these logs. You can also read entries from existing logs and create your own custom event logs. In its simplest form, writing to an event log involves only a few steps to create a sample application. To do this, follow these steps:
  1. Open Visual Studio C#.
  2. Create a new Console application in Visual C#. The Console application creates a public class and an empty Mainmethod for you.
  3. Verify that the project references at least the System.dll file.
  4. Use the using directive on the System and System.Diagnostics namespaces so that you do not have to qualify declarations from these namespaces later in your code. You must use these statements before any other declarations.

    using System;
    using System.Diagnostics;
         
  5. To write to an event log, you must have several pieces of information: Your message, the name of the log you to which you want to write (which will be created if it does not already exist), and a string that represents the source of the event. You can register a particular source with only a single event log; if you want to write messages to more than one log, you must define multiple sources.

    string sSource;
    string sLog;
    string sEvent;
    
    sSource = "dotNET Sample App";
    sLog = "Application";
    sEvent = "Sample Event";
         
  6. Use two static methods of the EventLog class to check whether your source exists, and then, if the source does not exist, to create this source that is associated with a particular event log. If the log name that you specify does not exist, the name is created automatically when you write your first entry to the log. By default, if you do not supply a log name to the CreateEventSource method, the log file is named "Application Log."

    if (!EventLog.SourceExists(sSource))
     EventLog.CreateEventSource(sSource,sLog);
         
  7. To write a message to an event log, you can use the EventLog.WriteEntry static method. This method has several different overloaded versions. The following sample code shows the simplest method, which takes a source string and your message, and one of the more complex methods, which supports specifying the event ID and event type:

    EventLog.WriteEntry(sSource,sEvent);
    EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning,  234);
         
  8. Save your application. Run your application, and then check the Application log in the Event Viewer to see your new events.

Complete code listing


using System;
using System.Diagnostics;

namespace WriteToAnEventLog_csharp
{
 /// Summary description for Class1.
 class Class1
 {
  static void Main(string[] args)
  {
   string sSource;
   string sLog;
   string sEvent;

   sSource = "dotNET Sample App";
   sLog = "Application";
   sEvent = "Sample Event";

   if (!EventLog.SourceExists(sSource))
    EventLog.CreateEventSource(sSource,sLog);

   EventLog.WriteEntry(sSource,sEvent);
   EventLog.WriteEntry(sSource, sEvent,
    EventLogEntryType.Warning, 234);
  }
 }
}
    

How to view and manage event logs in Event Viewer in Windows XP

Event Viewer

In Windows XP, an event is any significant occurrence in the system or in a program that requires users to be notified, or an entry added to a log. The Event Log Service records application, security, and system events in Event Viewer. With the event logs in Event Viewer, you can obtain information about your hardware, software, and system components, and monitor security events on a local or remote computer. Event logs can help you identify and diagnose the source of current system problems, or help you predict potential system problems.

Event Log Types

A Windows XP-based computer records events in the following three logs:
  • Application log

    The application log contains events logged by programs. For example, a database program may record a file error in the application log. Events that are written to the application log are determined by the developers of the software program.
  • Security log

    The security log records events such as valid and invalid logon attempts, as well as events related to resource use, such as the creating, opening, or deleting of files. For example, when logon auditing is enabled, an event is recorded in the security log each time a user attempts to log on to the computer. You must be logged on as Administrator or as a member of the Administrators group in order to turn on, use, and specify which events are recorded in the security log.
  • System log

    The system log contains events logged by Windows XP system components. For example, if a driver fails to load during startup, an event is recorded in the system log. Windows XP predetermines the events that are logged by system components.

How to View Event Logs

To open Event Viewer, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, click Event Viewer.

    The Application, Security, and System logs are displayed in the Event Viewer window.

How to View Event Details

To view the details of an event, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, expand Event Viewer, and then click the log that contains the event that you want to view.
  3. In the details pane, double-click the event that you want to view.

    The Event Properties dialog box containing header information and a description of the event is displayed.

    To copy the details of the event, click the Copy button, then open a new document in the program in which you want to paste the event (for example, Microsoft Word), and then click Paste on the Edit menu.

    To view the description of the previous or next event, click the UP ARROW or DOWN ARROW.

How to Interpret an Event

Each log entry is classified by type, and contains header information, and a description of the event.

Event Header

The event header contains the following information about the event:
  • Date

    The date the event occurred.
  • Time

    The time the event occurred.
  • User

    The user name of the user that was logged on when the event occurred.
  • Computer

    The name of the computer where the event occurred.
  • Event ID

    An event number that identifies the event type. The Event ID can be used by product support representatives to help understand what occurred in the system.
  • Source

    The source of the event. This can be the name of a program, a system component, or an individual component of a large program.
  • Type

    The type of event. This can be one of the following five types: Error, Warning, Information, Success Audit, or Failure Audit.
  • Category

    A classification of the event by the event source. This is primarily used in the security log.

Event Types

The description of each event that is logged depends on the type of event. Each event in a log can be classified into one of the following types:
  • Information

    An event that describes the successful operation of a task, such as an application, driver, or service. For example, an Information event is logged when a network driver loads successfully.
  • Warning

    An event that is not necessarily significant, however, may indicate the possible occurrence of a future problem. For example, a Warning message is logged when disk space starts to run low.
  • Error

    An event that describes a significant problem, such as the failure of a critical task. Error events may involve data loss or loss of functionality. For example, an Error event is logged if a service fails to load during startup.
  • Success Audit (Security log)

    An event that describes the successful completion of an audited security event. For example, a Success Audit event is logged when a user logs on to the computer.
  • Failure Audit (Security log)

    An event that describes an audited security event that did not complete successfully. For example, a Failure Audit may be logged when a user cannot access a network drive.

How to Find Events in a Log

The default view of event logs is to list all its entries. If you want to find a specific event, or view a subset of events, you can either search the log, or you can apply a filter to the log data.

How to Search for a Specific Log Event

To search for a specific log event, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, expand Event Viewer, and then click the log that contains the event that you want to view.
  3. On the View menu, click Find.
  4. Specify the options for the event that you want to view in the Find dialog box, and then click Find Next.
The event that matches your search criteria is highlighted in the details pane. Click Find Next to locate the next occurrence of an event as defined by your search criteria.

How to Filter Log Events

To filter log events, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, expand Event Viewer, and then click the log that contains the event that you want to view.
  3. On the View menu, click Filter.
  4. Click the Filter tab (if it is not already selected).
  5. Specify the filter options that you want, and then click OK.
Only events that match your filter criteria are displayed in the details pane.

To return the view to display all log entries, click Filter on the View menu, and then click Restore Defaults.

How to Manage Log Contents

By default, the initial maximum of size of a log is set to 512 KB, and when this size is reached, new events overwrite older events as needed. Depending on your requirements, you can change these settings, or clear a log of its contents.

How to Set Log Size and Overwrite Options

To specify log size and overwrite options, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, expand Event Viewer, and then right-click the log in which you want to set size and overwrite options.
  3. Under Log size, type the size that you want in the Maximum log size box.
  4. Under When maximum log size is reached, click the overwrite option that you want.
  5. If you want to clear the log contents, click Clear Log.
  6. Click OK.

How to Archive a Log

If you want to save your log data, you can archive event logs in any of the following formats:
  • Log-file format (.evt)
  • Text-file format (.txt)
  • Comma-delimited text-file format (.csv)
To archive a log, follow these steps:
  1. Click Start, and then click Control Panel. Click Performance and Maintenance, then click Administrative Tools, and then double-click Computer Management. Or, open the MMC containing the Event Viewer snap-in.
  2. In the console tree, expand Event Viewer, and then right-click the log in which you want to archive, and then click Save Log File As.
  3. Specify a file name and location where you want to save the file. In the Save as type box, click the format that you want, and then click Save.
The log file is saved in the format that you specified.