How do I output a single record log for each event, currently it is repeating the same record over and over in the output file

The below code just keeps filling the text file with the same SessionSwitchReason.SessionLock.
I just want it to enter it into the log once and then clear itself out for the next SessionSwitchReason. I tried setting e.Reason to null but that didn’t work and I don’t know how to handle it in: SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;

using System;
using Microsoft.Win32;
using static Microsoft.Win32.SystemEvents;

namespace CSharpTutorials
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
            }       
        }

        static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLogon:
                    //Logon
                    RecordLog("User logged into the session.");
                    break;
                case SessionSwitchReason.SessionLogoff:
                    //Logoff
                    RecordLog("User logged off the session.");
                    break;
                case SessionSwitchReason.RemoteConnect:
                    //Remote Connect
                    RecordLog("User remote connected.");
                    break;
                case SessionSwitchReason.RemoteDisconnect:
                    //Remote Disconnect
                    RecordLog("User remote disconnected.");
                    break;
                case SessionSwitchReason.SessionLock:
                    //lockQ
                    RecordLog("User locked the computer.");
                    break;
                case SessionSwitchReason.SessionUnlock:
                    //Unlock
                    RecordLog("User unlocked the computer.");
                    break;
                default:
                    break;
            }
        }
        public class MyClass 
        {
            public int MyProperty { get; set; }
        }
        private static void RecordLog(string message)
        {
            // Add code to record the event in a log file or database
            // For example, you can write the message to a log file:
            using (StreamWriter writer = File.AppendText("log.txt"))
            {
                writer.WriteLine(message);
            }
        }
    }
}


It has been suggested that I should remove the while loop to fix my problem. That said, when I do that, the program appears to run once and quit. I need to keep it running as I want to develop a log of these events.
Output of Debugging just stops

>Solution :

SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; adds the event handler (SystemEvents_SessionSwitch) to the event SessionSwitch on the object SystemEvents. This means that when the SessionSwitch event fires, it calls the method you have defined. By adding the event handler in an infinite loop, you are adding many instances of the method to the event so when the event fires once it will call that method over and over again.

You need to get rid of the while loop and only call the SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; once.

Leave a Reply