Service that writes to the application Event Log

  • Thread starter Thread starter Aaron Hackney
  • Start date Start date
A

Aaron Hackney

Hello. I am writing a service that I would like to have write access to
the Application event log. Since the service is running as a system
account, it would appear that I do not have access. (Security Error).

I am following the MSDN howto and it does not appear to have any
additional requirements in their howto.

If I run this as an application under my credentials (local admin) it
runs just fine.

I have a feeling this is realted to XP SP2 or Perhaps .Net 1.1

Advice?
Thanks in advance!
-Aaron
 
Hi,


Aaron Hackney said:
Hello. I am writing a service that I would like to have write access to
the Application event log. Since the service is running as a system
account, it would appear that I do not have access. (Security Error).

I do not think so, IIRC everybody can write in the log

Post your code just to be sure it's correct.
I have a feeling this is realted to XP SP2 or Perhaps .Net 1.1

Not at all, the problem should be in something else you are trying to do,
post your code !
 
You should only need admin rights to create a new log or log source. If
you are writing to an existing log with an existing log source, it
should work. If you need to create a new log or source, it is best to do
it during deployment by making an Installer class (which you can execute
using InstallUtil.exe). Do not require your application to run as an
admin just to create the event log/source.
 
Ignacio said:
Hi,





I do not think so, IIRC everybody can write in the log

Post your code just to be sure it's correct.



Not at all, the problem should be in something else you are trying to do,
post your code !
I've muched with the code a bit but I was following directions from MSDN
here:
http://msdn.microsoft.com/library/d...kthroughcreatingwindowsserviceapplication.asp

Thanks much
-Aaron

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace MyNewService
{
public class MyNewService : System.ServiceProcess.ServiceBase
{
private System.Diagnostics.EventLog eventLog1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public MyNewService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MyNewService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// eventLog1
//
this.eventLog1.Log = "System";
this.eventLog1.MachineName = "skinner";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}

protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}


}
}
 
I found my problem/answer. By default loggin is turned on and allow
without registering or creating a source.

I just needed to use the static methods provided such as
EventLog.WriteEntry instead of creating an instance of
System.Diagnostics.EventLog()

*DOH*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top