【问题标题】:Writing messages to Application Event Log将消息写入应用程序事件日志
【发布时间】:2013-02-21 22:30:03
【问题描述】:

我在将事件写入 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 + "]";
        }
    }
}

【问题讨论】:

  • 什么是 Tracer.Misc.Error ?完整的源样本?

标签: c# event-viewer


【解决方案1】:

我发现了我的问题。我的代码很好。问题在于我使用的 EventSource 的名称。事件源包含空格和破折号。当我删除它们时,它起作用了。

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

【讨论】:

  • 这也解决了我的问题。没有明显说明事件源不能有空格,甚至事件查看器中的一些事件源名称中都有空格......非常令人费解!
【解决方案2】:

此外,如果您正在登录“应用程序”,事件源也不能是“应用程序”(仅供参考,供人们弄清楚事件日志记录,就像我现在尝试的那样。不是答案)。它会给出如下的日志记录细节 -

来自源应用程序的事件 ID 101 的描述不能是 成立。引发此事件的组件未安装在 您的本地计算机或安装已损坏。你可以安装 或修复本地计算机上的组件。

如果事件源自另一台计算机,则显示信息 必须与事件一起保存。

活动中包含以下信息:

日志消息示例

消息资源存在,但在 字符串/消息表

【讨论】:

    猜你喜欢
    • 2014-11-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多