C# service as a daemon in debian with mono
I can't seem to find a way to get a C# service run as a "service" in debian. What Am I doing wrong?
I followed this post from msdn to create a sample windows service : http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
I can run the service at my windows machine, Start and stop the service and see that it writes to MyNewLog.
Then I copy it (.exe file) to my debian machine and tries to run it with (as root) mono-service MyNewService.exe
The Syslog tell's me that the service have started!
I have no errors and I can't see any newly created logfiles in the system. What Am I doing wrong?
If it helps, here is the code: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace MyNewService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new MyNewService()
};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
}
Service.cs
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 MyNewService
{
public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
myEventLog.Source = "MySource";
myEventLog.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
myEventLog.WriteEntry("Service Started: OnStart");
}
protected override void OnStop()
{
myEventLog.WriteEntry("Service Halted: OnStop.");
}
}
}
/Cheers
Try logging somewhere else than with System.Diagnostics.EventLog, perhaps to a text file or similar.
I can't find anything recent, but this post suggests that the EventLog is simply discarded when running on Linux; which would make sense.
EDIT:
Investigating the Mono source seems to bear this out. There are three possible options for event logging: win32, local and null. If the MONO_EVENTLOG_TYPE environment variable is set to local, then on Linux the logs should be written to /var/lib/mono/eventlog by default.
You really need to isolate your logging code from your expectations - it seems that your service works fine, but the logging is the problem.
set environment MONO_EVENTLOG_TYPE https://man.cx/mono(1)
/lib/systemd/system/order-log-writer.service
[Service]
Environment=MONO_EVENTLOG_TYPE=local:/var/lib/mono/eventlog
User=root
Group=root
Type=forking
PIDFile=/run/order-log-writer.pid
ExecStart=/usr/bin/mono-service -l:/run/order-log-writer.pid /usr/bin/rmq-bot/order-log-writer.exe
ExecStop=/bin/kill -HUP $MAINPID
C#
try
{ }
catch (Exception e)
{
EventLog.WriteEntry(this.GetType().FullName, " error[.] " + e.Message);
}
Linux
user@host:/var/lib/mono/eventlog/Application$ ls
1.log 2.log 3.log Application EDAClasses.OrderLogWriterClass
user@host:/var/lib/mono/eventlog/Application$ cat 1.log
InstanceID: 0
EntryType: 4
Source: EDAClasses.OrderLogWriterClass
Category: 0
TimeGenerated: 20170926140803456
ReplacementStrings: 1
error[.] Error converting value 1 to type 'OrderLog'. Path '', line 1, position 1.
链接地址: http://www.djcxy.com/p/90022.html