C# 为什么WriteEntry不适用于此事件日志源?
我正在编写一个简单的服务,并将异常和其他值得注意的项目记录到事件日志中。下面是该服务的代码。不知何故,虽然我可以看到“FDaemon”日志,但在其中我没有看到任何事件。日志中没有我的已启动和已停止事件;日志列出了0个事件C# 为什么WriteEntry不适用于此事件日志源?,c#,logging,event-log,C#,Logging,Event Log,我正在编写一个简单的服务,并将异常和其他值得注意的项目记录到事件日志中。下面是该服务的代码。不知何故,虽然我可以看到“FDaemon”日志,但在其中我没有看到任何事件。日志中没有我的已启动和已停止事件;日志列出了0个事件 using System; using System.ComponentModel; using System.Diagnostics; using System.ServiceProcess; using System.Threading; namespace FDaemo
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);
}
}
}
使用系统;
使用系统组件模型;
使用系统诊断;
使用System.ServiceProcess;
使用系统线程;
名称空间FDaemon
{
公共类EmailDigester:ServiceBase,IDebuggableService
{
专用定时器;
私人雇员;
私有事件日志事件日志;
公共电子邮件摘要()
{
//启动事件日志
this.eventLog=new System.Diagnostics.eventLog();
((ISupportInitialize)(this.eventLog)).BeginInit();
this.eventLog.Source=“EmailDigester”;
this.eventLog.Log=“FDaemon”;
如果(!EventLog.SourceExists(this.EventLog.Source))
{
CreateEventSource(this.EventLog.Source,this.EventLog.Log);
}
this.AutoLog=false;
this.ServiceName=this.eventLog.Source;
((ISupportInitialize)(this.eventLog)).EndInit();
}
public void DebugStart(字符串[]args)
{
这个.OnStart(args);
}
启动时受保护的覆盖无效(字符串[]args)
{
this.worker=新的EmailDigesterWorker(1,事件日志);
//不需要多线程,所以使用一个简单的计时器
//注意:回调委托的时间不要超过重复间隔
if(worker.RunTime.HasValue)
{
worker.ServiceStarted=true;
TimerCallback work=新的TimerCallback(this.worker.ExecuteTask);
TimeSpan daily=新的TimeSpan(24,0,0);//每24小时重复一次
TimeSpan startIn;//还有多少时间我们才能启动计时器
if(worker.RunTime首先:我假设您已经完成了,并且实际上执行了WriteEntry()
函数
如果您的源“EmailDigester”
已在任何其他事件日志中注册(例如应用程序、安全性等),则无论您做什么,消息都将出现在该事件日志中。事实上,我相信只考虑源的前8个字符
您可以通过进入注册表@来检查这一点:
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\
并检查每个日志的来源
您还可以考虑将源代码更改为随机值(您知道该值不会被注册),并查看日志是否显示。是,WriteEntry()确实执行。而且,EmailDigester的注册表中除了HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\eventlog\FDaemon下的条目外,没有其他条目。我注意到一件奇怪的事情,就是,逐步查看,eventlog.Entries集合实际上包含85个条目,但我在事件查看器中没有看到它们?对不起,revi正在删除这个旧线程,但是我他妈的怎么才能从“应用程序”中注销我的源代码呢?(我可以创建自定义日志和源代码,但它仍然会登录到应用程序日志中)。请帮助我!谢谢。@Mitulátbáti你试过了吗?