日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 如何调用EventLog

發布時間:2025/3/19 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 如何调用EventLog 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

工作原理:

? ? ?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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。