log4net在ASP.NET MVC中的配置,还有一种配置方式,即不在web.config中,而是单独新建一个log4net.config 在根目录下
第一、引用log4net.dll
第二、在站点根目录下增加log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!--定义输出到文件中--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--定义文件存放位置--> <file value="log\\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyy-MM-dd‘.txt‘"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <param name="Encoding" value="utf-8" /> <layout type="log4net.Layout.PatternLayout"> <!--<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>--> <conversionPattern value="%newline %nDate:%date %nThread:[%thread] %nLevel: %-5level %nClass: %logger [%property{NDC}] %nMessage: %message%newline" /> </layout> </appender> <root> <level value="ALL"/> <!--文件形式记录日志--> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net> </configuration>
第三、配置xmlConfiguration
在站点properties 下找到AssemblyInfo增加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
第四、在控制台写测试一样work
public class HomeController : Controller { log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController)); public ActionResult Index() { log.Info("test...."); return View(); } }
时间: 2024-12-06 17:29:42