4

I created a Windows service. I create an event log.

public Service1()
{
        InitializeComponent();
        this.ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");

        string sourceName = ConfigurationManager.AppSettings.Get("ServiceName");
        string logName = ConfigurationManager.AppSettings["EventLogName"];
        try
        {
            if (!System.Diagnostics.EventLog.Exists(sourceName))
                System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
            eventLog.Source = sourceName;
            eventLog.Log = logName;
        }
        catch
        {
            eventLog.Source = "Application";
        }
    }

During initialization, the service is installed and log is not created. The log entries are in the Application log of the system.

What did I miss?

I used process installer to installation

 public ProjectInstaller()
 {
        InitializeComponent();
        this.Installers.Add(GetServiceInstaller());
        this.Installers.Add(GetServiceProcessInstaller());
 }

 private ServiceInstaller GetServiceInstaller()
 {
        serviceInstaller.ServiceName = GetConfigurationValue("ServiceName");
        serviceInstaller.Description = GetConfigurationValue("Description");
        serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
        return serviceInstaller;
 }

 private ServiceProcessInstaller GetServiceProcessInstaller()
 {
        serviceProcessinstaller.Account = ServiceAccount.LocalSystem;
        return serviceProcessinstaller;
 }

How to create event log?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pooja
  • 495
  • 3
  • 9
  • 25
  • Possible duplicate of [Write to Windows Application Event Log](https://stackoverflow.com/questions/25725151/write-to-windows-application-event-log) – Liam Jun 12 '19 at 14:58

3 Answers3

6

change your code to following:

if (!System.Diagnostics.EventLog.SourceExists(source: sourceName))
{
    System.Diagnostics.EventLog.CreateEventSource(source: sourceName, logName: logName);
}

Note that as per Microsoft's KB, the first 8 characters of the Event Log name must be distinct from all other Event Logs on the computer (so if the user's computer already has a log named "Application" then you cannot create a new EventLog named "Applicat1" or "ApplicationFoobar" as they share the same 8 characters as the built-in Application event-log).

Dai
  • 141,631
  • 28
  • 261
  • 374
Afshin
  • 1,222
  • 11
  • 29
  • 2
    i tried your code. but it didnt create the event log and cannot start the service. the service shows Service cannot be started. System.ArgumentException: The source 'SyncronizationService' is not registered in log 'SyncronizationLog'. (It is registered in log 'Application'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property. error message – Pooja Jan 06 '12 at 07:44
  • 2
    be aware that logName must have a unique first characters, check this link http://msdn.microsoft.com/en-us/library/y7x55536%28v=vs.90%29.aspx – Mhmd May 16 '12 at 17:06
4

ServiceName and Source must be different name.

ServiceName

this.serviceInstaller1.ServiceName = "MaliyeWMService";

Source

if (!System.Diagnostics.EventLog.SourceExists("MaliyeMailService"))
{
  System.Diagnostics.EventLog.CreateEventSource("MaliyeMailService", "MaliyeMailServiceLog");

}
OlayLog.Source = "MaliyeMailService";
Companjo
  • 1,789
  • 18
  • 24
  • I just spent 5 hours .... Tnx MS i read entire documentation is not mention this little thing........... @Mustafa Unal solution work for me – NDym Mar 15 '20 at 21:08
0

Try setting the AutoLog to false first.

    this.AutoLog = false;

    if (!System.Diagnostics.EventLog.SourceExists(sourceName)) 
    {        
        System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
    }

    eventLog.Source = "MySource";

In this MSDN walkthrough, they seem to completely omit that step. However, in this MSDN "How to:" they state:

If you want to write to an event log other than the Application log, you must set the AutoLog property to false, create your own custom event log within your services code, and register your service as a valid source of entries for that log.

After I set the AutoLog to false, my custom log and events showed as expected.

mmcfly
  • 834
  • 8
  • 12