EventLog with custom Log

  • Thread starter Thread starter Bill Ward
  • Start date Start date
B

Bill Ward

I have a suite of applications and services that use a common library and I
would like to use a custom event log to gather all the events in one place
rather than putting events in the "Application" event log. The code I use to
create the event log object is below. Clearly I'm missing some steps because
the results are not correct. A new event log called EJobs is created but is
never populated with events. All event log mesages still go into the
Application event log but are not displayed correctly. In the event viewer a
typical entry looks like this...

The description for Event ID ( 0 ) in Source ( MyNewService ) cannot be
found. The local computer may not have the necessary registry information or
message DLL files to display messages from a remote computer. You may be
able to use the /AUXSOURCE= flag to retrieve this description; see Help and
Support for details. The following information is part of the event:
Registering service types.

If I create the EventLog object to write into the Application log, then
everything works properly and you only see the intended text rather than the
text about needing to use AUXSOURCE as well.

Any suggestions would be gratefully received.

Thanks

Bill


public abstract class EJobEventLog
{
private const string EventLogName = @"EJobs";
// private const string EventLogName = @"Application";

/// <summary>
/// Create an event log object.
/// </summary>
/// <param name="strEventSourceName">The name to use for the source of
the new event log.
/// Typically this is the name of the calling application or
service.</param>
/// <returns>New <see cref="EventLog"/> object</returns>
public static EventLog CreateEJobEventLog(string strEventSourceName)
{
EventLog.DeleteEventSource(strEventSourceName, @".");
if (!EventLog.SourceExists(strEventSourceName, @"."))
{
EventSourceCreationData oSource =
new EventSourceCreationData(strEventSourceName,
EventLogName);
EventLog.CreateEventSource(oSource);
}
return new EventLog(EventLogName, @".", strEventSourceName);
}
}
 
Event logs are written by default to the Application log if the
EventLog.Log property is not explicitly set to another event log.

after creating the new event log, try the following when writing to the
event log

EventLog.Log = EventLogName //or "EJobs"
EventLog.WriteEntry ("MyAppSource", "Test Log Entry");

That should register MyAppSource as an event source to the EJobs log
and write a log the EJobs event log.

Hope this helps...

NuTcAsE
 
Solved the problem (sort of).

Thanks Kevin for pointing me to that bit of code. It confirmed that I was
already doing enough to achieve my purpose so I went looking further.
I had already registered the sources under a different log name
(Application). It seems to me that the logging system does not like sources
changing logs for some reason. After testing with minimal applications and
my services I was getting all sorts of seemingly random behaviour - logs
being created but not used, my original problem with the "AUXSOURCE" text
and even events not even appearing in the log at all!
My level of confidence in the event logging system was flagging so I decided
to restart XP. After a reboot everything seems to be in order. My original
problem has gone and all the missing messages appeared. My original code
works properly with no changes other than making the delete conditional if
the source is pointing at the wrong log.

Thanks

Bill
 
Back
Top