<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <!--必须在AssemblyInfo类添加配置信息--> <!--[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]--> <log4net> <!--默认逻辑--> <logger name="DefaultLogger"> <level value="ALL"/> <!--info-warn级别--> <appender-ref ref="InfoLoging" /> <!--error+级别--> <appender-ref ref="ErrorLoging" /> </logger> <!--其它逻辑--> <logger name="OtherCustomerLogger"> <level value="ALL"/> <!--info-warn级别--> <appender-ref ref="OtherInfoLoging" /> <!--error+级别--> <appender-ref ref="OtherErrorLoging" /> </logger> <!--默认逻辑info-warn级别--> <appender name="InfoLoging" type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogTipMsg.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="INFO"/> <LevelMax value="Warn"/> </filter> </appender> <!--默认逻辑Error+级别--> <appender name="ErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogErrorMsg.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="ERROR" /> </filter> </appender> <!--其它逻辑info-warn级别--> <appender name="OtherInfoLoging" type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogOtherTipMsg.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="INFO"/> <LevelMax value="Warn"/> </filter> </appender> <!--==================================================================================--> <!--其它逻辑Error+级别--> <appender name="OtherErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogOtherErrorLoging.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="ERROR" /> </filter> </appender> </log4net> </configuration>
core版本
core中使用log4net与.Net中有所不同,在Startup类中声明一个ILoggerRepository类型
public static ILoggerRepository repository;
然后再中间件中添加配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
repository = LogManager.CreateRepository("NETCoreRepository");
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
}
使用时在控制器中创建Log对象
public class HomeController : Controller
{
private ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(HomeController));
public IActionResult Index()
{
log.Info("这是Info信息");
log.Error("这是Error信息");
return View();
}
}
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- This section contains the log4net configuration settings --> <log4net> <!--不同--> <root> <level value="ALL" /> <appender-ref ref="DefaultLogger" /> <appender-ref ref="ErrorLoging" /> </root> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" /> </appender> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.log" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="DefaultLogger" type="log4net.Appender.RollingFileAppender"> type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogTipMsg.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="INFO"/> <LevelMax value="Warn"/> </filter> </appender> <!--默认逻辑Error+级别--> <appender name="ErrorLoging" type="log4net.Appender.RollingFileAppender,log4net" > <File value="App_DataLogLogErrorMsg.log" /> <!--日志以追加形式记录--> <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Date"/> <!--最大日志文件数量--> <param name="MaxSizeRollBackups" value="10"/> <!--单个日志文件最大大小--> <param name="MaxmumFileSize" value="1MB"/> <!--设置日志文件名称生成规则--> <param name="DatePattern" value=""Logs_"yyyyMMdd".log""/> <!--日志名称是否为静态--> <param name="StaticLogFileName" value="false"/> <!--布局--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> <!--级别设置--> <filter type="log4net.Filter.LevelRangeFilter"> <LevelMin value="ERROR" /> </filter> </appender> <!-- Setup the root category, add the appenders and set the default level --> </log4net> </configuration>