在日志文件中写入的 Windows 服务权限

问题描述:

我使用 C# 创建了一个 Windows 服务,该服务在 C:\ 中写入日志(使用 NLog)程序数据.
当我调试服务(在我的 Windows 7 上)(使用上面编写的代码)时,正确创建了日志文件并正确写入了日志记录.但是当我在运行于 Windows server 2008 (x86) 的服务器上安装该服务时,没有创建日志文件(我还检查了 C:\Windows\System32\,什么都没有).

I have created a Windows service using C# that writes logs (with NLog) in C:\ProgramData.
When I debug the service (on my Windows 7) (using the code written above), the log file is correctly created and log records correctly written. But when I install the service on my server which runs on Windows server 2008 (x86), no log file is created (I have also checked in C:\Windows\System32\, nothing there).

我怀疑是授权问题,如何知道我的服务使用了哪些权限?

I suspect that it is an authorization problem so how can I know what rights is my service using?

PS: 我已经使用命令行安装了我的服务 C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\PathToMyService\MyService.exe 具有管理员权限.

PS: I have installed my service using the command line C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\PathToMyService\MyService.exe with Administrator rights.

运行 services.msc,找到您的服务,右键单击并选择 Properties.检查 Log On 标签以查看您的服务在哪个帐户下运行.

Run services.msc, find your service right click and select Properties. Check the Log On tab to see which account your service is running under.

至于您的问题,默认情况下,我认为大多数服务在 Local System Account 下运行,我认为该帐户具有写入 C:\ProgramData 目录的权限.您确定 nlog.config 文件与您的服务一起部署了吗?

As for your problem, by default I think most services run under the Local System Account which I would assume has permissions to write to the C:\ProgramData directory. Have you made sure the nlog.config file is deployed with your service?