【问题标题】:C# service as a daemon in debian with mono-serviceC# 服务作为具有单服务的 debian 中的守护进程
【发布时间】:2013-02-27 20:17:57
【问题描述】:

我似乎找不到让 C# 服务在 debian 中作为“服务”运行的方法。 我做错了什么?

我按照 msdn 的这篇文章创建了一个示例 Windows 服务:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx

我可以在我的 windows 机器上运行该服务,启动和停止该服务并查看它是否写入 MyNewLog。

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

系统日志告诉我服务已经启动!

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

如果有帮助,代码如下: 程序.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.");
    }
}
}

/干杯

【问题讨论】:

  • pleaee 显示一些源代码...由于您的服务是在 debian 上启动的,因此主要问题似乎是缺少日志?

标签: c# service mono debian daemon


【解决方案1】:

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

我找不到任何最近的东西,但是这个post 表明在 Linux 上运行时 EventLog 被简单地丢弃了;这是有道理的。

编辑:

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

您确实需要将您的日志记录代码与您的期望隔离 - 看起来您的服务运行良好,但日志记录是问题所在。

【讨论】:

  • 谢谢,我已纠正!我创建了一个单独的文件来记录。它奏效了。现在我在停止服务时遇到问题。在 PID 上做了一个 并且可以工作。开始时它被创建并停止删除。但是它自己的服务将不起作用。移除锁时,服务可以工作,但如果没有“ps -efH”我无法停止它,然后杀死 PID .... service MyApp stop 不会这样做!
  • @Christian 最好将其添加为一个新问题:)
【解决方案2】:

设置环境 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多