Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My application is installed as windows service.
For log on file, I use the following class :
C++
debugTextListener = gcnew TextWriterTraceListener("c:\\myApp.log.txt");
...
debugTextListener->WriteLine( "app starts.");
debugTextListener->Flush();

If I use my application as program, log file is filled correctly;
If I use it as service, It run correctly, but no log file is created.
Any Idea ?
Best regards.
Posted

It depends under which credentials the windows service is running. It may well be that the account the windows service is running under does not have sufficient rights to write to the root folder of C:. If possible try to let the service run under the same account when you ran it as a program and see if that will help fix the issue. Then it is up to you to decide to either change the ACL on C:\ or have the service run under an account that has sufficient rights to write to C:\.

Regards,

Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Feb-12 13:38pm    
Correct explanation, but what would be the positive advice?
Certainly not hard-coding the path name and not using "C:\" and changing ACL (which is possible but why?)
I voted 4 this time.

The whole approach of logging is wrong. I advised exactly what to do in my answer, please see.
--SA
As Manfred explained, the server usually don't have permission to write to this location. The locations like that are simply illegal; you should use appropriate directory under "Users" or "Documents and Settings". Also, there are no cases when any hard-coded path name could be useful, ever. All path names are always calculated during run-time, based on assembly location (for read-only files only), environment of configuration files.

But you don't need to do even that. For logging, use the system log via the class System.Diagnostics.EventLog, http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

For some more useful detail and idea, please see my past solutions:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

This way, you can make logging structured per you project, redirect it, etc.

—SA
 
Share this answer
 
Hi,

This means that the service doesn't have sufficient privileges to
create the log file. Try giving admin privilege to service.
Try one of the following.

1. Edit the manifest to make sure that the service will run only with admin privileges.
<requestedExecutionLevel
level="asInvoker|highestAvailable|requireAdministrator"
uiAccess="true|false"/>
2. Lookup the current privilege, and then adjust it to give admin privileges.
LookupPrivilegeValue() and AdjustTokenPrivileges() will do the trick.


Thanks
 
Share this answer
 
v2
Comments
Foulques NERA 10-Feb-12 4:08am    
Now it's working.
I think there is a combination between :
- changing service "connection account" to Local account (which was not the default setting);
- changing "system time update" privilege;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900