log4net in .Net Core

  1. Reference in .csproj
 <ItemGroup>
   <PackageReference Include="log4net" Version="2.0.8" />
 </ItemGroup>

  1. 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);


  1. 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));
}

  1. 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>
  1. Invalid ways
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
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”));

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE