简体   繁体   English

在 C# 写入 Application EventLog

[英]Write to Application EventLog in C#

I would like to write a custom source in the Event Viewer, for a number of different events.我想在事件查看器中为许多不同的事件编写自定义源。

I'd like to have the log name to just be "Application" and source to be "DDG ServiceWare".我希望日志名称为“Application”,源名称为“DDG ServiceWare”。 I looked at the documentation on MSDN and made this:我查看了 MSDN 上的文档并做了这个:

    private static void _writeToApplicationEventLog(string logMessage, int eventID)
    {
        if(!EventLog.SourceExists("DDG ServiceWare"))
        {
            EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");
            EventLog.CreateEventSource(exitEvents);
        }

        using (EventLog eventLog = new EventLog("Application"))
        {
            eventLog.Source = "DDG ServiceWare";
            eventLog.WriteEntry(logMessage, EventLogEntryType.Error, eventID);
        }
    }

However, when I run it I get an exception saying:但是,当我运行它时,出现异常:

An unhandled exception of type 'System.ArgumentException' occurred in System.dll The source 'DDG ServiceWare' is not registered in log 'Application'. System.dll 中发生类型为“System.ArgumentException”的未处理异常源“DDG ServiceWare”未在日志“应用程序”中注册。 (It is registered in log 'DDG ServiceWare'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property. (它在日志 'DDG ServiceWare' 中注册。)" Source 和 Log 属性必须匹配,或者您可以将 Log 设置为空字符串,它将自动与 Source 属性匹配。

It doesn't change even if I change EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application");即使我更改EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "Application"); to EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "DDG ServiceWare");EventSourceCreationData exitEvents = new EventSourceCreationData("DDG ServiceWare", "DDG ServiceWare");

What am I doing wrong here?我在这里做错了什么?

You may want to consider a package like SeriLog which will make logging much easier (not just to event logs but to files and other destinations).您可能需要考虑像 SeriLog 这样的 package,这将使日志记录变得更加容易(不仅是事件日志,还有文件和其他目的地)。 Have a look at this https://github.com/serilog/serilog/wiki/Writing-Log-Events看看这个https://github.com/serilog/serilog/wiki/Writing-Log-Events

i created example for you我为你创建了例子

void Main()
{
    _writeToApplicationEventLog("test", 123, EventLogEntryType.Error);
    _writeToApplicationEventLog("test", 123, EventLogEntryType.Warning);
    _writeToApplicationEventLog("test", 123, EventLogEntryType.Information);
}

private static void _writeToApplicationEventLog(string logMessage, int eventID, EventLogEntryType logType)
{
    string sourceName = "SampleApplicationSource";
    string myLogName = "myNewLog";
    string messageFile = "customlog.log";
    
    if (!EventLog.SourceExists(sourceName))
    {
        EventSourceCreationData mySourceData = new EventSourceCreationData(sourceName, myLogName);
        mySourceData.MessageResourceFile = messageFile;
        mySourceData.CategoryResourceFile = messageFile;
        mySourceData.CategoryCount = 1;
        mySourceData.ParameterResourceFile = messageFile;
        EventLog.CreateEventSource(mySourceData);
    }

    myLogName = EventLog.LogNameFromSourceName(sourceName,".");

    using (EventLog eventLog = new EventLog(myLogName, ".", sourceName))
    {
        eventLog.Source = sourceName;
        eventLog.WriteEntry(logMessage, logType, eventID);
    }

    
}

the log created under Applications and Services under name myNewLog在名为 myNewLog 的应用程序和服务下创建的日志在此处输入图像描述

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

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