如何将多个应用程序(源)中的事件写入同一事件日志? [英] How to write events from multiple applications(sources) to the same event log?

查看:99
本文介绍了如何将多个应用程序(源)中的事件写入同一事件日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义事件日志,并希望我的所有应用程序都写入同一个事件日志。 (您将能够在Windows日志中的事件查看器中看到这种情况。)

同样,我有2个服务要写入同一个eventLog。我尝试通过创建一个事件日志并从我创建的2个Windows服务中传递不同的源名称来实现这一点。但我发现只有一个服务写入日志,而另一个没有。



此外,当我尝试停止服务时,服务1无法停止和抛出一个错误。

错误显示 Windows无法停止本地计算机上的< servicename>服务。错误1061:此时服务无法接受控制消息。



我尝试过:



以下是我为其创建的类库事件簿。



I have created a custom event log and would like all my applications to write to the same event log. (You will be able to see that happen in your Event Viewer in Windows Logs.)
Similarly, I have 2 services that I want to write to the same eventLog. I tried doing that by creating one event log and passing different source names from the 2 Windows Services that I have created. But I find only one Service writing to the log while the other doesn't.

Also, when I try to stop the services, Service 1 fails to stop and throws an error.
The error reads "Windows could not stop the <servicename> service on Local Computer. Error 1061: The service cannot accept control messages at this time."

What I have tried:

Below is the class library that I created for Event Log.

public class EventLogger
{
    private EventLog eventLog1 = new System.Diagnostics.EventLog();

    public EventLogger(string logSource)
    {
        if (!System.Diagnostics.EventLog.SourceExists(logSource))
        {
            System.Diagnostics.EventLog.CreateEventSource(logSource, "SampleLog");
        }
        eventLog1.Source = logSource;
        eventLog1.Log = "SampleLog";
    }

    public void WriteLog(string message)
    {
        eventLog1.WriteEntry(message);
    }
}





创建第一个Windows服务



Created 1st Windows Service

public partial class Service1 : ServiceBase
{
    private EventLogger.EventLogger eventLog;
    public Service1()
    {
        InitializeComponent();
        eventLog = new EventLogger.EventLogger("WinService1");
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            eventLog.WriteLog("Service started in 1st");
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.ToString());
        }
    }

    protected override void OnStop()
    {
        eventLog.WriteLog("Service stopped");
    }
}





如上所述创建了第二个Windows服务。





Created the 2nd windows Service as well, as above.

public partial class Service2 : ServiceBase
{
    private EventLogger.EventLogger eventLog;
    public Service2()
    {
        InitializeComponent();
        eventLog = new EventLogger.EventLogger("WinService2");
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            eventLog.WriteLog("Service started in 2nd");
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.ToString());
        }
    }

    protected override void OnStop()
    {
        eventLog.WriteLog("Service stopped");
    }
}





Service1似乎没有记录任何东西,而我可以看到日志客服2。我可能在这里错误地做了很多事情。请帮我找到解决方案。此外,如果可以通过使用log4Net实现这一点,那么也欢迎解决方案。提前谢谢。



Service1 doesn't seem to log anything, whereas, I can see the logs for Service2. I might be doing a lot of things incorrectly here. Please help me in finding a solution to this. Also, if this can be achieved by using log4Net then solutions with respect to that are welcome as well. thanks in advance.

推荐答案

这可能无法解决您的问题,但有一点值得一提:



在注册之后,您不能使用 EventLog ,因为系统需要一些时间来刷新已注册的源列表。因此,建议在安装期间创建它,或在第一次执行后注册后退出应用程序:

This might not solve your problem but there is something to mention:

You can not use an EventLog just after having it registered because the system needs some time to refresh the list of registered sources. Therefore, it is recommended to create it during installation or exit the application after registering upon the first execution:
Quote:

在安装应用程序期间创建新的事件源。这允许操作系统有时间刷新其已注册事件源列表及其配置。如果操作系统没有刷新其事件源列表,并且您尝试使用新源编写事件,则写入操作将失败。

Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail.

因为您可能已经多次启动了服务,所以源应该具有已经注册。但是在其他机器上安装时应该注意这个事实。



也没有必要设置 Log 设置 Source 时的属性:

Because you have probably started your services multiple times meanwhile, the sources should have been registered. But you should note this fact when installing on other machines.

There is also no need to set the Log property when the Source has been set:

Quote:

当您编写日志条目时,系统使用Source查找放置条目的相应日志

When you write a log entry, the system uses the Source to find the appropriate log in which to place your entry

设置日志属性用于读取日志文件。



来自MSDN EventLog页面的所有引用(例如 EventLog.CreateEventSource方法(字符串,字符串)(System.Diagnostics) [ ^ ])。



你不喜欢获取一个服务写入的日志条目,并在停止该服务时出错(请显示我的评论中已建议的完整消息)。这可能也是由与日志记录无关的问题引起的。

Setting the Log property is provided for reading log files.

All quotes from the MSDN EventLog pages (e.g. EventLog.CreateEventSource Method (String, String) (System.Diagnostics)[^]).

You don't get log entries written by one service and got an error when stopping that service (please show the full message as already suggested in my comment). That may be also sourced by a problem not related to the logging at all.


这篇关于如何将多个应用程序(源)中的事件写入同一事件日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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