How to create a custom event log using C#
22,183
Solution 1
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).
Solution 2
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";
Author by
Pooja
Updated on April 18, 2021Comments
-
Pooja about 3 years
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?
-
Pooja over 12 yearsi 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
-
Mhmd almost 12 yearsbe aware that logName must have a unique first characters, check this link msdn.microsoft.com/en-us/library/y7x55536%28v=vs.90%29.aspx
-
NDym about 4 yearsI just spent 5 hours .... Tnx MS i read entire documentation is not mention this little thing........... @Mustafa Unal solution work for me