工作原理:

1.在没有指定logname,仅仅指定了source的时候。

1.1 source存在

在写eventlog的时候,首先去找source,如果找到的话,就往这个source所在的log里面写日志。

   EventLog eventLog = new EventLog();
eventLog.Source = $@"LisaEventLog 2018-04-17 18:37:16.907 +08:00";
var message =
$"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{AppDomain.CurrentDomain.FriendlyName} {DateTimeOffset.Now}";
eventLog.WriteEntry(message, EventLogEntryType.Error);
Console.WriteLine($@"{eventLog.Log},{eventLog.Source}");

1.2 source 不存在 (直接绑定Application作为logname,然后自动创建一个source)

https://github.com/dotnet/corefx/

 

dotnet\corefx\src\System.Diagnostics.EventLog\src\System\Diagnostics\EventLogInternal.cs

private void VerifyAndCreateSource(string sourceName, string currentMachineName)

如果log没有指定,默认会使用Application

if (GetLogName(currentMachineName) == null)
this.logName = "Application";

然后自动创建一个event source

EventLog.CreateEventSource(new EventSourceCreationData(sourceName, GetLogName(currentMachineName), currentMachineName));

  EventLog eventLog = new EventLog();
eventLog.Source = $@"{nameof(LisaEventLog)} {DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zzz}";
var message =
$"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{AppDomain.CurrentDomain.FriendlyName} {DateTimeOffset.Now}";
eventLog.WriteEntry(message, EventLogEntryType.Error);
Console.WriteLine($@"{eventLog.Log},{eventLog.Source}");

2.指定logname和source

2.1 source不存在

2.1.1 logname也不存在

那么会自动创建log和source,然后写log

2.1.2 logname存在

那么会在log下自动创建source,然后写log

2.2 source存在

那么这个source肯定有对应的log了,要么不指定log,让系统自动去匹配。上面的1.1

如果要指定log的话,那么必须指定为正确的,否则会抛出异常

3. source没有指定

这个是不允许的

System.ArgumentException : Source property was not set before writing to the event log.
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)

 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, EventLogEntryType type)
{
var sourceName = AppDomain.CurrentDomain.FriendlyName;
if (!EventLog.SourceExists(sourceName))
{
EventLog.CreateEventSource(sourceName, _logName);
}
using (EventLog eventLog = new EventLog(_logName))
{
eventLog.Source = sourceName;
var message = $"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{error}";
eventLog.WriteEntry(message, type);
}
}
}

左侧栏里面的叫做LogName,每一条event log中的source列,对应的是source

EventLog.Entries

这里的entries是指event log,比如上图中对应有5个。

System.ArgumentException : Only the first eight characters of a custom log name are significant, and there is already another log on the system using the first eight characters of the name given. Name given: 'Application1', name of existing log: 'Application'.
at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)
at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)

System.ArgumentException : The source 'klnagent2' is not registered in log 'Application'. (It is registered in log 'Appplicat'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property.
at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)
at ExcelTest.Test.TestEventLog() in D:\ChuckLu\Git\Edenred\LISA_5.0.0.0\ExcelTest\Test.cs:line 692

C# 如何调用EventLog的更多相关文章

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

    面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针对各种事件的日志,我们的应用 ...

  2. 将日志写入EventLog

    将日志写入EventLog 面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针 ...

  3. Requested registry access is not allowed(不允许所请求的注册表访问权)

    尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息 EventLog.CreateEventS ...

  4. 【Logstash系列】使用Logstash作为收集端采集IIS日志

    现阶段Logstash在Windows端的日志采集一直存在若干问题,包括:   1. LS有读锁:进程开启后Input指定路径下的所有文件都会被锁死无法重命名或删除. 2. LS不识别*:如果在pat ...

  5. c#.NET中日志信息写入Windows日志中解决方案

    1. 目的应用系统的开发和维护离不开日志系统,选择一个功能强大的日志系统解决方案是应用系统开发过程中很重要的一部分.在.net环境下的日志系统解决方案有许多种,log4net是其中的佼佼者.在Wind ...

  6. Windows Power Shell

    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能. 它引入了许多非常有用的新概念,从而进一步扩展了您在 W ...

  7. 《果壳中的C# C# 5.0 权威指南》 - 学习笔记

    <果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...

  8. 黑马毕向东Java基础知识总结

    Java基础知识总结(超级经典) 转自:百度文库 黑马毕向东JAVA基础总结笔记    侵删! 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部 ...

  9. 玩转PowerShell第二节——【利用PsExec进行远程调用】-技术&分享

    概述 PowerShell用的最多的地方就是远程调用,在远程机器上执行脚本,监控远程机器的状态,如NLB状态,EventLog,SqlServer DataBase状态等. 本篇将讲到用PsExec. ...

随机推荐

  1. zblog插件增加后台导航栏的方法

    有时我们经常需要对插件进行设置,但是又不能让用户去做这些,那么下面的方法将会给插件增加在后台导航栏显示的功能 首先打开对应插件的文件夹,找到对应插件的  include.php  文件 将下面的代码粘 ...

  2. STL之string篇

    常用代码整理: #include<iostream> #include<cstdio> #include<cstring> #include<string&g ...

  3. JS——预解析

    1.排查语法错误 <script> console.log(1; </script> 2.变量提升和函数整体提升 <script> console.log(n1); ...

  4. strut2 拦截器 使用

    拦截器是strut2里一个很振奋人心的应用.通过配置拦截器可以在action执行之前进行一些初始化或者是其他的操作,但是在action执行之后,返回结果就已经确定,结果是很难改变了(目前我还不知道怎么 ...

  5. Split()函数

    最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组. 贴出一个奇怪的例子 using System; using System.Collecti ...

  6. CAD绘制一个箭头(com接口)

    1 2 3 4 5 6 7 8 //绘制一个箭头  axMxDrawX1.PathMoveToEx(1000, 300, 10, 10, 0);  //设置路径下一点  axMxDrawX1.Path ...

  7. jdbcUrl is required with driverClassName错误解决

    jdbcUrl is required with driverClassName springboot2.0配置多数据源: spring.datasource.primary.url=jdbc:mys ...

  8. ThinkPHP框架表单验证AJAX

    验证有两种方式:静态验证与动态验证. 一.静态验证 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 验证时要在test表的Model里面加验证条件:新建testModel.class. ...

  9. Oracle最大游标数控制

    /************************************************************************ ********* Oracle最大游标数控制 ** ...

  10. Spring MVC 笔记2 HelloWorld

    实现这个例子的问题 WEB-INFO目录下必须有spring的包,放在lib下:如下图(这里我直接把idea创建时宣称springmvc,然后把idea给的lib拷贝了下来,也可以的) request ...