第一步在 AssemblyInfo 添加如下代码
第二步:在web.config添加如下代码:
<log4net> <root> <level value="all" /> <appender-ref ref="INFOAppender" /> <appender-ref ref="DEBUGAppender" /> <appender-ref ref="WARNAppender" /> <appender-ref ref="ERRORAppender" /> <appender-ref ref="FATALAppender" /> <appender-ref ref="ColoredConsole" /> <appender-ref ref="TraceAppender" /> </root> <appender name="INFOAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\info\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd'.log'" /> <staticLogFileName value="false" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender name="DEBUGAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\debug\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd'.log'" /> <staticLogFileName value="false" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <appender name="WARNAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\warn\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd'.log'" /> <staticLogFileName value="false" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN" /> <param name="LevelMax" value="WARN" /> </filter> </appender> <appender name="ERRORAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\error\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd'.log'" /> <staticLogFileName value="false" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> <appender name="FATALAppender" type="log4net.Appender.RollingFileAppender"> <file value="logs\fatal\" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd'.log'" /> <staticLogFileName value="false" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="10MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="FATAL" /> <param name="LevelMax" value="FATAL" /> </filter> </appender> <!--调试模式,输出日志到控制台--> <appender name="ColoredConsole" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> <!--<backColor value="Green" />--> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Yellow, HighIntensity" /> <backColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <param name="conversionPattern" value="%newline*******************************************%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline*******************************************%newline" /> </layout> </appender> </log4net>
相关的log文件添加在根目录中记得:
第三:添加如下代码:记得引入log4net
第四:
得到的效果如下: