Search code examples
c#.netserviceevent-log

Create a customized EventLog logname


I am trying to create my customized EventLog for my service. I was able to customize my source but not not the logName by using CreateEventSource method. In my service constructor:

        public Service1()
        {
            InitializeComponent();
            eventLog1 = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }

            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

I was able to customize MySource to whatever I want, but whenever I change MyNewLog, my service crashes because this eventLog1 is writing specifically to MyNewLog in the Event Viewer. This article shows you how to check if a EventLog Exists and how to Delete it. But, how do I CREATE a customized EventLog?


Solution

  • I finally found out how to create a new EventLog inside Applications and Services Logs. According to msdn,

    The source cannot be registered because it already exists on the local computer.

    So it was my mistake that I didn't know you have to create a NEW source when you create a new log, that is to say, you cannot have an existing source inside a new log.