C#服务作为debian中的守护进程,具有单一服务

Chr*_*ian 0 c# mono service debian daemon

我似乎无法找到一种方法让一个C#服务作为debian中的"服务"运行.我究竟做错了什么?

我跟着这个帖子从MSDN创建一个样本窗口服务:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx

我可以在我的Windows机器上运行该服务,启动和停止服务,并看到它写入MyNewLog.

然后我将它(.exe文件)复制到我的debian机器并尝试使用(作为root)mono-service MyNewService.exe运行它

Syslog告诉我,服务已经开始了!

我没有错误,我在系统中看不到任何新创建的日志文件.我究竟做错了什么?

如果有帮助,这里是代码: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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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.");
    }
}
}
Run Code Online (Sandbox Code Playgroud)

/干杯

The*_*man 5

尝试使用System.Diagnostics.EventLog以外的其他地方进行日志记录,可能是文本文件或类似文件.

我最近找不到任何东西,但是这篇文章表明在Linux上运行时只丢弃了EventLog; 这是有道理的.

编辑:

调查Mono源似乎证实了这一点.事件记录有三种可能的选项:win32,local和null.如果MONO_EVENTLOG_TYPE环境变量设置为local,则在Linux上,日志应默认写入/ var/lib/mono/eventlog.

您真的需要将您的日志记录代码与您的期望隔离开来 - 似乎您的服务工作正常,但日志记录是问题所在.