Feng's profileLudwig的骇客帝国BlogLists Tools Help

Blog


    March 20

    EventLog

    EventLog.CreateEventSource(source, logName);
    LogName, 日志的名称。这可以是 Application、System 和 Security,或一个自定义的日志名称。默认值为空字符串 ("")。

    默认情况下,服务器上存在三个日志文件: Application、System 和 Security。应用程序和服务使用 Application 日志文件。设备驱动程序使用 System 日志文件。打开审核时,系统在 Security 日志中生成成功审核事件和失败审核事件。如果安装了其他应用程序(如 Windows Server 上的 Active Directory),则可能会有其他默认日志文件。另外,还可以在本地计算机或远程计算机上创建自定义日志文件。与组件向默认的 Application 日志写入事件相比,自定义日志可以更详细地组织日志项。

    如果写入事件日志,则仅指定 Log (即LogName)属性是不够的。必须将 Source 属性与事件日志资源关联,才能将其连接到特定的日志。若仅从日志中读取,则不需要指定 Source,但必须使某个事件源与服务器注册表中的事件日志资源关联。只能指定 Log 名称和从中读取的 MachineName(服务器计算机名称)。

    EventLog

    Code:
    using System;
    using System.Diagnostics;
    using System.Threading;
    class MySample{

        public static void Main(){
            // Create the source, if it does not already exist.
            if(!EventLog.SourceExists("MySource")){
                EventLog.CreateEventSource("MySource", "Application");  //日志写入"应用程序"分类下
            }
            // Create an EventLog instance and assign its source.
            EventLog myLog = new EventLog();
            myLog.Source = "MySource";
            // Write an informational entry to the event log.   
            myLog.WriteEntry("Writing to event log.");
        }
    }