Quartz.NET summary (eight) How to configure the service according to their Topshelf

Speaking in front of how to use this article Topshelf rapid development of service windows, you can see clear before: https://www.cnblogs.com/zhangweizhong/category/771057.html ,

Today to talk about the configuration of Topshelf.

Simple Configuration

Official documentation of the HostFactory inside the parameters of a detailed explanation: http: //docs.topshelf-project.com/en/latest/configuration/config_api.html, next only to some commonly used methods simple explanation:

We will look at the above program code change:

            HostFactory.Run(x =>                                 //1
            {
                x.Service<TownCrier>(s =>                        //2
                {
                    s.ConstructUsing (name => new new TownCrier ());      // configure a fully customized service for Topshelf no dependencies. Common way.
            // The Start-Service and STOP Methods for The 
                    s.WhenStarted (TC => tc.Start ());               // . 4 
                    s.WhenStopped (TC => tc.Stop ());                // . 5 
                });
                x.RunAsLocalSystem ();                             // service runs NETWORK_SERVICE built-in accounts. Identity, there are several ways, such as: x.RunAs ( "username", " password"); x.RunAsPrompt (); x.RunAsNetworkService (); et 

                x.SetDescription ( " Description Sample Topshelf Host Service " ) ;         // after installation services, and services described 
                x.SetDisplayName ( " Stuff display name " );                        // display name 
                x.SetServiceName ( " Stuff service name " );                        // service name 
            });    

 

Heavy equipment installed to run:

Quartz.NET summary (eight) complete Topshelf service configuration

 

Through the above, we believe we are well aware SetDescription, SetDisplayName, SetServiceName difference. Not elaborate.

Service Configuration

Topself service generally there are two modes of use.

First, the simple mode. ServiceControl inherited interface, this interface can be.

Quartz.NET summary (eight) complete Topshelf service configuration

 

Example:

namespace TopshelfDemo
{
    public class TownCrier : ServiceControl
    {
        private Timer _timer = null;
        readonly ILog _log = LogManager.GetLogger(typeof(TownCrier));
        public TownCrier()
        {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);
        }

        public  bool Start (Host Control Host control)
        {
            _log.Info("TopshelfDemo is Started");
            _timer.Start ();
            return true;
        }

        public  bool Stop (Host Control Host control)
        {
            throw new NotImplementedException();
        }
    }
    class Program {
        public static void Main(string[] args)
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);
            HostFactory.Run(x => {
                x.Service<TownCrier>();
                x.RunAsLocalSystem();
                x.SetDescription ( " the Sample topshelf Host service description " );
                x.SetDisplayName ( " Stuff display name " ); x.SetServiceName ( " Stuff service name " );
            });
        }
    }
}

 

Second, the common pattern.

Quartz.NET summary (eight) complete Topshelf service configuration

 

Example:

namespace TopshelfDemo {
    public class TownCrier {
        private Timer _timer = null;
        readonly ILog _log = LogManager.GetLogger(typeof(TownCrier));
        public TownCrier() {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);
        }
        public void Start()
        {
            _timer.Start();
        }
        public void Stop()
        {
            _timer.Stop();
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);
            HostFactory.Run(x => {
                x.Service<TownCrier>(s => {
                s.ConstructUsing(name => new TownCrier());
                s.WhenStarted(tc => tc.Start());
                s.WhenStopped(tc => tc.Stop()); });
                x.RunAsLocalSystem();
                x.SetDescription("Sample Topshelf Host服务的描述");
                x.SetDisplayName("Stuff显示名称");
                x.SetServiceName("Stuff服务名称");
            });
        }
    }
}

 

两种方式,都使用了Log4Net,相关配置:

<? xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name = "log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name = "RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--日志路径-->
      <param name = "File" value= "D:\App_Data\servicelog\"/>
      <!--是否是向文件中追加日志-->
      <param name= "AppendToFile" value= "true"/>
      <!--log保留天数-->
      <param name= "MaxSizeRollBackups" value= "10"/>
      <!--日志文件名是否是固定不变的-->
      <param name= "StaticLogFileName" value= "false"/>
      <!--日志文件名格式为:2008-08-31.log-->
      <param name= "DatePattern" value= "yyyy-MM-dd".log=""""/>
      <!--日志根据日期滚动-->
      <param name= "RollingStyle" value= "Date"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d[% t] %-5p %c - %m%n %loggername" />
      </layout>
    </appender>
    <!-- 控制台前台显示日志 -->
    <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="Info" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="Info" />
        <param name="LevelMax" value="Fatal" />
      </filter>
    </appender>
    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="all" />
      <appender-ref ref="ColoredConsoleAppender"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
</configuration>

A second common mode is recommended.

Guess you like

Origin www.cnblogs.com/zhangweizhong/p/12177680.html