2013-03-12 118 views
0

我似乎无法找到一种方式让C#服务在debian中作为“服务”运行。 我在做什么错?C#服务作为Debian中的单服务守护进程

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

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

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

系统日志告诉是我该服务已经开始!

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

如果有帮助,这里是代码: 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."); 
    } 
} 
} 

/干杯

+0

pleaee显示一些源代码...因为你的服务在debian上开始,似乎主要问题是缺少日志? – Yahia 2013-03-12 10:52:47

回答

5

尝试登录其他地方比System.Diagnostics程序。 EventLog,可能是文本文件或类似的文件。

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

编辑:

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

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

+0

谢谢,我纠正!我创建了一个单独的文件来登录。它的工作。现在我遇到了停止服务的问题。在PID上运行了一个。当它开始时它被创建并且被停止删除。但它自己的服务不起作用。当删除锁,服务工作,但我不能停止没有“ps -efH”,然后杀死PID ....服务MyApp停止不会这样做! – Christian 2013-03-13 12:40:03

+0

@Christian可能更好地补充说,作为一个新的问题:) – TheNextman 2013-03-13 14:35:00

+0

可能,thanx! – Christian 2013-03-14 18:29:34

0

组环境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

[email protected]:/var/lib/mono/eventlog/Application$ ls 
1.log 2.log 3.log Application EDAClasses.OrderLogWriterClass 
[email protected]:/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. 
相关问题