Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
    }
}


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");
    }
}


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 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.
Posted
Updated 12-Sep-17 1:01am
v2
Comments
Jochen Arndt 12-Sep-17 6:36am    
Please add the text from the error message to your question using the green 'Improve question' link.

Many here will not open external (image) links and your links are generating a 502 Bad Gateway error page.
AksPat 12-Sep-17 7:01am    
Removed the links and updated the question.

1 solution

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.
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:
When you write a log entry, the system uses the Source to find the appropriate log in which to place your entry
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900