日期:2014-05-18  浏览次数:20546 次

在ASP.NET2.0下使用log4net不能写日志!找不出原因
在asp.net2.0下使用log4net,加了ASPNET用户,也给了写权限。但始终无法写日志。配置和代码如下: 

--------------------------------- 

[web.config文件]

<?xml version="1.0" encoding="utf-8"? > 

<configuration > 
   
  <configSections > 
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/ > 
  </configSections > 
   
  <system.web > 
  <compilation debug="true" / > 
  <authentication mode="Windows" / > 
  </system.web > 

  <log4net > 

  <appender name="LogFileAppender" type="log4net.Appender.FileAppender" > 
  <param name="File" value="D:\Log\Log4Net.log"/ > 
  <layout type="log4net.Layout.PatternLayout" > 
  <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/ > 
  </layout > 
  </appender > 
  <logger name="File" > 
  <level value="ALL" / > 
  <appender-ref ref="LogFileAppender" / > 
  </logger > 

  </log4net > 

</configuration > 


[Global.asax文件] 

<%@ Application Language="C#" % > 

<script runat="server" > 

  void Application_Start(object sender, EventArgs e)  
  {  
  log4net.Config.BasicConfigurator.Configure ( ); 
  } 
   
  void Application_End(object sender, EventArgs e)  
  {} 
   
  void Application_Error(object sender, EventArgs e)  
  {} 

  void Session_Start(object sender, EventArgs e)  
  {} 

  void Session_End(object sender, EventArgs e)  
  {} 
   
</script > 


[Default.aspx.cs文件] 

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using log4net; 

public partial class _Default : System.Web.UI.Page  

  protected void Page_Load(object sender, EventArgs e) 
  { 
  ILog logger = log4net.LogManager.GetLogger ("File"); 
  logger.Info ( "Just test log4net."); 
  } 


--------------------------------- 

D:\Log 已加ASPNET用户,并给予了“完全控制”的权限,仍然无法写日志(Log4Net.log文件也没有生成)。 

我还试过在Default.aspx.cs中的Page_Load方法内加入如下代码: 

------------------------- 
string fileName = "D:\\Log\\test.txt"; 
using (System.IO.FileStream fs = System.IO.File.Create (fileName))  

  byte[] info = new System.Text.UTF8Encoding (true).GetBytes ("Test test test."); 
  fs.Write (info, 0, info.Length); 

------------------------- 

这段