2007-04-10

 

.Net2.0下,Windows Service程序Log无法输出的问题

这两天遇到了这个问题,最后发现,是路径的问题。
以下是我的配置:
<system.diagnostics>
<sources>
<source name="DefaultSource" switchname="DefaultSwitch">
<listeners>
<add name="FileLogListener">
<!--<add name="EventLog">-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information">
</switches>
<sharedlisteners>
<add name="FileLogListener"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
initializeData="FileLogListenerWriter"
location="Custom"
customlocation="LOG\" />

<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializedata="APPLICATION_NAME"> -->
</sharedlisteners>
</system.diagnostics>
其中,红色部分为LOG输出路径,我写的是相对路径。
运行Windows Service后,我在EXE所在路径下去检查LOG文件,但不存在。这使我误以为在Windows service下无法使用my.Application.Log,为解决这个问题走了很多弯路。后来,查了MSDN,上面明确提到支持Windows service,而且配置文件,除了使用绝对路径外,其它都相同。于是我也改成绝对路径,测试后发现,确实能输出LOG。
那为什么用相对路径无法输出呢?还是输出到其它路径下去了?
我运行Process Explorer查看windows service进程,发现它打开了文件:%SystemRoot%\system32\LOG\myService.log
这样看来,当采用相对路径时,输出目录是相对于程序的工作目录,而不是可执行文件所在目录。一般的windows application的工作目录,与可执行文件所在目录一致,而windows service由于是系统目录system32下的svchost.exe加载的,它的工作目录也就成了系统目录system32。

Labels: ,


Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]