- 引用log4net.dll
- AssemblyInfo.cs中
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: log4net.Config.DOMConfigurator(ConfigFileExtension = "config", Watch = true)]
3.App.config
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <!--错误日志配置--> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogError\\"/> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="100"/> <param name="MaxFileSize" value="10240"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyyMMdd"/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n异常时间:%d [%t] %n异常级别:%-5p %n异 常 类:%c [%x] %n%m %n "/> </layout> </appender> <!--信息日志配置--> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\"/> <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"/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%nLogDateTime:%d [%t] %nLogLevel:%-5p %nLogType:%c [%x] %n%m %n"/> </layout> </appender> <!--控制台--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline"/> </layout> </appender> <!--log4net.LogManager.GetLogger("logerror")用这个来选择这种类型--> <logger name="logerror"> <level value="ERROR"/> <appender-ref ref="ErrorAppender"/> </logger> <logger name="loginfo"> <level value="INFO"/> <appender-ref ref="InfoAppender"/> </logger> <!--<root> <level value="INFO"/> <appender-ref ref="InfoAppender"/> <appender-ref ref="ConsoleAppender"/> </root>--> </log4net>