C# 记录信息或调试Windows服务的最简单方法是什么?

C# 记录信息或调试Windows服务的最简单方法是什么?,c#,.net,vb.net,windows-services,C#,.net,Vb.net,Windows Services,我需要调试Windows服务,从Visual Studio我无法运行该服务,因此我使用的是Installutil.exe 因此,在每次构建之后,我都会使用Installutil.exe,但我无法在代码中一步一步地进行 你知道任何简单的方法来: 使用VS调试器检查代码 或者在事件中记录一些消息以便我可以跟踪发生了什么 谢谢您可以使用类似于写入文件的记录器,但如果要调试,则有一种方法。您可以使用类似于写入文件的记录器,但如果要调试,则有一种方法 因此,每次构建之后,我都会使用Installuti

我需要调试Windows服务,从Visual Studio我无法运行该服务,因此我使用的是Installutil.exe

因此,在每次构建之后,我都会使用Installutil.exe,但我无法在代码中一步一步地进行

你知道任何简单的方法来:

  • 使用VS调试器检查代码
  • 或者在事件中记录一些消息以便我可以跟踪发生了什么
谢谢

您可以使用类似于写入文件的记录器,但如果要调试,则有一种方法。

您可以使用类似于写入文件的记录器,但如果要调试,则有一种方法

因此,每次构建之后,我都会使用Installutil.exe,但我不能一步一步地进行 请输入我的密码

如果要在VisualStudio中调试服务,则可以执行以下操作

Program.cs
文件中定义:

private static void RunService()
{
    var service = new YourService();
    System.ServiceProcess.ServiceBase.Run(service);
}

//for debug only
private static void RunNoService()
{
    using (var service = new YourService())
    {
        service.Start();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}
Main
方法中

static void Main()
{
    if (Environment.UserInteractive) //This is IMPORTANT
        RunNoService();
    else
        RunService();
}
在您的服务中,定义一个具有
OnStart
功能的
Start
方法,将您的
OnStart
方法修改为:

protected override void OnStart(string[] args)
{
    Start();
}
Environment.UserInteractive
将检查服务是否正在从Visual Studio运行,并允许您调试它

用于日志记录:

始终在服务中记录您的活动。这将帮助您在服务投入生产后调试它。您可以使用或创建自己的自定义日志类。即使登录到一个简单的文本文件也比什么都没有要好。但是你必须保持记录,否则如果生产中出现问题,它会变得非常令人沮丧

因此,每次构建之后,我都会使用Installutil.exe,但我不能一步一步地进行 请输入我的密码

如果要在VisualStudio中调试服务,则可以执行以下操作

Program.cs
文件中定义:

private static void RunService()
{
    var service = new YourService();
    System.ServiceProcess.ServiceBase.Run(service);
}

//for debug only
private static void RunNoService()
{
    using (var service = new YourService())
    {
        service.Start();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}
Main
方法中

static void Main()
{
    if (Environment.UserInteractive) //This is IMPORTANT
        RunNoService();
    else
        RunService();
}
在您的服务中,定义一个具有
OnStart
功能的
Start
方法,将您的
OnStart
方法修改为:

protected override void OnStart(string[] args)
{
    Start();
}
Environment.UserInteractive
将检查服务是否正在从Visual Studio运行,并允许您调试它

用于日志记录:


始终在服务中记录您的活动。这将帮助您在服务投入生产后调试它。您可以使用或创建自己的自定义日志类。即使登录到一个简单的文本文件也比什么都没有要好。但是你必须保持记录,否则如果生产中出现问题,它会变得非常令人沮丧

要在Visual Studio中调试服务,我在服务项目中使用以下代码:

程序.cs中

    static class Program
    {
#if DEBUG
        static AutoResetEvent sare = new AutoResetEvent(false);
#endif

        static void Main()
        {
#if (!DEBUG)           
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new Service() };
            ServiceBase.Run(ServicesToRun);
#else
            Service service = new Service();
            service.DebugServiceStopped += new Action(SetAutoResetEvent);

            service.DebugStart();

            sare.WaitOne();
#endif
        }

#if DEBUG
        private static void SetAutoResetEvent()
        {
            sare.Set();
        }
#endif        
    }
Service.cs(实际
服务
类的文件)中,您需要添加以下代码部分:

#if DEBUG
        public event Action DebugServiceStopped;
        public void DebugStart()
        {
            OnStart(null);
        }
#endif

        protected override void OnStop()
        {
#if DEBUG
            DebugServiceStopped();
#endif
        }
如果在Visual Studio中选择
Debug
作为配置,则可以像调试普通应用程序一样调试该服务,否则该项目将编译为真正的服务

日志记录: 在Windows上有Windows事件日志,用于存储应用程序的信息、警告和错误。从服务中,事件日志可以通过以下方式写入:

EventLog.WriteEntry("your log message", EventLogEntryType.Information); // or EventLogEntryType.Error,... depending on the entry type

要在Visual Studio中调试服务,我将在服务项目中使用以下代码:

程序.cs中

    static class Program
    {
#if DEBUG
        static AutoResetEvent sare = new AutoResetEvent(false);
#endif

        static void Main()
        {
#if (!DEBUG)           
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new Service() };
            ServiceBase.Run(ServicesToRun);
#else
            Service service = new Service();
            service.DebugServiceStopped += new Action(SetAutoResetEvent);

            service.DebugStart();

            sare.WaitOne();
#endif
        }

#if DEBUG
        private static void SetAutoResetEvent()
        {
            sare.Set();
        }
#endif        
    }
Service.cs(实际
服务
类的文件)中,您需要添加以下代码部分:

#if DEBUG
        public event Action DebugServiceStopped;
        public void DebugStart()
        {
            OnStart(null);
        }
#endif

        protected override void OnStop()
        {
#if DEBUG
            DebugServiceStopped();
#endif
        }
如果在Visual Studio中选择
Debug
作为配置,则可以像调试普通应用程序一样调试该服务,否则该项目将编译为真正的服务

日志记录: 在Windows上有Windows事件日志,用于存储应用程序的信息、警告和错误。从服务中,事件日志可以通过以下方式写入:

EventLog.WriteEntry("your log message", EventLogEntryType.Information); // or EventLogEntryType.Error,... depending on the entry type

我通常使用一些AOP工具,比如PostSharp来插入
Trace.WriteLine
调用(你也可以自己做)。当我需要排除故障时,我会设置一个。我通常使用一些AOP工具,比如PostSharp来插入
Trace.WriteLine
调用(你也可以自己做)。当我需要排除故障时,我会设置一个。