我有一個控制檯應用程序將通過計劃任務運行,我想要做的是寫入事件日誌中的catch塊。我試過使用寫入事件日誌Windows計劃任務
EventLog.WriteEntry("My App Name","Error Message - " ex.ToString());
但由於某些原因,它不寫入錯誤。 我做錯了什麼?
感謝
我有一個控制檯應用程序將通過計劃任務運行,我想要做的是寫入事件日誌中的catch塊。我試過使用寫入事件日誌Windows計劃任務
EventLog.WriteEntry("My App Name","Error Message - " ex.ToString());
但由於某些原因,它不寫入錯誤。 我做錯了什麼?
感謝
您需要確保事件源存在,如:
if (!EventLog.SourceExists("MySource"))
EventLog.CreateEventSource("MySource","Application");
此代碼是從MSDN網站在C#中,我希望它幫你。
using System;
using System.Diagnostics;
using System.Threading;
class MySample{
public static void Main(){
// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource")){
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";
// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
}
有一點要注意的是,有時會有調用EventLog.CreateEventSource當一個小延遲,因此在創建後立即嘗試訪問創建的EventSource時應該意識到這一點。