Nugut Installing log4net and Topshelf
1) Servicerunner class
Using Log4net;
Using Topshelf;
Class servicerunner:servicecontrol//, Servicesuspend
{
Private Timer _timer = null;
ReadOnly ILog _log = Logmanager.getlogger (typeof (Servicerunner));
Public Servicerunner ()
{
Double interval = 5000;
_timer = new timer (interval);
_timer. Elapsed + = new Elapsedeventhandler (OnTick);
}
protected virtual void OnTick (object sender, Elapsedeventargs e)
{
_log. Debug ("Tick:" + DateTime.Now.ToLongTimeString ());
}
public bool Start (Hostcontrol Hostcontrol)
{
This._timer = new timer (1000);
This._timer. Elapsed + = this. OnTick;
return true;
}
public bool Stop (Hostcontrol Hostcontrol)
{
This._timer. Enabled = false;
This._timer. AutoReset = false;
return true;
}
public void Start ()
{
_log. Info ("Sampleservice is Started");
_timer. AutoReset = true;
_timer. Enabled = true;
_timer. Start ();
}
public void Stop ()
{
_log. Info ("Sampleservice is Stopped");
_timer. AutoReset = false;
_timer. Enabled = false;
}
}
And then call it in the main function.
static void Main (string[] args)
{
Initlog4net ();
var logger = Logmanager.getlogger (typeof (program));
Logger. Info ("message");
Hostfactory.run (x =
{
X.uselog4net ("~/log4net.config");
X.service<servicerunner> ();
X.setdescription ("Sample topshelf Host");
X.setdisplayname ("Stuff");
X.setservicename ("Stuff");
X.runaslocalsystem ();
X.enablepauseandcontinue ();
});
}
private static void Initlog4net ()
{
var logcfg = new FileInfo (AppDomain.CurrentDomain.BaseDirectory + "Log4net.config");
Xmlconfigurator.configureandwatch (LOGCFG);
}
Log4net.config
<?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
<!--log path-->
<param name= "File" value= "D:\Log\servicelog\"/>
<!--whether to append the log--> to the file,
<param name= "Appendtofile" value= "true"/>
<!--log retention days-->
< param name= "maxsizerollbackups" value= "/>";
<!--the log file name is a fixed-->
<param name= " Staticlogfilename "value=" false "/>
<!--log file name format: 2008-08-31.log-->
<param name=" Datepattern " Value= "Yyyy-mm-dd" read.log" " />
<!--log Scrolls--> by date,
<param name= "Rollingstyle" value= "date"/>
<layout type= "log4net. Layout.patternlayout "
<param name=" Conversionpattern "value="%d [%t]%-5p%c-%m%n%loggername "/>
< ;/layout>
</appender>
<!--console display logs--
<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>
<!--(high) OFF > FATAL > ERROR > WARN > INFO > DEBUG > All (Low)--
<level value= "All"/>
<appender-ref ref= "Coloredconsoleappender"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>
</configuration>
After the compilation is successful
Installing ServiceRunner.exe Install in Cmd.exe
Uninstalling ServiceRunner.exe Uninstall
Run it with ServiceRunner.exe start
Simple implementation of Windows services Topshelf