How to write event log category

Xiaodan Mao picture Xiaodan Mao · May 6, 2012 · Viewed 12.3k times · Source

As we know, we can use the class EventLog to write event logs, but I am confused about how to write category name in my event log. Thoug it provides the category parameter, for example, one of the type of WriteEntry is:

public void WriteEntry(
string message,
EventLogEntryType type,
int eventID,
short category

)
And it just shows number in my log. Why the type of category is short, not string? How to display the category name in the Event Viewer? Thanks! By the way, we will not create the custom CategoryMessageFile.

Answer

Alois Kraus picture Alois Kraus · May 6, 2012

You can write an event log entry with a given category but you need to create an extra native dll with a String Resource table which is registered in the event log. This does complicate your deployment further. You currently have to do during installation

  1. To create new Event Log Sources you need to have admin rights every time you create a new source. It is therefore wise to collect all sources so you can install at once during the initial installation.
  2. Create a native dll which does contain a String Resource table for each category id you want to supply.
  3. Register the category dll in the registry to make Windows aware of it.

Now you can use the overload to write an event log message with a given category.

There is a very good Dr. Jobs Journal article describing exactly your problem.