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?

1

3 Answers 3

7

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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
be aware that logName must have a unique first characters, check this link msdn.microsoft.com/en-us/library/y7x55536%28v=vs.90%29.aspx
5

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

1 Comment

I just spent 5 hours .... Tnx MS i read entire documentation is not mention this little thing........... @Mustafa Unal solution work for me
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.