为什么 WriteEntry 不适用于此 EventLog 源?

发布于 2024-11-30 06:20:04 字数 2716 浏览 1 评论 0原文

我正在编写一个简单的服务并将异常和其他值得注意的项目记录到事件日志中。以下是该服务的代码。不知何故,虽然我可以看到“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);
        }
    }
}

I am writing a simple service and logging exceptions and other notable items to the EventLog. Below is the code for the service. Somehow, although I can see the "FDaemon" log, I don't see any events in it. My started and stopped events are nowhere in the log; the log lists 0 events.

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-12-07 06:20:05

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

如果您的源“EmailDigester”已注册到任何其他EventLogs(例如应用程序、安全性等),则消息将出现在该EventLog中> 无论你做什么。事实上,我相信只考虑源的前 8 个字符

您可以通过转到注册表@来检查这一点:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\ 并检查每个日志的来源。

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

First: I am assuming that you've stepped through and the WriteEntry() function does, in fact, execute.

If your source "EmailDigester" is registered with any other EventLogs (e.g. Application, Security, etc.), then the messages will appear in that EventLog no matter what you do. In fact, I believe only the first 8 characters of a source are considered.

You can check this by going to the registry @:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\ and checking each log's sources.

You might also considering changing your source to a random value (that you know won't be registered) and seeing if your logs show up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文