调试windows服务最简单的方法之一(转载)

转自:http://www.cnblogs.com/builderman/archive/2011/06/14/2081045.html

原理:先修改Program中的调用逻辑,增加在用户交互模式中的调用逻辑,然后将程序的输出类型改为控制台应用程序,此时即增加了用户交互,然后增加2个方法在控制台程序的Program中调用,等调试完成,再修改输出类型为windows应用程序

实现如下:

1、服务类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
    
    public partial class TestService : ServiceBase
    {

        private static log4net.ILog log = log4net.LogManager.GetLogger("SysLogger");

        public TestService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            log.Info("this is a test service start");
        }

        protected override void OnStop()
        {
            log.Info("this is a test service stop");
        }

        internal void DebugStart()
        {
            log.Info("this is a test service start");
        }

        internal void DebugStop()
        {
            log.Info("this is a test service stop");
        }
    }
}

2、Program调用类

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            TestService test = new TestService();
            if (Environment.UserInteractive)
            {
                test.DebugStart();
                Console.ReadKey();
                test.DebugStop();
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { test };
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
}

关键就在对Environment.UserInteractive的判断上,存在交互界面,则调用true,不存在节目,则调用false
请看MSDN上面的解释:获取一个值,用以指示当前进程是否在用户交互模式中运行。
UserInteractive 属性为运行时无用户界面的 Windows 进程或一个服务(如 IIS)报告 false。 如果此属性为 false,请不要显示模式对话框或消息框,因为没有用来与用户进行交互的图形用户界面。

更改Project的输出类型,即可调用不同的交互逻辑,实现服务的调试与运行。

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <root>
      <level value="ERROR" />
      <appender-ref ref="nhAppender" />
    </root>
    <!-- Print only messages of level DEBUG or above in the packages -->
    <logger name="SysLogger" additivity="false">
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </logger>
    <appender name="nhAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="ERROR" />
      </filter>
      <param name="File" value="nhLogs/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />-->
        <param name="ConversionPattern" value="%newline%date [%thread] %n级别:  %-5level %n所在类:%location%n描述:%message%newline %n" />
      </layout>
    </appender>
    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="ERROR" />
      </filter>
      <param name="File" value="Logs/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />-->
        <param name="ConversionPattern" value="%newline%date [%thread] %n级别:  %-5level %n所在类:%location%n描述:%message%newline %n" />
      </layout>
    </appender>
  </log4net>
</configuration>

猜你喜欢

转载自blog.csdn.net/yzy85/article/details/82846547