views:

614

answers:

4

Hi community,

I have this simple code that records appends a log to a text file:

    public static void RecordToFile(string filename, Log log)
    {
            TextWriter textWriter = new StreamWriter(Constants.APP_PATH + 
                "\\" + filename, true);
            textWriter.WriteLine(log.ToString());
            textWriter.Close();
    }

This works perfectly in a Windows Forms application. However, using the instsrv and srvany trick, I made this a Windows Service. The service runs fine, accesses the database, performs queries and all... Except for this StreamWriter. The log just doesn't get updated as it should. Any ideas why?

+6  A: 

Most likely the service is running under user credentials that does not have access rights to that directory.

So check the properties dialog for the service, and check the Log On tab to see what it logs on as.

Lasse V. Karlsen
The best way to find out what's going on is have the service log any exceptions to the windows event log. This is a pretty easy thing to do in .Net
Chris Lively
Yes, well, except if the service doesn't even have rights to do that, I've seen that happen as well.
Lasse V. Karlsen
I like to use Process Monitor for debugging file rights problems. It's saved me tons of time in the past with IIS or service issues. It can monitor all file accesses by process, and it will tell you about any failed access along with the path that was used. this used to be a sysinternals tool, but now it's on Microsoft's site. You can find it here: http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
JMarsch
nice link, thanks
m_oLogin
+2  A: 

Possible Reasons:

  1. Constants.APP_PATH is pointing to a mapped drive - services don't run in the same environment as a logged-in user, so the path may not be valid
  2. Permissions - depending on what user the service is running as, it may not have access to the same set of directories that the WinForms app did
Harper Shelby
+1  A: 

Without more information there isn't much that anyone can help with. In what way, exactly, does it not function? Do you get an exception? Does it just fail silently?

Just a couple of tips...

1) Don't use string concatenation to create file paths. Use System.IO.Path.Combine instead. Like this:

TextWriter textWriter = new StreamWriter(
    System.IO.Path.Combine(Constants.APP_PATH, filename), true);

2) Enclose your writer in a using() block. Like so:

using(TextWriter textWriter = new StreamWriter(
        System.IO.Path.Combine(Constants.APP_PATH, filename), true))
{
    textWriter.WriteLine(log.ToString());
}

3) Verify that the account the service is using has access to create/overwrite files in that directory. Often service accounts like LOCAL_SYSTEM or NETWORK_SERVICE aren't going to have the same permissions as user accounts would. This could explain why it works as a user but not as a service. It could also be that your APP_PATH constant points to something user specific, like a drive mapping to a network share. Drive mappings don't span users, so this could also be an issue.

Adam Robinson
thanks for the tips. I'm still trying to solve the problem
m_oLogin
A: 

Without any more information I'd guess that the account your service is running under doesn't have rights to open the file for writing.

Sean