log4net的在app.config的配置如下:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--日志路径-->
<param name="File" value="App_Log\oneWireLog_"/>
<param name="AppendToFile" value="true"/>
<param name="MaxSizeRollBackups" value="100"/>
<param name="MaximumFileSize" value="10MB"/>
<param name="RollingStyle" value="Size"/>
<param name="StaticLogFileName" value="false"/>
<param name="DatePattern" value="yyyyMMdd".txt""/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - (%line) %m%n"/>
</layout>
</appender>
<!-- 控制台前台显示日志 -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<mapping>
<level value="Info"/>
<foreColor value="Green"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Info"/>
<param name="LevelMax" value="Fatal"/>
</filter>
</appender>
<appender name="LogDebug" type="log4net.Appender.RollingFileAppender">
<file value="logs\Debug.log"/>
<appendToFile value="true"/>
<maximumFileSize value="5MB"/>
<maxSizeRollBackups value="10"/>
<RollingStyle value="Composite"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date - [%level]%newline%message%newline"/>
<!--<conversionPattern value="%date [%level] %logger - %message%newline" />-->
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="all"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<logger name="AlarmSender">
<appender-ref ref="ColoredConsoleAppender"/>
<level value="DEBUG"/>
</logger>
<logger name="OneWireAppRead">
<appender-ref ref="RollingLogFileAppender"/>
<level value="DEBUG"/>
</logger>
</log4net>
添加完上面的配置后需要在configuration节点下添加如下节点
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
当做完上面的配置后需要在main方法中添加
log4net.Config.XmlConfigurator.Configure();
上面的操作做完以后就可以将日志打印在日子和文件里面了。