C# 为什么我的 Windows 服务不会写入我的日志文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why won't my windows service write to my log file?
提问by Mike Roosa
I have a windows service and use nlog for logging. Everything works fine when I run from the visual studio ide. The log file updates with no issues. When I install the service, the service runs fine but the log file never updates. I am running under LOCAL SERVICE if that helps. Yes, I have created the logs directory under my application folder.
我有一个 Windows 服务并使用 nlog 进行日志记录。当我从 Visual Studio ide 运行时,一切正常。日志文件更新没有问题。当我安装该服务时,该服务运行良好,但日志文件永远不会更新。如果有帮助,我将在 LOCAL SERVICE 下运行。是的,我已经在我的应用程序文件夹下创建了日志目录。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/logs/${shortdate}_info.txt"
layout="${date} ${logger} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" maxlevel="Info" writeTo="file" />
</rules>
</nlog>
采纳答案by Michael Meadows
Your local service account doesn't have access to write to the file location specified. You set it to use a system account in the "Log On" tab of the service properties dialog, or you can set up the user account as part of the setup process.
您的本地服务帐户无权写入指定的文件位置。您可以在服务属性对话框的“登录”选项卡中将其设置为使用系统帐户,或者您可以在设置过程中设置用户帐户。
回答by Eoin Campbell
Have you tried install/run your service as a different named user.
您是否尝试过以不同的命名用户身份安装/运行您的服务。
If that works, then you can be pretty sure you've a permissions issue where your Local system account doesn't have permission to write to the directory/file.
如果可行,那么您可以非常确定您遇到了权限问题,即您的本地系统帐户无权写入目录/文件。
回答by Richard
You can use Process Monitorto look at the file operations being performed, and why they are failing.
您可以使用Process Monitor查看正在执行的文件操作以及它们失败的原因。
I would suspect (along with other answerers) that this is a permission problem, with the service's account not having sufficient access to the file.
我怀疑(以及其他回答者)这是一个权限问题,服务的帐户没有足够的权限访问该文件。
回答by genki
Just out of curiousity, have you checked whether anything is being written in the system32 directory of your windows installation? Iirc, that's the default application runtime base directory for services...
出于好奇,您是否检查过 Windows 安装的 system32 目录中是否写入了任何内容?Iirc,这是服务的默认应用程序运行时基目录...
回答by Tim Clem
I've had this issue too. As mentioned by genki you are probably logging into the \Windows\System32 directory. Maybe check for the log file you are expecting there first. When writing services I've often put a line like this in the beginning to get the current directory to behave like a normal application
我也遇到过这个问题。正如 genki 所提到的,您可能正在登录 \Windows\System32 目录。也许首先检查您期望的日志文件。在编写服务时,我经常在开头放置这样的一行,以使当前目录像普通应用程序一样运行
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
回答by Stefan Cvetanovski
If you are using x64 version of Windows than the log file is saved in C:\Windows\SysWOW64 folder
如果您使用的是 x64 版本的 Windows,则日志文件将保存在 C:\Windows\SysWOW64 文件夹中
This is the default case if you build your project using the AnyCPUconfiguration and deploy to a 64 bit operating system.
如果您使用AnyCPU配置构建项目并部署到 64 位操作系统,则这是默认情况。
回答by peterincumbria
I have just had the same problem with Enterprise framework logging.
我刚刚在企业框架日志记录方面遇到了同样的问题。
To conclude this question of which the Answers together tell the correct story.
总结这个问题,答案一起讲述了正确的故事。
In your example when using the Visual Studio IDE the log file is being written using the application's user permissions and the log file is being written.
在您使用 Visual Studio IDE 的示例中,正在使用应用程序的用户权限写入日志文件,并且正在写入日志文件。
The Windows Service does not have these same permissions so the log file will not get written. Windows Service does have permission (I have tested this) to write to the
Windows 服务没有这些相同的权限,因此不会写入日志文件。Windows 服务确实有权限(我已经测试过)写入
AppDomain.CurrentDomain.BaseDirectory
using System.IO namespace.
使用 System.IO 命名空间。
So direct the log file to this base directory and you will be safe.
因此,将日志文件定向到此基本目录,您将是安全的。
回答by Aykut ?evik
It's maybe your service is running under an other user context and also because of Windows restrictions. I had the same issue and solved it logging into the following folder:
可能您的服务正在其他用户上下文下运行,也可能是因为 Windows 限制。我遇到了同样的问题并解决了它登录到以下文件夹:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Maybe this will help you.
也许这会帮助你。
回答by bressain
I found this post very helpful when I had the same problem:
当我遇到同样的问题时,我发现这篇文章非常有帮助:
http://nlog-forum.1685105.n2.nabble.com/Nlog-not-working-with-Windows-service-tp6711077p6825698.html
http://nlog-forum.1685105.n2.nabble.com/Nlog-not-working-with-Windows-service-tp6711077p6825698.html
Basically, you'll want to include ${basedir} as part of your file location in your config. This will make NLog start at where your executable is running from.
基本上,您需要在配置中包含 ${basedir} 作为文件位置的一部分。这将使 NLog 从您的可执行文件运行的位置开始。
回答by jwatts1980
After creating an installation project for my service, and installing it multiple times, I finally realized that I had not included the NLog.config file as one of the files to install. Now that it is included alongside the executable, it's working perfectly.
在为我的服务创建了一个安装项目,并安装了多次之后,我终于意识到我没有将 NLog.config 文件作为要安装的文件之一。现在它包含在可执行文件旁边,它运行良好。
For what it's worth, the NLog.config file can be manually added after the fact, but the service may need to be stopped and restarted.
无论如何,NLog.config 文件可以事后手动添加,但可能需要停止并重新启动服务。