轉自: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=""Logs_"yyyyMMdd".txt"" />
<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=""Logs_"yyyyMMdd".txt"" />
<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>