1.configuration配置
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
2.Global.asax
protected void Application_Start() { //加载log4配置 log4net.Config.XmlConfigurator.Configure(); }
3.configuration的log4net节点
<log4net> <logger name="wolflogInfo"> <level value="Info" /> <appender-ref ref="wolflogInfoFileAppender" /> </logger> <logger name="wolflogError"> <level value="Error" /> <appender-ref ref="wolflogErrorFileAppender" /> </logger> <logger name="wolflogALL"> <level value="ALL" /> <appender-ref ref="wolflogALLFileAppender" /> </logger> <!-- 绑定使用的名称 --> <appender name="wolflogInfoFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <!--输出文件夹路径--> <param name="File" type="" value="App_Data/Log/Info/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <!--文件名称命名规则--> <param name="DatePattern" value="yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <param name="MaxSizeRollBackups" value="62" /> <layout type="log4net.Layout.PatternLayout,log4net"> <!--输出头--> <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /> </layout> </appender> <appender name="wolflogErrorFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" type="" value="App_Data/Log/Error/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <param name="MaxSizeRollBackups" value="62" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /> </layout> </appender> <appender name="wolflogALLFileAppender" type="log4net.Appender.RollingFileAppender,log4net"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <param name="File" type="" value="App_Data/Log/ALL/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <param name="MaxSizeRollBackups" value="62" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" /> </layout> </appender> </log4net>
4.调用
ILog log = log4net.LogManager.GetLogger("wolflogInfo"); log.Info("输出点东西Info"); log.Error("输出点东西Error"); ILog log1 = log4net.LogManager.GetLogger("wolflogError"); log1.Info("输出点东西Info"); log1.Error("输出点东西Error"); ILog log2 = log4net.LogManager.GetLogger("wolflogALL"); log2.Info("输出点东西Info"); log2.Error("输出点东西Error");
效果