Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.33/5 (4 votes)
See more:
I have an application in production that has intermittent errors. I would like to add an error tracking feature so that, when an error gets channeled through the global handling routines, an entry gets written to the Event Log. I would like for this entry to use an event source unique to the application, to better track the errors.

So following Microsoft's own documentation, I tried this:

VB
If Not EventLog.SourceExists("MyApp") Then<br />            EventLog.CreateEventSource("MyApp", "Application")<br />        End If<br />        MyLog.Source = "MyApp"<br />        MyLog.WriteEntry(DetailedMessage)<br />
Unfortunately, EventLog.SourceExists throws an exception: the Framework attempts to check ALL of the logs, not just "Application", and Vista's psychotic paranoia does not allow the Framework to check the System log. So I tried always creating the source and ignoring the "source already exists" exception, like this:

VB
Try<br />            EventLog.CreateEventSource("MyApp", "Application")<br />        Catch ex As Exception<br />            'Do nothing<br />        End Try<br />        MyLog.Source = "MyApp"<br />        MyLog.WriteEntry(TextBox1.Text.Trim)<br />
That throws the same exception about the System log, this time on EventLog.CreateEventSource. Attempting to write the entry therefore causes a "source does not exist" exception.

The solutions I have been able to find involve creating the event source manually, by writing the key to the registry. The key gets put in HKEY_LOCAL_MACHINE/System. Vista's "virtualization" has a firm lock on that node and redirects all programatic reads and writes to an isolated, user specific zone. This apparently rules out having the application create the source, as three different users will create three different logs, none of which will be visible to me when I log in as an administrator. And because this app runs on about 40 different machines, it is not feasable to go around to every one and configure the registry manually.

At this point, I see two options:

1. Roll my own event log. Write the errors out to an XML file and write a simple viewer to present the information.

2. Roll every workstation in the office back to XP because I am fed up with Vista.

Please, anyone, is it possible to use the event log under Vista? If so, HOW?
Posted

1 solution

Just like writing to the HKLM section of the registry, creating an
event source requires elevated privileges. And also just like writing to
HKLM in the registry, apps that runs without elevation need to do these operations
at install time (which is why many installers give you a UAC prompt).

From the EventLog class docs:
"If you write to an event log, you must specify or create an event Source.
You must have administrative rights on the computer to create a new event source.
"


TechBearSeattle wrote:
Please, anyone, is it possible to use the event log under Vista? If so, HOW?


Yes. Provide some way to create the event source. Once the source is
registered, no elevation will be required for the app to write to the
event log.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900