我在将事件写入 Windows 事件日志时遇到问题。我浏览了这些帖子,我认为我做得对,但我仍然在事件查看器中收到以下错误:
消息资源存在,但在字符串/消息表中找不到消息
有人对我所缺少的有任何想法吗?
我在 WIX 安装程序中创建了事件源:
<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>
这是我写入事件日志的 C# 代码:
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 + "]";
}
}
}