简体   繁体   English

Windows服务输出未显示在事件日志中

[英]Windows Service output not showing up in Event Log

I am trying to follow this walkthrough . 我试图按照这个演练

I'm using Visual Studio 2010 Premium. 我正在使用Visual Studio 2010 Premium。

The only events I see in Server Explorer or in Event Viewer are "Service started successfully." 我在服务器资源管理器或事件查看器中看到的唯一事件是“服务已成功启动”。 and "Service stopped successfully." 并且“服务已成功停止”。

Here is the service code: 这是服务代码:

namespace MyNewService
{
    public partial class MyNewService : ServiceBase
    {
        public MyNewService()
        {
            InitializeComponent();
            if (!EventLog.SourceExists("MySource"))
            {
                EventLog.CreateEventSource("MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart");
        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("In onStop.");
        }
    }
}

I had the same issue with Visual Studio 2010 Professional on Win 7 64. My newly compiled service was installed with InstallUtil.exe running as Administrator. 我在Win 7 64上遇到了与Visual Studio 2010 Professional相同的问题。我新安装的服务是在以管理员身份运行的InstallUtil.exe上安装的。 It started and stopped correctly. 它正确启动和停止。 No exceptions but no amount of refreshing the Event Viewer revealed a new event log. 没有例外,但事件查看器没有刷新任何数量的事件日志。 Fran71's suggestion worked for me: I closed the Event Viewer and reopened. Fran71的建议对我有用:我关闭了事件查看器并重新打开。 Voilà, the newly created application log file appeared. Voilà,新创建的应用程序日志文件出现了。

FWIW, I ran into this problem in Win8 and found that Refreshing the Event Viewer did not cause my newly created Event Log to show up, but closing and reopening Event Viewer did. FWIW,我在Win8中遇到了这个问题,发现刷新事件查看器并没有导致我新创建的事件日志显示,但是关闭并重新打开事件查看器。 Was driving me insane since no exceptions were being thrown when Creating the Event source or writing a log entry. 因为在创建事件源或编写日志条目时没有抛出异常,所以让我疯了。

Probably a permissions problem. 可能是权限问题。 Like the commenter said, try running VStudio or your compiled service as an Administrator. 与评论者说的一样,尝试以管理员身份运行VStudio或编译的服务。

Also, you can still debug a service - put a thread.Sleep(up to 20 seconds) as your first line (to give yourself time to attach the debugger), then put a break point on the line after that. 此外,您仍然可以调试服务 - 将一个thread.Sleep(最多20秒)作为您的第一行(给自己时间来附加调试器),然后在该行之后放置一个断点。 Then go Tools --> Attach To Process and select your .exe. 然后转到工具 - >附加到进程并选择您的.exe。 If you get that done before the end of your Thread.Sleep() then you should break. 如果你在Thread.Sleep()结束之前完成了那么你应该打破。

Keep in mind that the service must complete the OnStart within IIRC 30 seconds or Windows will think it is not responding and kill your process, so move your code out of the service start if you are going to do much debugging. 请记住,该服务必须在IIRC内完成OnStart 30秒,否则Windows会认为它没有响应并终止您的进程,因此如果您要进行大量调试,请将代码移出服务启动。 Just throw a timer in there or something. 只需在那里扔一个计时器。

To make it easier to debug, consider moving your functionality to a DLL and then keep your service to just enough code to call into your DLL - this will ease unit testing and debugging - but don't forget that lots of things change when you're actually running it as a service. 为了便于调试,请考虑将您的功能转移到DLL,然后将您的服务保持足够的代码以调用您的DLL - 这将简化单元测试和调试 - 但不要忘记,当您'时,很多事情会发生变化'实际上将它作为一项服务运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM