log4net in .Net Core
- Reference in .csproj
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" /></ItemGroup>
- log4net.Config.
Assembly entryAssembly = Assembly.GetEntryAssembly();
var logRepository = LogManager.GetRepository(entryAssembly);
System.IO.FileInfo config = new System.IO.FileInfo(pathConfigFile);
log4net.Config.XmlConfigurator.Configure(logRepository, config);
- log4net.LogManager.GetLogger()
log4net.LogManager.GetLogger(typeof(T));
log4net.ILog log = log4net.LogManager.GetLogger("testApp.Logging");
log.Info(DateTime.Now.ToString() + ": login success");
private log4net.ILog GetAppLogger<T>(T t)
{
return log4net.LogManager.GetLogger(typeof(T));
}
- Content of configfile
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="log\Jin-Roll.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="6" /> <layout type="log4net.Layout.PatternLayout"> <!--conversionPattern value="%date %level %thread %logger - %message%newline" /--> <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> <!--param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline"/--> <param name="Footer" value="**** Trace Closed By Jin ****%newline"/> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="RollingFile" /> </root> </log4net> </configuration>
- Invalid ways
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Insert
Insert
[assembly: log4net.Config.XmlConfigurator(Watch = true)] into AssemblyInfo.cs, which is invalid.
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath(“~”)+@”\log4net.config”));
评论
发表评论