如何调试 windows 服务的 OnSessionChange 事件

How to debug windows service OnSessionChange Event

我有一个 windows 服务,我想看看当用户登录或注销、注销等时会发生什么。我在 [=13] 的覆盖中放置了一个 Debugger.Launch =] 但它不会触发。

我如何调试那部分代码?

public class MyService:ServiceBase
{
     protected override void OnSessionChange(SessionChangeDescription changeDescription) {
         Debugger.Launch();  //debugger does not fire here !
      }
    protected override void OnStart(){ } //debugger fires here
    protected override void OnStop(){ }  //debugger fires here
}

P.S 我期待当我解锁计算机时,触发事件并使用 debugger.Is 进入 visual studio 有任何方法可以调试它,或者日志是查看那里发生了什么的唯一方法?

您需要将 CanHandleSessionChangeEvent 属性 设置为 true 以启用该事件的下沉。