2016-12-06 84 views
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); 

任何想法,這是什麼突如其來的問題可能是?

+0

'programSource'註冊了事件日誌服務嗎? – AlG

+0

您需要將註冊表項HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ services \ eventlog \ Application \ applicationName的權限授予您正在運行代碼的用戶。 –

+0

@AlG使用EventLog.CreateEventSource生成源應該是這樣做的,雖然,對吧?有沒有一種方法來編程註冊程序源? – Rich

回答

0

我想通了!註冊表中有些東西被破壞,並且肯定還有另一個被破壞的組件在某個地方潛伏着。我更改了源名稱,並且沒有任何問題。

原始的源名在其他機器上工作,這讓我覺得它絕對只是註冊表的一些東西。

相關問題