工作原理:

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. gtest ASSERT_TRUE和EXPECT_TRUE

    调用ASSERT_TRUE的函数,返回值类型定义必须是void,如果想返回别的类型,就用EXPECT_TRUE: void abc::fun() { ASSERT_TRUE(fun1()); } bo ...

  2. ubuntu14.3安装phpmyadmin

    一.安装 sudo apt-get install phpmyadmin 二.软连接 cd /var/www/html/ sudo ln -s /usr/share/phpmyadmin phpmya ...

  3. 《Mysql - 到底可不可以使用 Join ?》

    一:Join 的问题? - 在实际生产中,使用 join 一般会集中在以下两类: - DBA 不让使用 Join ,使用 Join 会有什么问题呢? - 如果有两个大小不同的表做 join,应该用哪个 ...

  4. const浅析

    前言 c++中使用到const的地方有很多, 而且const 本身也针对不同的类型可能有不同的含义, 比如对指针就有顶层和底层. 本节就是探讨关于C++中const的在不同的地方不同表现或含义. co ...

  5. TCP/IP UDP 协议首部及数据进入协议栈封装的过程

    数据的封装 UDP 封装 TCP 封装 IP 封装 检验和算法 当应用程序用TCP传送数据时,数据被传送入协议栈中,然后逐一通过每一层直到被当作一串比特流送入网络 注: UDP数据TCP数据基本一致. ...

  6. Navicat premium连接Oracle报ORA-28547错误

    1:ORA-28547 原因:navicate Primium版本的OCi和本地数据库的OCI版本不一致. 解决方法: 1:把navicate Primium版本自带oci.dll替换本地Oracle ...

  7. Django——1 环境搭建

    Django 什么是Django 使用前的准备工作 新建项目 开启服务器 新建APP 简单实战 什么是Django框架 http服务器:用来接受用户请求,并将请求转发给web应用框架进行处理.Web应 ...

  8. noip模拟赛 三部曲

    分析:子树上操作,要用到线段树+dfs序,关键就是子树内k还要增加,这个就不是很好办.可以求出在根节点+0后每个点会加多少,记为d[i],如果要对点x进行A操作,实际上只需要对子树加k - d[i]再 ...

  9. spring boot 中访问 REST 接口

    RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("http ...

  10. nyoj_18_The Triangle_201312071533

    The Triangle 时间限制:1000 ms  |           内存限制:65535 KB 难度:4   描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure ...