EventLog.EntryWritten Event

刚看到在MSND论坛上有人问一个EventLog.EntryWritten Event相关的问题,说是在2015触发了一个2013年的EventWritten的事件,比较好奇,然后查看了下这个类:

https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.entrywritten%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

在的Remarks里看到有这么一段话:

The system responds to WriteEntry only if the last write event occurred at least six seconds previously. This implies you will only receive one EntryWritten event notification within a six-second interval, even if more than one event log change occurs. If you insert a sufficiently long sleep interval (around 10 seconds) between calls to WriteEntry, you are less likely to miss an event. However, if write events occur more frequently, you might not recieve the event notification until the next interval. Typically, missed event notifications are not lost, but delayed.

意思大概是这个时间只会在6秒内触发一次,如何将两次调用WriteEntry的时间间隔大于6s,那就不会造成事件miss,只是会被延迟。写了一个程序来测试下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P20150409
{
    class Program
    {
        static void Main(string[] args)
        {
            EventLog myNewLog = new EventLog("Application", ".", "dotNET Sample App");

            myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);
            myNewLog.EnableRaisingEvents = true;
            while (true)
            {
                System.Threading.Thread.Sleep(3000);
                string EventWriteTime = DateTime.Now.ToString();
                Console.WriteLine("Log is written at" + EventWriteTime);
                myNewLog.WriteEntry("Test message written at" + EventWriteTime + " " + System.Threading.Thread.CurrentThread.ManagedThreadId, EventLogEntryType.Information);
                Console.WriteLine();
            }
            Console.ReadLine();
        }

        private static void MyOnEntryWritten(object sender, EntryWrittenEventArgs e)
        {
            System.Threading.Thread.Sleep(6000);
            Console.WriteLine("EntryWritten event is fired at" + DateTime.Now.ToString());
            Console.WriteLine("Log time is" + e.Entry.Message);
            Console.WriteLine();
        }
    }
}

以下是输出结果:

可以看到事件确实是没有被miss,而且触发的那次call EntryWrite方法与当前call EntryWrite的时间间隔越来越大,也就是说,这是有可能在两年后触发该事件。。。):

但是在把事件里的线程睡眠代码去掉后,事件是实时触发的,所谓的6s时间间隔内的限制并没有体现出来,不知道是不是测试的方式不对,还是系统的设置问题。如各位有知道,请指导下。

时间: 2024-08-16 18:57:06

EventLog.EntryWritten Event的相关文章

C#学习日记24----事件(event)

事件为类和类的实例提供了向外界发送通知的能力,实现了对象与对象之间的通信,如果定义了一个事件成员,表示该类型具有 1.能够在事件中注册方法 (+=操作符实现). 2.能够在事件中注销方法(-=操作符实现). 3.当事件被触发时注册的方法会被通知(事件内部维护了一个注册方法列表).委托(Delegate)是事件(event)的载体,要定义事件就的要有委托.  有关委托的内容请点击 委托(De... www.mafengwo.cn/event/event.php?iid=4971258www.maf

PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件

本系列文章从最初的初识开始,基本上可以完成一些简单的系统管理了,为了更方便的管理系统,同时为了更好的发掘系统的性能,就需要用到系统提供 的一些高级特性,在Windows Server系列的OS中,如果可以利用最新的特性对系统进行管理,将会是一件非常不错的事情,虽然目前WinServer用的比较少 但是在一些地方还是有用到,尤其当某些场合需要非计算机专业的人员在服务器上进行操作的时候,winServer以其比Unix/Linux简单性更加适合应用.今天 这里就对PS中一些高级特性进行介绍. 一.W

Win7 服务优化个人单机版

我的PC设备比较旧了,为了系统能流畅点,不必要的服务就不开启了.然而,服务那么多,每次重装,都要从头了解一下一边,浪费时间. 个人在网络上收集信息并结合自己的摸索,整理如下,以备查找. 服务名称  显示名称 设置状态  备注  AxInstSV  ActiveX Installer (AxInstSV)  Manual    SensrSvc  Adaptive Brightness  Disable  非触屏用户可以关了,当然有感光传感器的显示器的,也应打开,一般的显示器没有.  AeLook

服务器重启后SQL Server Agent由于"The EventLog service has not been started" 启动失败

案例环境: 操作系统   : Microsoft Windows Server 2003 Standard Edtion SP2 数据库版本 : SQL Server 2005 Standard Edition SP4 案例描述: 服务器重启过后,MSSQLSERVER服务自动重启了,但是SQLSERVERAGENT服务启动失败(当然SQL Agent服务的启动类型为自动启动(Automatic)),在这台服务器第二次遇到这种情况,第一次遇到时没太注意,以为只是特殊案例,直到在这台服务器第二次遇

当访问EventLog时,抛出SecurityException

当调用EventLog.SourceExists("source1")时,抛出一下异常 System.Security.SecurityException was unhandled  HResult=-2146233078  Message=The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.  Source=System  S

Android event logcat的研究

经常有这样的需求:在程序A启动之后,在这个程序上覆盖一个界面,让用户输入密码后才能启动程序A,这类场景主要集中在安全软件中. 那应该怎样得知某某程序启动了,系统没有相应的广播,这很难知道程序启动了. 既然没有通知,那我们就只能用最笨的方法:主动去询问最近启动过哪些程序. 这需要用后台服务器一直跑,每隔一段时间就询问一次.程序启动的时候,会在Logcat中打印出一些日志信息,其中就有类似下面的日志,表明是启动了哪个程序 01-21 15:09:56.957: I/ActivityManager(2

.NET Core的日志[4]:将日志写入EventLog

面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针对各种事件的日志,我们的应用也可以利用提供的API将日志消息写到EventLog中.与EventLog相关的API都定义在System.Diagnostics.EventLog这个类型中,我们不仅仅可以利用它读取.写入和删除日志,还可以使用它来创建和删除Event Source..NET Core的日志模型利用EventLog

添加无线服务wzcsvc服务,Eventlog服务

<添加eventlog服务.reg> Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog]"Description"="启用在事件查看器查看基于 Windows 的程序和组件颁发的事件日志消息.无法终止此服务.""DisplayName"="Event Log"&quo

C# 如何调用EventLog

public class LisaEventLog { private readonly string _logName = @"Lisa"; public string LogName => _logName; public LisaEventLog() { } public LisaEventLog(string logName) { _logName = logName; } public void WriteEntry(string error, EventLogEntr