Windows Service Error/Log File Location.

PhillD

Well-known member
Joined
Mar 5, 2010
Messages
91
Programming Experience
10+
Before I post about my problem, please consider the following: If you are having problems with a windows application (especially a commercial app that run's as a service on a server), where would you look for the error/log file?

Ok, so yes, I have written a windows service that run's on a Windows 2008 server. I need to log errors/program activity to a text file. I would like to store the text file in the Program Files directory with my application (and let's be honest, is where any tech or any programmer would expect to find it). So given the issues with UAC, how can I save the log file to the program files directory?

Before anyone lectures me about not writing to Program Files folder, MS SQL stores its own database files (and log files) inside the program files folder which are written to constantly. How about that for not following your own advice???
 
If you are having problems with a windows application (especially a commercial app that run's as a service on a server), where would you look for the error/log file?
For a service I would first look in the Windows event logs. Check out EventLog Class (System.Diagnostics)
 
I'm already logging to the event log for critial application errors and failures. I need to store a detailed list of records that are being processed and the action the service took including any resulting error. Each night, this will equate to approx 600-800 entries in the text file. I don't want to store all that in the event viewer.
 
You wouldn't look for an event/log file in the program data directory (you JUST wouldn't). I should be able to store this in the program files directory with my application.
 
Data files should be put in one of the applications data folders. SpecialFolder enumeration can specify places like ApplicationData/CommonApplicationData/LocalApplicationData.
 
ApplicationData folders are user specific and this is a windows service (hence a specific user is inapplicable). Your statement about data files is inaccurate, in addition to my example of MS placing SQL data files inside the program files folder, I also looked at a program called syslogd which is installed on my own PC. Inside the Program Files\syslogd folder is a text file called errorlog.txt

I'm not going to accept the fact that you shouldn't store an error log file in the respective program files folder. This has been COMMON practice since Windows 98. Besides, it is obvious that other developers (writing in non .NET languages) are still doing it.
 
ApplicationData folders are user specific and this is a windows service (hence a specific user is inapplicable).
CommonApplicationData is common, but only admin user can write here.
There is CommonDocuments that I believe all users can write to.
help said:
CommonApplicationData: The directory that serves as a common repository for application-specific data that is used by all users.
in addition to my example of MS placing SQL data files inside the program files folder
I can't exactly recall, but maybe it is possible to edit content of existing files and not create new files in that folder under UAC ? Maybe something you can look into. Anyway, it is usually easier to play ball with the new security systems than trying to fight/circumvent them.
This has been COMMON practice since Windows 98
A lot has changed about common practice since security were introduced with Vista and up.
 
That is where I am currently writing the file to (= Program Data on Server 2008), but I guarantee that if I asked one of my tech's to locate the error log, they wouldn't be able to find it. I can really test that if you want? My point is, if the logfile is not easy to find, then how would anyone (other than me) know that it exists. I suppose I could write another entry to the windows event viewer with the location of the log file. But you have to admit that normally, you would expect to find the log file in the program files directory; and in a lot of cases, still do.
 
Last edited:
Log files are not necessarily supposed to be easy to find. They are considered by most to be an advanced feature. Think of all the various obscure Windows log locations!

If someone needs to look in a log file then they probably have a bigger problem and would ask or check your documentation. On top of that, even if they find the log file, would an end user understand it? My point is, they would need help either way.

That said, I have a commercialy sold application that is a Windows service, and it writes logs to a ProgramData folder. I then have a log viewer feature in my gui support application that allows the user to view the logs. You could write a quick app that just shows your logs if it really is that important.

Richard

But you have to admit that normally, you would expect to find the log file in the program files directory; and in a lot of cases, still do.

Anyone who has written code for Windows since UAC should understand that this is no longer a valid expectation.
 
Richard,

According to the technicians who work at my company, they expect to find the log files in the program files directory. I have been writing .Net Applications and services for the past 4 years and this is the first time I've written something where I needed to track an extensive log of events of application activity. I probably wouldn't think to look in the program data folder for a event log file in 6 months time. What if I needed to move files around the hard drive? What if I absolutley had a genuine need to write files to the program files folder. Your telling me .NET could not do this? What a weakness!!!!
 
Richard,

According to the technicians who work at my company, they expect to find the log files in the program files directory. I have been writing .Net Applications and services for the past 4 years and this is the first time I've written something where I needed to track an extensive log of events of application activity. I probably wouldn't think to look in the program data folder for a event log file in 6 months time. What if I needed to move files around the hard drive? What if I absolutley had a genuine need to write files to the program files folder. Your telling me .NET could not do this? What a weakness!!!!

You can do anything you want on your system. All I am saying is that writing "Program Data" to the "Program Files" folder is not a recommended practice since the advent of UAC. If only for the basic fact that Windows users "by default" now have Read-Only access to the "Program Files" folder and would need elevated permissions to allow your application to write a log file there.

There are several decent articles that can help:
Where Should I Write Program Data Instead of Program Files? - The App Compat Guy - Site Home - MSDN Blogs

Mathew's diary of Microsoft Technologies: Making .Net Applications UAC compliant (Vista,Server 2008, etc)

Trust me... I feel the pain. I've been coding since DOS 2.7 and I went through this too. As I said, its your system and, if it is within your network (ie you modify permissions) then you can do whatever you want, but if you were ever going to distribute software you need to think about storing Application Data in the (new) proper locations.

Richard
 
Back
Top