Paul said:
Can anyone tell me how to add several event categories to the Window
Application Event Log? I have successfully registered a log source
which is registered under the Application.Log. The events are
displayed perfectly but it would be nice if I could display the
events with my own event categories since none of the currently
defined categories match my application.
I wrote one of my newsletters for DDJ on this subject
(
http://www.ddj.com/columns/dotnet/), the article is due to be published in
a week or so's time.
Basically to display a string for the event category you need to do this:
1) create a resource only Win32 DLL with a RT_MESSAGETABLE resource that
describe the categories, create the resource with the MC message compiler
and the RC resource compiler
2) register your source to use the resource file for categories
The article will have example code to show you how to do all of this.
#2 can be be done with this function:
static void CreateSource(string name, string msgFile, int count)
{
if (!EventLog.SourceExists(name))
EventLog.CreateEventSource(name, "Application");
RegistryKey hklm = Registry.LocalMachine;
string keyName = @"SYSTEM\CurrentControlSet\Services"
+ @"\EventLog\Application\"
+ name;
RegistryPermission perm = new RegistryPermission(
RegistryPermissionAccess.Create|RegistryPermissionAccess.Write,
@"HKEY_LOCAL_MACHINE\"+keyName);
RegistryKey el = hklm.OpenSubKey(keyName, true);
string file = Environment.CurrentDirectory + @"\" + msgFile;
try
{
el.SetValue("CategoryMessageFile", file);
el.SetValue("CategoryCount", count);
}
catch(Exception){}
}
Richard