C#服务作为一个守护进程在debian与单声道服务 [英] C# service as a daemon in debian with mono-service
问题描述
我做错了什么?
我从msdn跟随这个帖子来创建一个示例的Windows服务: http://msdn.microsoft.com/en-us/library/zt39148a(v = vs80) .aspx
我可以在我的Windows机器上运行服务,启动并停止服务,看到它写入MyNewLog。
然后我将它(.exe文件)复制到我的debian机器,并尝试使用(作为root用户)单声道服务运行MyNewService.exe
Syslog告诉我服务已经开始了!
我没有错误,我看不到系统中新创建的任何日志文件。我做错了什么?
如果有帮助,这里是代码:
Program.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.ServiceProcess;
使用System.Text;
命名空间MyNewService
{
static class Program
{
///< summary>
///应用程序的主入口点。
///< / summary>
static void Main()
{
System.ServiceProcess.ServiceBase [] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase []
{
new MyNewService()
};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
}
Service.cs
using System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Diagnostics;
使用System.Linq;
使用System.ServiceProcess;
使用System.Text;
命名空间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(服务暂停:OnStop);
}
}
}
/干杯
尝试使用System.Diagnostics.EventLog记录其他地方,也许是文本文件或类似文件。
我最近找不到任何东西,但这个 post 表明,在Linux上运行时,EventLog简单地被丢弃;这是有道理的。
编辑:
调查Mono源似乎承担了这一点。事件记录有三种可能的选项:win32,local和null。如果MONO_EVENTLOG_TYPE环境变量设置为本地,那么在Linux上,默认情况下,日志应写入/ var / lib / mono / eventlog。
你真的需要隔离您的记录代码从您的期望 - 似乎您的服务工作正常,但记录是问题。
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.
这篇关于C#服务作为一个守护进程在debian与单声道服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!