2013-02-21 57 views
1

我遇到了将事件写入Windows事件日志的问题。我已经看过帖子,我认为我做得正确,但在事件查看器中仍然出现以下错误:将消息写入应用程序事件日志

消息资源存在但消息未在字符串/消息表中找到

有没有人有什么想法我失踪?

我有在维克斯安装程序创建的事件源:

<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 + "]"; 
     } 
    } 
} 
+0

什么是示踪.Misc.Error?全源样本? – Kiquenet 2014-06-04 10:59:34

回答

3

我想通了,我的问题。我有的代码很好。问题在于我使用的EventSource的名称。事件源包含空格和破折号。当我删除它的工作。

I.E.从“我的事件源”到“MyEventSource”

+2

这也解决了我的问题。没有任何地方明确指出事件源不能有空格,甚至事件查看器中的某些事件源在其名称中也有空格......非常令人费解! – dreamlax 2015-02-21 01:40:43

0

此外,如果您正在登录“应用程序”,则事件源也不能是“应用程序”(仅供参考,现在尝试,不是答案)。它将给记录详情如下 -

从源应用程序事件ID 101的描述无法找到 。引发此事件的组件未安装在您的本地计算机的 或安装已损坏。您可以安装 或在本地计算机上修复组件。

如果该事件源于另一台计算机上,显示信息 必须与事件被保存。

下面的信息包括与所述事件:

日志信息示例

消息资源存在,但该消息未在 串/消息表中找到

相关问题