2014-01-10 79 views
0

我已經嘗試了幾種方法來調整,但Log4Net文件記錄器沒有記錄任何東西在我的MVC3網絡應用程序。爲什麼Log4Net不寫錯誤日誌?

我缺少什麼?我也沒有得到任何錯誤。

log4net dll被添加到應用程序,它指向本地副本。

1主要的web.config:

<appSettings> 
    <add key="Log4NetConfigFilename" value="log4net.config" /> 
    <add key="Log4NetConfigFilePath" value="C:\DEV\App\source\App.Web\log4net.config" />  
    </appSettings> 

2的Global.asax.cs

protected void Application_Start() 
{ 
    log4net.Config.XmlConfigurator.Configure(); 
} 

3 Log4net.config文件:

<?xml version="1.0" encoding="utf-8" ?> 
<log4net> 
    <logger name="MyLogger"> 
    <level value="ALL" /> 
    <appender-ref ref="MyRollingLogFileAppender" /> 
    </logger> 

    <appender name="MyRollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> 
    <param name="File" value="C:\\Support\Logs\\logs.log" /> 
    <param name="AppendToFile" value="true" /> 
    <param name="RollingStyle" value="Size" /> 
    <param name="MaxSizeRollBackups" value="10" /> 
    <param name="MaximumFileSize" value="1024KB" /> 
    <param name="StaticLogFileName" value="true" /> 
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
    <layout type="log4net.Layout.PatternLayout, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821">  
     <param name="ConversionPattern" value="%date{MM-dd-yyyy HH:mm:ss} [%-5level] [%identity] - %message %newline" />  
    </layout> 
    </appender> 
</log4net> 

#4 I have a folder created C:\Support\Logs 
#5 The log4net.config file is located in C:\DEV\App\source\App.Web\log4net.config 

6)控制器,其中我想記錄字符串

These 2 values getting in the controller correctly 

using log4net; 
using log4net.Config; 
using System.Configuration; 
namespace My.Controllers 
{  
    public class TestController : MyBaseController 
    { 
     private static readonly ILog _log = LogManager.GetLogger("MyLogger"); 
     private static readonly string _log4netConfigFilename = ConfigurationManager.AppSettings["Log4NetConfigFilename"]; 
     private static readonly string _log4netConfigFilePath = ConfigurationManager.AppSettings["Log4NetConfigFilePath"]; 

    [HttpGet] 
    public ActionResult Index() 
    {    
      if (_log.IsDebugEnabled) <-- ALWAYS FALSE     
       _log.DebugFormat("Processing Index() view"); <-- NEVER GETS WRITTEN 

      var model = new MyModel();    
      return View(model); 
     } 
    } 
} 

回答

2

這就像它沒有看到你的配置文件。嘗試明確設置它通過更新App_Start代碼下面來看看配置文件:

log4net.Config.XmlConfigurator.Configure(ConfigurationManager.AppSettings["Log4NetConfigFilePath"]); 

我不是,我可以在此刻測試,但看看是否能得到它。

0

您需要通過指定的路徑,以確保您使用的是自定義log4net.config調用Configure時:

log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["Log4NetConfigFilePath"])); 

而且據我所知,你不需要額外的組件信息配置的<layout>部分(導致我錯誤):

<layout type="log4net.Layout.PatternLayout">  
    <param name="ConversionPattern" value="%date{MM-dd-yyyy HH:mm:ss} [%-5level] [%identity] - %message %newline" />  
</layout>