C#服务为Debian中有守护进程单服务 [英] C# service as a daemon in debian with mono-service

查看:280
本文介绍了C#服务为Debian中有守护进程单服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一种方式来获得运行在Debian的服务一个C#的服务。 ?
我究竟做错了

I can't seem to find a way to get a C# service run as a "service" in debian. What Am I doing wrong?

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

I followed this post from msdn to create a sample windows service : http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx

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

I can run the service at my windows machine, Start and stop the service and see that it writes to MyNewLog.

然后我把它复制(.exe文件)到我的Debian系统,并尝试与运行(作为root)单服务MyNewService.exe

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?

如果有帮助,这里是代码:
的Program.cs

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

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

推荐答案

尝试登录其他地方比System.Diagnostics.EventLog,也许到一个文本文件或类似的。

Try logging somewhere else than with System.Diagnostics.EventLog, perhaps to a text file or similar.

我找不到任何最近的,但是这个建议在Linux上运行时,事件日志,简单地丢弃; 。这将使意义

I can't find anything recent, but this post suggests that the EventLog is simply discarded when running on Linux; which would make sense.

编辑:

调查Mono的源似乎证实了这一点。有事件日志记录三种可能的选择:Win32中,本地和空。如果MONO_EVENTLOG_TYPE环境变量设置为本地,那么在Linux日志应该是默认的写入/ var / lib中/单声道/事件日志。

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.

这篇关于C#服务为Debian中有守护进程单服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆