为什么WriteEntry不能在这个EventLog源上工作?

本文关键字:工作 EventLog WriteEntry 不能 为什么 | 更新日期: 2023-09-27 18:05:58

我正在写一个简单的服务,并记录异常和其他值得注意的项目到EventLog。下面是该服务的代码。不知何故,虽然我可以看到"FDaemon"日志,但我没有看到其中的任何事件。我的启动和停止事件在日志中无处可寻;该日志列出0个事件。

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
namespace FDaemon
{
    public class EmailDigester : ServiceBase, IDebuggableService
    {
        private Timer digestTimer;
        private EmailDigesterWorker worker;
        private EventLog eventLog;
        public EmailDigester()
        {
            // fire up the event log
            this.eventLog = new System.Diagnostics.EventLog();
            ((ISupportInitialize)(this.eventLog)).BeginInit();
            this.eventLog.Source = "EmailDigester";
            this.eventLog.Log = "FDaemon";
            if (!EventLog.SourceExists(this.eventLog.Source))
            {
                EventLog.CreateEventSource(this.eventLog.Source, this.eventLog.Log);
            }
            this.AutoLog = false;
            this.ServiceName = this.eventLog.Source;
            ((ISupportInitialize)(this.eventLog)).EndInit();
        }
        public void DebugStart(string[] args)
        {
            this.OnStart(args);
        }
        protected override void OnStart(string[] args)
        {
            this.worker = new EmailDigesterWorker(1, eventLog);
            // no need to multithread, so use a simple Timer
            // note: do not take more time in the callback delegate than the repetition interval
            if (worker.RunTime.HasValue)
            {
                worker.ServiceStarted = true;
                TimerCallback work = new TimerCallback(this.worker.ExecuteTask);
                TimeSpan daily = new TimeSpan(24, 0, 0);  // repeat every 24 hrs
                TimeSpan startIn; // how much time till we start timer  
                if (worker.RunTime <= DateTime.Now)
                    startIn = (worker.RunTime.Value.AddDays(1.00) - DateTime.Now); // runTime is earlier than now. we missed, so add a day to runTime
                else
                    startIn = (worker.RunTime.Value - DateTime.Now);
                this.digestTimer = new Timer(work, null, startIn, daily);
            }
            eventLog.WriteEntry("EmailDigester started.", EventLogEntryType.Information);
        }
        public void DebugStop()
        {
            this.OnStop();
        }
        protected override void OnStop()
        {
            worker.ServiceStarted = false; 
            if (this.digestTimer != null)
            {
                this.digestTimer.Dispose();
            }
            eventLog.WriteEntry("EmailDigester stopped.", EventLogEntryType.Information);
        }
    }
}

为什么WriteEntry不能在这个EventLog源上工作?

首先:我假设您已经完成了,并且WriteEntry()函数实际上执行了。

如果您的源"EmailDigester"与任何其他EventLogs(例如应用程序,安全等)注册,那么无论您做什么,消息都会出现在该EventLog中。事实上,我认为只有源代码的前8个字符才会被考虑。

你可以通过进入注册表@:HKEY_LOCAL_MACHINE'SYSTEM'CurrentControlSet'Services'Eventlog'和检查每个日志的来源。

您也可以考虑将您的源代码更改为随机值(您知道不会注册),并查看您的日志是否显示