tags:

views:

59

answers:

3

I've been messing with this all day and have still not found a solution. I have created a windows service that is supposed to log to it's install directory (Program Files/Service/LOGS/)

But when I run my service it doesn't do what I would expect it to do and it doesn't log anythying. I'm having a hell of a time figuring out what the problem is without any sort of logging. Below is the class I am using for logging, can anyone see what I am doing wrong? I have even tried giving "Everyone" all permissions on the LOGS folder to see if that was the issue (but no luck).

public class Logging
{
    public static void Log(string message)
    {

        string logFile = "LOGS/" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
        if (!Directory.Exists("LOGS"))
        {
            Directory.CreateDirectory("LOGS");
        }

        File.AppendAllText(logFile, message);

    }
}

The service successfully starts and there are no errors in my Windows Event Viewer...

UPDATE:

I have changed my log path to the following and am still having no luck:

string logFile = @"F:\LOGS\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
+1  A: 

Have you tried calling Directory.GetCurrentDirectory and writing the name of the current directory to a file (using an absolute path, with permissions configured so that "Everyone" has "Full control" of the target folder). That way you can find out if it is writing where you think it should be writing.

Also important: When you service is running, what account is it using to run? If the account used to run the service does not have privilegies to write at Program Files/Service/LOGS/, that could be the reason for your failure to see anything written there.

And, of course, you could also use write to the event log, instead of writing in to the file system ( http://support.microsoft.com/kb/307024 ). IMO that is the recommended way of doing this kind of thing.

Luxspes
+4  A: 

Well, for starters, you should not be writing anything to a folder in the Program Files directory. This is bad and will fail on Vista, Windows 7, or Windows Server 2008 and above.

Secondly, your code isn't actually specifying what you think it is. The service's "current directory" is not it's program files directory. It's probably something like the users home directory that it runs under, or Windows\system32. Again, the app shouldn't have write persmissions there either. Instead, you should be loggint to somewhere like the Program Data subdirectires.

Mystere Man
Wouldn't this show in the windows Event Viewer if it were the problem?
Abe Miessler
What do you mean by `the Program Data subdirectiries`? I updated my code to use a directory not in my Program Files directory but still no luck. Any suggestions?
Abe Miessler
Well, ProgramData is different depending on which OS you use, which is why you use the `Environment.GetFolderPath(Environment.SpecialFoder.ApplicationData)` Method.
Mystere Man
Is that better than using something like `F:\LOGS`?
Abe Miessler
As I said above, .NET has special rules and security issues when dealing with network drives. If F: is a local drive, then you can do that, but if it's a network drive, then extra hoops have to be jumped through.
Mystere Man
It's a local drive.
Abe Miessler
+1  A: 

So you are now writing to F:\LOGS\, but:

  • What account are you using to run your service? Is that account able to write to F:\LOGS\?
  • Also, Did you configure that folder so that "Everyone" has "Full control" of it?
Luxspes
1) Does it matter is Everyone has full control of that folder? 2) Yes, Everyone has full control.
Abe Miessler
Not sure, but maybe you also need to change the permissions of F:\ so that your service is able to check if the directory exists.
Luxspes
I would recommend you to configure your service to run as your current user account, if it works that way, then you can be sure that it is a filesystem permissions problem
Luxspes
But doesn't giving "Everyone" full permissions on the folder already assure us that it's not a permissions problem?
Abe Miessler
Also, could you update all your code in your question? because if you only changed the asignment to the logFile variable, that means the rest of you code (for example the line with "if (!Directory.Exists("LOGS"))" is still looking for the LOGS directory at Program Files
Luxspes
Either you change all your references to LOGS to be absolute, or you can use Directory.SetCurrentDirectory to change the current directory.
Luxspes
+1, I think you're on the right track. I updated the `Directory.Exsits` section of my code and now I'm getting a `The process cannot access the file` exception but i'm not sure what else would be accessing the file. I have already given `Everyone` all permissions...
Abe Miessler
Then it is time to configure your service to run as your current user account
Luxspes
What difference does that make if `Everyone` has full permissions?
Abe Miessler
Both F:/ and LOGs have Everyone with full permissions?
Luxspes
Also, there could be other restrictions in the account that is being used to run the service (Code Access Security: http://msdn.microsoft.com/en-us/library/930b76w0%28VS.80%29.aspx ). The account used to run the service, could for example, be forbidden from interacting with the FileSystem ( http://msdn.microsoft.com/en-us/library/system.security.permissions.fileiopermission.aspx )
Luxspes
Everyone is not Everyone. Everyone does not include, for example, NETWORK_SERVICE, Anonymous User, etc.. see http://support.microsoft.com/kb/278259
Mystere Man