简体   繁体   中英

Logging Windows Service

I have the following code for logging my windows service which I found through research online. its done inside of my service class right before it is being initialized like below.

public GBBInvService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MyLogSource"))
                System.Diagnostics.EventLog.CreateEventSource("MyLogSource",
                                                                      "MyDoLog");
            eventLog1.Source = "MyLogSource";
            eventLog1.Log = "MyDoLog";
        }

code for when logging:

eventLog1.WriteEntry("GBBInvService Service Started");

I have however been advised against it by our consultant. His comments below:

I'd avoid having the service create event sources at runtime. This requires the service to run with high privileges (ie more access to the computer than is really necessary). Setting up the Windows event log sources is really an install-time job. You may be able to add an event log installer to the project installer file (along with the service installer) and then the event source will always exist

The problem with this advise is that I have not been able to find any examples where the logs are created in the project installer files. I also tried moving this log creation portion into my project installer but then it won't let me call or write to eventlog1 from my web service cs page. He has also suggested log4net but that is something that is new to me and pretty complicated to grasp a hold of. I'm still very new to windows services having just completed my first windows service project and would be very grateful for any sort of direction for creating logs in project installer, writing to it from my service cs page or any heads up on log4net.

Your consultant is correct, it is a bad idea to do that within the service.

Here's how you can create the event source in the installer:

http://blogs.msdn.com/b/helloworld/archive/2008/12/11/creating-an-event-log.aspx?Redirected=true

Short version: Subclass Installer, and create the event log sources in the constructor of your subclassed installer.

UPDATE

From the link:

After the service installer is executed, the log is 'registered', but not created yet. To create it, one event must be written. If the service runs using a restricted user account, that account may not have enough security permission to write the first log, as the log need to be created.

The example code does not show one event being written. Be sure you are doing that.

When installing the service, the user must run the installer as Administrator

Make sure you ran the installer as Administrator.

From your comment:

Service wouldn't start.

Look in the Event Log for a reason it is not starting. Perhaps it is throwing an Exception, eg if you did not write the one event in the installer or if you did not run the installer as Administrator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM