Pooja
Pooja

Reputation: 495

How to create a custom event log using C#

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?

Upvotes: 4

Views: 28134

Answers (3)

Afshin
Afshin

Reputation: 1242

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

Upvotes: 6

Mustafa Unal
Mustafa Unal

Reputation: 51

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

Upvotes: 5

mmcfly
mmcfly

Reputation: 876

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.

Upvotes: 1

Related Questions