title: Debugging Program Keeps Sending Emails After Exit
body: <p>I have the below code which I use as part of a program which reads some event logs.</p>
  <p>After stepping through the code this morning to test something out, I keep getting error messages emailed to me:</p>
  <blockquote>
  <p>EventLogHelper error occurred in QueryEvents</p>
  <p>Exception: The RPC server is unavailable</p>
  <p>User:</p>
  <p>Client: D7-089</p>
  </blockquote>
  <p>The process is not running on my machine, and I only stepped through the method in question once or twice. However The messages just keep coming. The space of time between each message seems to vary, and I have received at least 15 now.</p>
  <p>How is this possible? I feel that if the program is sending me emails, it must be executing somewhere, but I cannot see it in the <code>Processes</code> tab in <code>Task Manager</code>, and I tried ending all <code>Visual Studio</code> related processes without any joy.</p>
  <hr />
  <p>I restarted the PC on which VS was running, but I am still receiving the emails.</p>
  <hr />
  <h3>Main()</h3>
  <pre><code>public static void Main()
  {
      var eh = new EventLogHelper();
      var eventFired = eh.CheckEvents();
  }
  </code></pre>
  <h1>EventLogHelper.cs</h1>
  <pre><code>public readonly string PcName;
  private readonly int Timespan;
  private readonly string Filter;
  private static EventLogSession Session;
  
  /// &lt;summary&gt;
  /// ctor
  /// &lt;/summary&gt;
  public EventLogHelper()
  {
      Timespan = 30000; // 30 seconds
      PcName = &quot;D7-089&quot;; // This is usually &quot;Environment.MachineName&quot;, but specified it here for testing
      Filter = $&quot;*[System[(EventID='5061' or EventID='5058') and TimeCreated[timediff(@SystemTime) &lt;= {Timespan}]]]&quot;;
  }
  </code></pre>
  <h3>CheckEvents</h3>
  <pre><code>/// &lt;summary&gt;
  /// Checks the event logs for remote pc and returns true if any of the events we are interested in fired
  /// &lt;/summary&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public bool CheckEvents()
  {
      var query = BuildQuery(PcName, Filter);
      
      for (var i = 0; i &lt; 60; i++)
      {
          var logs = QueryEvents(query);
          var events = ReadLogs(logs);
  
          if (events &gt; 0)
          {
              return true;
          }
  
          System.Threading.Thread.Sleep(1000);
      }
  
      return false;
  }
  </code></pre>
  <h3>BuildQuery</h3>
  <pre><code>/// &lt;summary&gt;
  /// Builds an EventLogQuery for the given pcname and filter. This should be set up with a user who has admin rights
  /// &lt;/summary&gt;bh
  private static EventLogQuery BuildQuery(string pcName, string filter)
  {
      try
      {
          using (var pw = GetPassword())
          {
              Session = new EventLogSession(
              pcName,
              &quot;DOMAIN&quot;,
              &quot;USER&quot;,
              pw,
              SessionAuthentication.Default);
          }
  
          return new EventLogQuery(&quot;Security&quot;, PathType.LogName, filter)
              { Session = Session };
      }
      catch (Exception ex)
      {
          Email.Send($&quot;EventLogHelper error occurred in BuildQuery \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {pcName}&quot;);
          Environment.Exit(Environment.ExitCode);
          return null;
      }
  }
  </code></pre>
  <h3>QueryEvents</h3>
  <p>This is where the error is occurring. I stepped through this method 2 times <strong>at most</strong> and as I type this question I am still getting error emails through.</p>
  <pre><code>/// &lt;summary&gt;
  /// Execute the given EventLogQuery
  /// &lt;/summary&gt;
  private EventLogReader QueryEvents(EventLogQuery query)
  {
      try
      {
          return new EventLogReader(query);
      }
      catch (Exception ex)
      {
          Email.Send($&quot;EventLogHelper error occurred in QueryEvents \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {PcName}&quot;);
          Environment.Exit(Environment.ExitCode);
          return null;
      }
  }
  </code></pre>
  <h3>ReadLogs</h3>
  <pre><code>/// &lt;summary&gt;
  /// Read the given EventLogReader and return the amount of events that match the IDs we are looking for
  /// &lt;/summary&gt;
  /// &lt;param name=&quot;logReader&quot;&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private int ReadLogs(EventLogReader logReader)
  {
      var count5058 = 0;
      var count5061 = 0;
      EventRecord entry;
  
      try
      {
          while ((entry = logReader.ReadEvent()) != null)
          {
  
              if (entry.Id == 5058)
              {
                  count5058++;
              }
              else
              {
                  count5061++;
              }
          }
      }
      catch (Exception ex)
      {
          Email.Send($&quot;EventLogHelper error occurred in ReadLogs \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {PcName}&quot;);
          Environment.Exit(Environment.ExitCode);
      }
  
      return count5058 + count5061;
  }
  </code></pre>