Easier way to debug a Windows service

后端 未结 28 1922
春和景丽
春和景丽 2020-11-22 15:36

Is there an easier way to step through the code than to start the service through the Windows Service Control Manager and then attaching the debugger to the thread? It\'s ki

28条回答
  •  -上瘾入骨i
    2020-11-22 16:32

    The best option is to use the 'System.Diagnostics' namespace.

    Enclose your code in if else block for debug mode and release mode as shown below to switch between debug and release mode in visual studio,

    #if DEBUG  // for debug mode
           **Debugger.Launch();**  //debugger will hit here
           foreach (var job in JobFactory.GetJobs())
                {
                    //do something 
                }
    
    #else    // for release mode
          **Debugger.Launch();**  //debugger will hit here
         // write code here to do something in Release mode.
    
    #endif
    

提交回复
热议问题