查找原因是因为 loginfo.IsInfoEnabled、loginfo.IsErrorEnabled等一直为false
如果是自己配置的log4net.config文件
需要在调用log4net记录日志的项目的AssemblyInfo.cs文件中添加
[assembly: log4net.Config.DOMConfigurator(ConfigFileExtension = "log4net.config", Watch = true)]
语句
如果是在app.config或者Web.config配置的log4net
需要在调用log4net记录日志的项目的AssemblyInfo.cs文件中添加
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
语句
app.config或者Web.config配置的log4net 加入log4net 设置
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections>
log4net配置
<!--配置Log4net--> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs"/> <!--对于RollingFileAppender,lockingModel不起作用--> <!--<param name="lockingModel" value="log4net.Appender.FileAppender+MinimalLock"/>--> <param name="AppendToFile" value="true"/> <param name="MaxSizeRollBackups" value="3"/> <param name="StaticLogFileName" value="false"/> <param name="DatePattern" value="yyyyMMdd'.log'"/> <param name="RollingStyle" value="Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n时间:%d 消息:%m %n "/> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net>