1.在common 和ui 程序集 添加log4net 引用
2.在common中添加loghelper.cs 操作类 以及在ui层添加configs/log4net.config 配置文件
<log4net> <!--错误日志--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="D:log\LogError\"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value="yyyyMMdd'.txt'"/> <staticLogFileName value="false"/> <param name="MaxSizeRollBackups" value="100"/> <layout type="log4net.Layout.PatternLayout"> <!--每条日志末尾的文字说明--> <!--输出格式--> <!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info--> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n错误描述:%message%newline %n"/> </layout> </appender> <!--Info日志--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径--> <param name="File" value="D:log\LogInfo\" /> <!--是否是向文件中追加日志--> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <!--log保留天数--> <param name="MaxSizeRollBackups" value="100" /> <!--日志文件名是否是固定不变的--> <param name="StaticLogFileName" value="false" /> <!--日志文件名格式为:2008-08-31.log--> <param name="DatePattern" value="yyyyMMdd'.txt'" /> <!--日志根据日期滚动--> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n日志描述:%message%newline %n"/> </layout> </appender> <!--监控日志--> <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="D:log\LogMonitor\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd'.txt'" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n跟踪描述:%message%newline %n"/> </layout> </appender> <!--Error日志--> <logger name="logerror"> <level value="ERROR" /> <appender-ref ref="RollingLogFileAppender" /> </logger> <!--Info日志--> <logger name="loginfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <!--监控日志--> <logger name="logmonitor"> <level value="Monitor" /> <appender-ref ref="MonitorAppender" /> </logger> </log4net>
3.在startup构造函数中 添加log4net的注册
public Startup(IConfiguration configuration) { Configuration = configuration; //repository = LogManager.CreateRepository("MvcMovieLog4Respository"); XmlConfigurator.Configure(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\Configs\log4net.config")); }