Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Windows event viewer - Part 72

Suggested Videos
Part 69 - Exception handling
Part 70 - Error events
Part 71 - Custom errors

Exceptions in an asp.net web application can be logged to the event viewer. First let us discuss about the event viewer and create custom event log and event source. To access the event viewer
1. Click on Start
2. Type "Run" and press enter
3. In the "Run" window type "eventvwr" and press enter
4. This should open event viewer



Under windows logs, you should see
1. Application - Logs information from applications like MS Office, SQL Server, Visual Studio etc.
2. Security - Logs information related to security like user sign-ons, access checks etc
3. System - Logs information related to driver, system service failures etc.

Let us now create a windows application that can be used to create a custom windows event log and event source. The Event Source is the name of the application that logged the event. The event source is displayed in the details pane, when an event is selected in the event viewer.



Steps to create the windows application to create the custom windows event log and event source.
1. Drag and drop two textboxes, two labels, and a button control on to the windows form
2. Arrange the controls, so the form looks as shown below.

Custom Event Logs in event viewer

3. Double click the button control to generate the event handler
4. Copy and paste the following code in the click event handler
private void CreateEventLogButton_Click(object sender, EventArgs e)
{
    if (EventLogNameTextBox.Text != string.Empty && EventLogSourceTextBox.Text != string.Empty)
    {
        System.Diagnostics.EventLog.CreateEventSource
            (EventLogSourceTextBox.Text, EventLogNameTextBox.Text);
        MessageBox.Show("Event Log and Source Created");
    }
    else
    {
        MessageBox.Show("Event Log and Source are required");
    }
}
5. Run the application
6. Enter the name and source for the event log.
7. Click "Create Event Log and Event Source Button" button

Open the event viewer. The newly created "event log" should be under Applications and Service Logs. If you are not able to locate them, restart your machine.

To delete the custom event log, use the Delete() method
System.Diagnostics.EventLog.Delete("EventLogNameToDelete")

6 comments:

  1. When I tried myself. I got an error message as the following. Can you please tell me what is wrong? Can you teach us how to handle this kind of exception? Thanks

    A first chance exception of type 'System.Security.SecurityException' occurred in System.dll

    Additional information: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.

    If there is a handler for this exception, the program may be safely continued.

    ReplyDelete
    Replies
    1. Try running the windows application as an administrator and see if that helps. Good Luck.

      Delete
  2. Dear sir, when i executed the code . i am getting a exception. can u help me.

    SecurityException was unhandled

    The source was not found, but some or all event logs could not be searched.
    To create the source, you need permission to read all event logs to make sure that the new source name is unique.
    Inaccessible logs: Security.

    ReplyDelete
  3. Hi Sir
    When I tried to create the event view then i get error. Can you please tell me what is wrong? Can you teach us how to handle this kind of exception? Thanks

    A first chance exception of type 'System.Security.SecurityException' occurred in System.dll

    Additional information: The source was not found, but some or all event logs could not be searched. To create the source, you need permission to read all event logs to make sure that the new source name is unique. Inaccessible logs: Security.

    If there is a handler for this exception, the program may be safely continued.

    ReplyDelete
  4. Hi Sir
    When I tried to create the event view then i get error. Can you please tell me what is wrong? Can you teach us how to handle this kind of exception? Thanks

    the system.Diagnostics.EventLog.CreateEventSource................
    give the following Error:
    security Exception was unhandled
    an unhandled exception of type ' system.security.securityException' occured in system.dll
    additional Information: The source was not found,but some or all event logs could not be searched.

    ReplyDelete
    Replies
    1. As Venkat suggested, run the application as an administrator it will work fine without errors, to do that you will need to build your solution then go to your C drive and find the .exe file for that application and right click and select run as administrator and put in the values. That will work fine without crashing. for example my path is C:\Users\JohnDoe\Documents\Visual Studio 2013\Projects\EventLog\EventLog\bin\Debug. I hope that helps

      Delete

It would be great if you can help share these free resources