How to write events from multiple applications(sources) to the same event log?

1.4k views Asked by At

I have created a custom event log and would like all my applications to write to the same event log. As you can see below in the image attached, DistributedCOM and Svc Ctrl Mgr are 2 sources writing to the same event log System.

Multiple sources under the same log file

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.

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

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.

EDIT: Also, when I try to stop the services, Service 1 fails to stop and throws an error. Image given below.

enter image description here

EDIT 2: Just changed the constructor of the EventLogger class as below and then it worked!! I am not entirely sure if this was the actual cause for the improper functioning. And I'm not quite sure if it had anything to do with the setting of the Log property either. Any light thrown on this by any one of you would be appreciated. I would like to understand better as to what exactly happened here. Thanks.

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

There are 0 answers