首先添加log4net的引用,可以使用VS的Nuget下载
1.配置Web.config文件在configuration节点下配置configSections中的section
<configSections> <!-- Add log4net config section--> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
2.在configuration节点下添加log4net节点
<log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\log.txt" />//文件保存的位置 <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net>
后台写日志时:
public ActionResult Index() { ILog log = log4net.LogManager.GetLogger("RollingLogFileAppender"); log.Error("有了 有了 哈哈 有了"); return View(); }
至此,就完成了