2013-11-05 26 views
12

シンプルですが、イベントログに何か書きたいと思います。イベントログ書き込みエラー

protected override void OnStop() 
    { 
     // TODO: Add code here to perform any tear-down necessary to stop your service. 
     if (!System.Diagnostics.EventLog.SourceExists("IvrService")) 
     { 
      System.Diagnostics.EventLog.CreateEventSource(
       "IvrService", "IvrServiceLog"); 
     } 
     EventLog eventLog1 = new System.Diagnostics.EventLog(); 
     eventLog1.Source = "IvrService"; 
     eventLog1.Log = "IvrServiceLog"; 
     try 
     { 
      eventLog1.WriteEntry("Successfully "+State.Stopped.ToString()); 
      IvrApplication.StopImmediate(); 
     } 
     catch (Exception ex) 
     { 
      // eventLog1.WriteEntry(ex.Message); 
     } 
    } 

例外は次のとおりです。

Failed to stop service. System.ArgumentException: The source 'IvrService' is not registered in log 'IvrServiceLog'. (It is registered in log 'Application'.) " 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) 

答えて

15

エラーメッセージが間違っている正確に何を言っています。イベントソースIvrServiceがIvrServiceLogではなくアプリケーションログに登録されています。 System.Diagnostics.EventLog.SourceExistsは、特定のログではなくソースが存在することを確認します。

私は、最初にこれをアプリケーションログに登録してから、後でIvrServiceLogに書き込むように変更したと思います。

開発マシンをクリーンアップするには、次のコードを実行するだけでコードを進めることができます。

System.Diagnostics.EventLog.DeleteEventSource("IvrService"); 
+0

あなたは最初にそれを削除し、とにかく私のコードを使用することを意味しました。 –

+0

@Loveええ、既存のログをクリーンアップするために開発マシンに一度だけ投稿したコードを実行する必要があります。 –

関連する問題