YFLog.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class YFLog { private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("WebLogger"); public static void logInfo(string logstr) { Console.WriteLine(logstr); initLog4net(); logger.Info(logstr); } public static void logInfo(string format, params object[] args) { Console.WriteLine(format, args); initLog4net(); logger.InfoFormat(format, args); } public static void logError(string logstr) { Console.WriteLine(logstr); initLog4net(); logger.Error(logstr); } public static void logError(string format, params object[] args) { Console.WriteLine(format, args); initLog4net(); logger.ErrorFormat(format, args); } private static void initLog4net() { if (!logger.IsInfoEnabled) { var logCfg = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); log4net.Config.XmlConfigurator.ConfigureAndWatch(logCfg); } } }
log4net.config
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="infoAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="INFO" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <encoding value="utf-8"/> <preserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <file value="Logs\\" /> <StaticLogFileName value="false"/> <datePattern value="yyyyMMdd'-info.log'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date - %message%newline" /> </layout> </appender> <appender name="errorAppender" type="log4net.Appender.RollingFileAppender"> <filter type="log4net.Filter.LevelMatchFilter"> <levelToMatch value="ERROR" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <encoding value="utf-8"/> <preserveLogFileNameExtension value="true" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <file value="Logs\\" /> <StaticLogFileName value="false"/> <datePattern value="yyyyMMdd'-err.log'" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date - %message%newline" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="infoAppender" /> <appender-ref ref="errorAppender" /> </root> <logger name="Performance" additivity="false"> <level value="ALL" /> </logger> </log4net>