开发者

Can't figure out how to add logging to my windows service!

开发者 https://www.devze.com 2023-01-16 13:48 出处:网络
I\'ve been messing with this all 开发者_如何学Goday 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/

I've been messing with this all 开发者_如何学Goday 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";


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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号