Category Archives: Debugging

Reading The Windows Event Log

IMG_3365

I had a need recently to access the Windows event logs. Reading the Event Log is useful when you want to be notified when a program fails. You read the Events and if there is a fatal error, do something like send an email the owner of the process. This is helpful if you have a console application that runs all the time unattended.

The .Net Framework offers the System.Diagnostics.EventLog class. This class gives you the power to interact with the Windows event logs.

 

Below is a C# program that accesses the Windows event logs.

using System;
using System.Text;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        StringBuilder sb = new StringBuilder();
        EventLog log = new
        EventLog("Application");

        DateTime dt = DateTime.Now.AddHours(-1.00);
        Console.WriteLine(dt.ToString());

        foreach (EventLogEntry entry in log.Entries)
        {
            if (entry.TimeGenerated > dt)
            {
                Console.WriteLine(entry.Source);
                Console.WriteLine(entry.EntryType);
                Console.WriteLine(entry.Message);
                Console.WriteLine("--------");
            }
        }

        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

 

Below is the result of executing the code above. As you can see I had a crash in my Windows Live Writer Application.

2010-12-19_073757

 

Below is the Event Viewer and you can see there as well where there was an issue with the WLW application.

2010-12-19_074318

 

I can see where this class can be very helpful and plan on doing more research into what it can do as far as reading as well as writing operations.

 

Related Information

Debugging: Failed to Enable Constraints

I have a DataSet that has had a new field added to it recently. When my console application runs, it was throwing a “Failed to enable constraints” exception.

image

I needed to find out which column in the DataSet was causing the error, so a co-worker suggested I create a temporary data adapter/set, so I can examine the GetErrors() method.

/*
*/
//-- Table Adaptor and DataSet with issue

spRoxStaffImportTableAdapter roxStaffImportTA = 
    new spRoxStaffImportTableAdapter();

RoxStaffImport.Data.StaffImport.spRoxStaffImportDataTable importdt = 
    roxStaffImportTA.spRoxStaffImport();
            
            
//-- Table Adaptor and DataSet with my temporary TA and DS

spRoxStaffImportTableAdapter roxStaffImportTA = 
    new spRoxStaffImportTableAdapter();

//added for debugging
RoxStaffImport.Data.StaffImport myDS = 
    new RoxStaffImport.Data.StaffImport(); 

//added for debugging
roxStaffImportTA.spRoxStaffImportfl(myDS.spRoxStaffImport);

//changed for debugging
RoxStaffImport.Data.StaffImport.spRoxStaffImportDataTable importdt = 
    myDS.spRoxStaffImport; //roxStaffImportTA.spRoxStaffImport();

C# Listing on GitHub

Once the exception occurs, access the GetErrors() property using the Watch window. This will tell you exactly what your issue is.

image

Once the issue is fixed, you can remove the temporary data adapter/set.

Debugging in .NET – Resources

The main topic of our “brown bag” this week was Debugging in .NET.  There is always a need for pointers on good debugging techniques, so I thought I would post some debugging resources here.

Information Resources

Video Tutorials

Happy Learning !!!

Follow

Get every new post delivered to your Inbox.