0
我遇到了一個非常奇怪的問題,我之前編寫了一個應用程序。它的工作沒有問題,但一段時間後,它停止運作。我將在此附上代碼:無法寫入Windows應用程序日誌(C#)
try
{
using (Process proc = Process.Start(starter))
{
windowHider();
proc.WaitForExit();
DateTime endStamp = DateTime.Now;
endStamp = truncate(endStamp);
TimeSpan diff = endStamp.Subtract(startStamp);
string programSource = "applicationName";
string logLocation = "Application";
string occurance = "Var='" + varName + "' Var2='"+ var2Name + "' Var3='" + var3Name + "' Var4='" + var4Name + "' Var5='" + var5Name + "' Var6='" + var6Name + "'";
try
{
if (!EventLog.SourceExists(programSource))
{
EventLog.CreateEventSource(programSource, logLocation);
}
else
{
EventLog.WriteEntry(programSource, occurance);
}
}
catch (Exception err)
{
string message = "There was an error with usage logging. Please contact IT.";
MessageBox.Show(message);
errorLogger(message, err.ToString(), ((Control)sender).Name);
this.Close();
}
this.Close();
}
}
當啓動的進程退出時,程序將寫入應用程序日誌。出於某種原因,但是,我收到以下錯誤:
Exception: System.ComponentModel.Win32Exception (0x80004005): The specified path is invalid
它引用此行的原因:
EventLog.WriteEntry(programSource, occurance);
任何想法,這是什麼突如其來的問題可能是?
'programSource'註冊了事件日誌服務嗎? – AlG
您需要將註冊表項HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ services \ eventlog \ Application \ applicationName的權限授予您正在運行代碼的用戶。 –
@AlG使用EventLog.CreateEventSource生成源應該是這樣做的,雖然,對吧?有沒有一種方法來編程註冊程序源? – Rich