Search code examples
c#custom-eventlog

Can't Write to Windows Application Log (C#)


I'm having a really strange problem with an application I wrote a while back. It has worked without issues, but after leaving it alone for a while, it simply stopped functioning. I'll attach the code here:

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();
    }
}

When the process that was started exits, the program writes to the application log. For some reason, however, I am getting the following error:

Exception: System.ComponentModel.Win32Exception (0x80004005): The specified path is invalid

It cites this line as the cause:

EventLog.WriteEntry(programSource, occurance);

Any ideas as to what this sudden problem could be?


Solution

  • I figured it out! There was something corrupted in the registry, and there must have been another corrupted component lurking around somewhere. I changed the sourcename, and it worked without any issues.

    The original sourcename works on other machines, which makes me think it was definitely just something wonky with the registry.