简体   繁体   English

将消息写入应用程序事件日志

[英]Writing messages to Application Event Log

I am running into an issue writing events to the Windows Event Log. 我遇到了将事件写入Windows事件日志的问题。 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: 我在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>

And here is my C# code where I write to the Event Log: 这是我写入事件日志的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 + "]";
        }
    }
}

I figured out my issue. 我想出了我的问题。 The code I have is fine. 我的代码很好。 The problem is with the name of the EventSource I was using. 问题在于我使用的EventSource的名称。 The event source contained spaces and dashes. 事件源包含空格和短划线。 When I removed those it worked. 当我删除那些它工作。

IE going from 'My Event-Source' to 'MyEventSource' IE从“我的事件源”变为“MyEventSource”

Also, the event source cannot also be "Application", if you are logging in "Application" (Just FYI, for people figuring out event logging, as I was trying now. Not an answer). 此外,如果您正在登录“应用程序”(仅供参考,对于那些计算事件记录的人,正如我现在正在尝试的那样,事件源也不能是“应用程序”。不是答案)。 It would give the logging details as the following - 它将提供日志详细信息如下 -

The description for Event ID 101 from source Application cannot be found. 无法找到源应用程序中的事件ID 101的描述。 Either the component that raises this event is not installed on your local computer or the installation is corrupted. 引发此事件的组件未安装在本地计算机上,或者安装已损坏。 You can install or repair the component on the local computer. 您可以在本地计算机上安装或修复该组件。

If the event originated on another computer, the display information had to be saved with the event. 如果事件源自另一台计算机,则必须随事件一起保存显示信息。

The following information was included with the event: 活动中包含以下信息:

Log message example 记录消息示例

the message resource is present but the message is not found in the string/message table 消息资源存在但在字符串/消息表中找不到该消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM