我能够使用log4net和Cassini/IIS dev服务器写入日志文件,但当我使用IIS7.5时,无法将其写入到文件中。
最初,我遇到了安全异常,因此我添加了requirePermission="false"
,异常消失了,但没有创建文件。
根据IISM的说法,信任级别是完整的。
我无法在自己的机器上使其工作,我想知道当我转移到ISP(discountASP)会发生什么。
下面是log4net的设置:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<encoding value="utf-8" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
C#
log4net.Config.XmlConfigurator.Configure();
ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Log.Info("This is a test");
有什么线索吗?ASP.NET 3.5, VS2008, Windows 7, IIS7.5, log4net 1.2.10
编辑:
我在Cassini中运行的测试Web应用程序,现在在IIS7.5上运行,并且它可以正常工作,因此有些特定于我的Web应用程序会阻止log4net正常运行。除了将每个部分添加到测试应用程序之外,有没有更好的方法来找出是什么导致log4net无法运行?
更新:
我使用AdoNetAppender避免文件权限问题,但仍然得到相同的结果。AdoNetAppender可在Cassini和IIS上运行的测试应用程序中工作,但不能在我的Web应用程序中工作。出现以下异常:
System.Security.SecurityException:请求类型为“System.Configuration.ConfigurationPermission,System.Configuration…”的权限失败。
更新2:
我错误地认为在IIS7.5中测试的Web应用程序fileAppender可以工作。情况是:测试Web应用程序的fileAppender和AdoDotNetAppender都可以在Cassini/IIS dev中工作,但是不能在IIS7.5中工作。因此,我认为问题在于IIS,而不是我的Web应用程序。
请注意。我将VS2008作为管理员运行,但未以管理员身份登录Windows 7。此外,我正在运行Windows 7 Home Premium而不是Professional。
我授予NETWORK SERVICE完全权限访问Web根目录,但仍未创建文件。也已将EVERYONE授予完全权限,但仍未创建文件。
由于adoDotNetAppender也无法工作(但在dev IIS中可以),因此我认为除了文件权限之外可能还存在其他问题。
更新3:
我已为IIS7上的FileAppender解决了问题。如果我添加以下内容:
<identity impersonate="true"
userName="zzz"
password="yyy" />
如果用户是管理员,则可以正常工作。如果是我这样不是管理员的用户,则无法工作。因此,这是一个权限问题。但是我之前已经将该目录的权限授予了所有人,并且仍然无法正常工作,所以肯定存在其他的权限设置。同时,虽然使用模拟用户后 FileAppender 能够工作,但 AdoNetAppender 在 IIS7 中仍然无法工作。我尝试添加:
<securityContext type="log4net.Util.WindowsSecurityContext">
<userName value="zzz" />
<password value="yyy" />
<domain value="aaa" />
</securityContext>
我已将AdoNetAppender部分添加到配置文件中,但仍然无声失败。
我为任何能帮助我在IIS7.5上使AdoNetAppender工作的人添加了悬赏。
更新4:
我终于得到了堆栈跟踪。以下是它:
log4net:ERROR [AdoNetAppender] Failed in DoAppend
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at log4net.Util.LogicalThreadContextProperties.GetProperties(Boolean create)
at log4net.Core.LoggingEvent.CreateCompositeProperties()
at log4net.Core.LoggingEvent.CacheProperties()
at log4net.Core.LoggingEvent.FixVolatileData(FixFlags flags)
at log4net.Core.LoggingEvent.set_Fix(FixFlags value)
at log4net.Appender.BufferingAppenderSkeleton.Append(LoggingEvent loggingEvent)
at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)
The action that failed was:
LinkDemand
The type of the first permission that failed was:
System.Security.Permissions.SecurityPermission
The Zone of the assembly that failed was:
MyComputer
我开启了SQL Profiler但没有任何数据传递到SQL Server。此外,SQL Server帐户具有执行插入操作所需的适当权限。同时,我删除了SecurityContext部分,因为log4net无法识别其中的一部分内容。