I am running into an issue writing events to the Windows Event Log. I have looked through the posts and I think I am doing it correctly, but I still get the following error in the event viewer:
the message resource is present but the message is not found in the string/message table
Does anyone have any ideas on what I am missing?
I am having the event source created in the WIX installer:
<Fragment>
<PropertyRef Id="NETFRAMEWORK35"/>
<PropertyRef Id="NETFRAMEWORK20"/>
<PropertyRef Id="NETFRAMEWORK20INSTALLROOTDIR"/>
<PropertyRef Id="NETFRAMEWORK20INSTALLROOTDIR64" />
<Component Id='EventSource'
Directory='INSTALLDIR'
Guid='F382AAC5-F7C5-46A4-95CF-EA7724DXXXXX' >
<Condition>ALLUSERS</Condition>
<?if $(var.Platform) = x64 ?>
<util:EventSource Log='Application'
Name='MySource'
EventMessageFile='[NETFRAMEWORK20INSTALLROOTDIR64]EventLogMessages.dll'
KeyPath='yes' />
<?else ?>
<util:EventSource Log='Application'
Name='MySource'
EventMessageFile='[NETFRAMEWORK20INSTALLROOTDIR]EventLogMessages.dll'
KeyPath='yes' />
<?endif ?>
</Component>
</Fragment>
And here is my C# code where I write to the Event Log:
public class Log
{
private const string EventSource = "MySource";
private const string EventLog = "Application";
private static EventLog _logger = null;
private static void InitLogger()
{
if (!System.Diagnostics.EventLog.SourceExists(EventSource))
{
System.Diagnostics.EventLog.CreateEventSource(EventSource, EventLog);
}
_logger = new EventLog() { Source = EventSource, Log = EventLog };
}
public static void Write(EventLogEntryType entryType, string format, params object[] args)
{
string entry = _Format(format, args);
try
{
if (_logger == null)
InitLogger();
_logger.WriteEntry(entry, entryType, 1);
}
catch (Exception ex)
{
Tracer.Misc.Error("Could not write to event log: {0}", entry);
}
}
private static string _Format(string format, object[] args)
{
if ((args == null) || (args.Length == 0))
return format;
try
{
return String.Format(format, args);
}
catch (Exception e)
{
return format + " [Format Exception:" + e.Message + "]";
}
}
}
I figured out my issue. The code I have is fine. The problem is with the name of the EventSource I was using. The event source contained spaces and dashes. When I removed those it worked.
I.E. going from 'My Event-Source' to 'MyEventSource'