Populating and Raising Events from Code

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The ability to populate and raise events from code is fundamental to the functionality of the Logging Application Block.

Typical Goals

In this scenario, you want to specify the data to be logged, along with a category and priority, so that the application block can log the events to the appropriate trace listener or listeners.

Solution

Configure the application to use the Logging Application Block. Use the Enterprise Library Configuration Console to configure categories and trace listeners to be used. Create the information that is submitted to the Logging Application Block in the LogEntry object. Call the Write method on the Logger class, passing the LogEntry object.

Note

The Logger class includes overloads that allow you to pass event information without constructing a LogEntry object. This scenario only demonstrates one overload provided by the Logger class. For information about the other overloads, see the Logging Application Block reference documentation.

QuickStart

For an extended example of how to use the Write method to raise events from code, see Walkthrough: Populating and Raising an Event from Code.

Creating and Writing a LogEntry

The following code shows how to create a LogEntry object and use the Write method**.** The LogEntry object has a priority of 2 and belongs to both the Trace and UI Events categories.

LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
logEntry.Categories.Add("Trace");
logEntry.Categories.Add("UI Events");

Logger.Write(logEntry);
'Usage
Dim logEntry As LogEntry = New LogEntry()
logEntry.EventId = 100
logEntry.Priority = 2
logEntry.Message = "Informational message"
logEntry.Categories.Add("Trace")
logEntry.Categories.Add("UI Events")

Logger.Write(logEntry)

Usage Notes

If the message cannot be written to the configured destination, the information is written to the listeners configured for the "Errors" special source.

If you have configured the "All Events" special source, the event information will be written to that source as well as the sources associated with the categories specified in the LogEntry object.

For applications configured to write to an event log trace listener, if the custom event log does not exist at the time your component writes a log entry, the Logging Application Block will attempt to create the event log. To successfully create the event log, the component using the Logging Application Block must be running with the appropriate privileges. Specifically, it must have full access rights to the registry key HKLM\System\CurrentControlSet\Service\EventLog. If applications using the Logging Application Block will not have the appropriate access rights, the custom event logs should be created by running installation programs under accounts with the necessary privileges.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.