Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a windows service as follows:
C#
public partial class UserMonitorService : ServiceBase
    {

        public UserMonitorService()
        {
             InitializeComponent();
        }

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLock:
                    LogEntry(string.Format("Locked at {0}", DateTime.Now));
                    break;
                case SessionSwitchReason.SessionLogoff:
                    LogEntry(string.Format("Logged Off at {0}", DateTime.Now));
                    break;
                case SessionSwitchReason.SessionLogon:
                    LogEntry(string.Format("Logged On at {0}", DateTime.Now));
                    break;
                case SessionSwitchReason.SessionUnlock:
                    LogEntry(string.Format("Unlocked at {0}", DateTime.Now));
                    break;
                default:
                    break;
            }
        }

        protected override void OnStart(string[] args)
        {
            LogEntry(string.Format("Service Started at {0}", DateTime.Now));
            SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
        }

        protected override void OnStop()
        {
            LogEntry(string.Format("Service Stopped at {0}", DateTime.Now));
        }

        void LogEntry(string message)
        {
            StreamWriter sw = new StreamWriter("D:/ServiceLog.txt",true);
            sw.WriteLine(message);
            sw.Close();
        }

    }

After installing, it logs Service start and stop messages perfectly, but does not log any of the session related events (so I know that problem is NOT with the logging part here).
Also, when I create a Console Application using the same code and execute it, it logs session lock/unlock events too (so I know that the SessionSwitch handler is also correct).

So what is the mistake here? Am I missing some windows service configuration change that needs to be done here? Thanks in advance.
Posted
Updated 31-Mar-16 1:21am
v2

Hi!

When you initialize the service, you have to set the "CanHandleSessionChangeEvent" property to true. It is set to false by default. You also need to enable other service abilities explicitly:

EDIT: Where the service component is initialized:

C#
private void InitializeComponent()
{
    this.CanHandleSessionChangeEvent = true;
    this.ServiceName = "MyUserMonitoringServiceName"; // Or whatever your service is called
}


Some other things you can enable here:

CSS
this.AutoLog = true;
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;


EDIT 2:

Instead of the way you process your change event...

CSS
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)


once you have enabled the ability as above, create your event handler like this:

CSS
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    switch(changeDescription.Reason)
    {
        case SessionChangeReason.SessionLock:
            LogEntry(string.Format("Locked at {0}", DateTime.Now));
            break;
        case SessionChangeReason.SessionLogoff:
            LogEntry(string.Format("Logged Off at {0}", DateTime.Now));
            break;
        case SessionChangeReason.SessionLogon:
            LogEntry(string.Format("Logged On at {0}", DateTime.Now));
            break;
        case SessionChangeReason.SessionUnlock:
            LogEntry(string.Format("Unlocked at {0}", DateTime.Now));
            break;
        default:
            break;

    }
}




Hope that helps!
 
Share this answer
 
v3
Comments
Member 8854976 20-Apr-12 8:53am    
Did NOT work :(
Thoughtweaver 23-Apr-12 8:47am    
I've edited the solution to include tested code. Hope it helps!
Member 8854976 23-Apr-12 23:58pm    
I still don't know why my code wasn't working, but this solution given by you works like a charm. So I still don't know what the problem was but it is solved at least...
Thanks a lot man! Really appreciate.
Thoughtweaver 24-Apr-12 2:02am    
You are most welcome!

Just a note: The reason your code (and my initial code) did not work, was that although the handler was coded, there was really nothing telling it *when* to perform the code. By telling it *what* we want it to listen for:

this.CanHandleSessionChangeEvent = true;

And then overriding the windows service event handler:

protected override void OnSessionChange(SessionChangeDescription changeDescription){...}

We end up with something that works!
Meir Zakay from Tel Aviv, Israel 7-Dec-14 10:13am    
Works like a charm! Thanks a lot!
 
Share this answer
 
Comments
Member 8854976 24-Apr-12 0:01am    
Thanks.
Anderson V. B. Costa 15-Aug-13 13:23pm    
Doesn't work with Terminal Service

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900