C# service as a daemon in debian with mono-service(C# 服务作为具有单服务的 debian 中的守护进程)
问题描述
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 中的守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 服务作为具有单服务的 debian 中的守护进程
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01