工作原理:

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. 接口测试(一)--soapui实践

    一.接口的概念 接口是指系统模块与模块或系统与系统之间进行交互,一般用的多的是HTTP协议的接口.webService协议的接口,还有RPC的接口. RPC:Remote Procedure Call ...

  2. Cesium学习笔记(四)Camera

    http://blog.csdn.net/HobHunter/article/details/74909641 Cesium 相机控制场景中的视野.操作相机的方法有很多,如旋转,缩放,平移和飞到目的地 ...

  3. Learning opencv续不足(七)线图像的设计D

    因为线图像startline有了起点和终点,我们就可以用DDA法求出线上所有点,任意斜率直线通过四象限八区域查表法界定.我们只示范一个区域:函数为: public PointF DdaFindPtIm ...

  4. windows 小知识---windows下生成公钥和私钥

    首先Windows操作系统需要安装git. 安装完成后,再到任意的文件夹内,点击右键.选择git bash here 打开之后,输入ssh-keygen,一路按enter键. 全部结束后,再到C:\U ...

  5. Appium 教您完美win10安装Appium1.7.2支持win客户端自动化

    参考内容: https://testerhome.com/topics/10193https://testerhome.com/topics/8223https://testerhome.com/to ...

  6. Python----递归------Eight Queens 八皇后问题

    递归思想是算法编程中的重要思想. 作为初学者,对递归编程表示很蒙逼,每次遇到需要递归的问题,心里就有一万头草泥马飞过~~~~~~(此处略去一万头草泥马) 在B站看数据结构与算法的视频时,视频中给了两个 ...

  7. A water problem (hdu-5832)

    不多说就是一个数对两个数的乘积求模运算 不得不说vj上这个题的翻译版本真是太暴力了 难点 主要还是时间的控制,这题太容易TLE了.用到的算法就是大数求余数的ans=(ans*10-a[i]-'0')% ...

  8. CentOS6.8 安装python2.7,pip以及yum

    由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...

  9. GitHub:创建和修改远程仓库

    创建远程仓库 首先在GitHub上创建一个仓库命名为learngit.选中public(private要钱),选中 生成README(就是markdown形式的说明文档),便于别人和自己了解仓库的作用 ...

  10. 用Twebbrowser做可控编辑器与MSHTML(调用js)

    记得intraweb的网页设计也程序开发分开中,是怎么定义的变量的.就是在网页中插入占位符.我们规定占位符是{%Name%} {%Birthday%}单页面装载之前or之后,我们用自己的js查找占位符 ...