netCore log4配置
1 /// <summary> 2 /// 使用LOG4NET记录日志的功能,在WEB.CONFIG里要配置相应的节点 3 /// </summary> 4 public class LogHelper 5 { 6 //log4net日志专用 7 8 private static ILoggerRepository repository = LogManager.CreateRepository("AgentRepository"); 9 10 private static ILog Loginfo; 11 private static ILog Logerror; 12 public LogHelper() 13 { 14 } 15 public static void SetConfig() 16 { 17 XmlConfigurator.Configure(repository, new FileInfo("log4net.config")); 18 Loginfo = LogManager.GetLogger(repository.Name, "loginfo"); 19 Logerror = LogManager.GetLogger(repository.Name, "logerror"); 20 } 21 22 public static void SetConfig(FileInfo configFile) 23 { 24 log4net.Config.XmlConfigurator.Configure(repository, configFile); 25 } 26 /// <summary> 27 /// 普通的文件记录日志 28 /// </summary> 29 /// <param name="info"></param> 30 public static void WriteLog(string info) 31 { 32 SetConfig(); 33 if (Loginfo.IsInfoEnabled) 34 { 35 Loginfo.Info(info); 36 } 37 } 38 /// <summary> 39 /// 错误日志 40 /// </summary> 41 /// <param name="info"></param> 42 /// <param name="se"></param> 43 public static void WriteLog(string info, Exception se) 44 { 45 SetConfig(); 46 if (Logerror.IsErrorEnabled) 47 { 48 Logerror.Error(info, se); 49 } 50 } 51 52 }
log4net.config
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <!--日志--> <log4net> <logger name="logerror"> <level value="ERROR" /> <appender-ref ref="ErrorAppender" /> </logger> <logger name="loginfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <logger name="logconsole"> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> </logger> <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="LogsErrorLog" /> <param name="DatePattern" value="yyyy-MM-dd".log"" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="100" /> <param name="MaximumFileSize" value="1MB" /> <param name="RollingStyle" value="Date" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p %d [%c] %m%n" /> </layout> </appender> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="LogsInfoLog" /> <param name="DatePattern" value="yyyy-MM-dd".log"" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="100" /> <param name="MaximumFileSize" value="1MB" /> <param name="RollingStyle" value="Date" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p %d [%c] %m%n" /> </layout> </appender> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %m%n" /> </layout> </appender> </log4net> </configuration>