NetCore two ways of log4
A. You need to specify the log warehouse
nuget:log4net,Microsoft.Extensions.Logging.Log4Net.AspNetCore
1. When the program starts, first create a log warehouse, warehouse log custom, the entire system is the only one of the best warehouse management
. 1 public the Startup (Configuration IConfiguration, IHostingEnvironment Environment) 2 { . 3 / * var = new new ConfigurationBuilder Builder () setBasePath (environment.ContentRootPath) .AddJsonFile ( "appsettings.json");. // json configuration file loading . 4 the Configuration = Configuration; * / . 5 . 6 // String = environment.WebRootPath the root; // the root directory of the project point . 7 String contentRootPath = environment.ContentRootPath; // points to a directory item . 8 System.IO.FileInfo for the fileInfo = new new System.IO.FileInfo for (contentRootPath + " \\ Config \\ log4net.config " ); // load log4net profile 9 log4net.Repository.ILoggerRepository Logger = log4net.LogManager.CreateRepository ( " NETCoreRepository " ); // create a log warehouse, custom name 10 log4net.Config.XmlConfigurator.Configure (Logger, the fileInfo ); // disposed . 11 // log4net.Config.XmlConfigurator.ConfigureAndWatch (Logger, the fileInfo); // configuration and matching 12 }
2. Use
log4net.ILog log = log4net.LogManager.GetLogger("NETCoreRepository",GetType()); log.info ( " Ordinary log " ); log.error ( " error message " );
II. Log warehouse, assembly without specifying the default log warehouse
1 public class Program 2 { 3 public static void Main(string[] args) 4 { 5 BuildWebHost(args).Run(); 6 } 7 8 9 10 public static IWebHost BuildWebHost(string[] args) => 11 WebHost.CreateDefaultBuilder(args) 12 .UseUrls("http://*:5001") 13 .UseIISIntegration() 14 .ConfigureLogging(loggingBuilder => 15 { 16 loggingBuilder.AddFilter("System", LogLevel.Warning); 17 loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);//过滤掉系统默认的一些日志 18 loggingBuilder.AddLog4Net(@"Config\log4net.config"); 19 }) 20 .UseStartup<Startup>() 21 .Build(); 22 }
2. Use
1 log4net.ILog log = log4net.LogManager.GetLogger (GetType ()); // this kind without specifying a log warehouse 2 log.info ( " Ordinary log " ); 3 log.error ( " error message " );
Or so
_Factory = ILoggerFactory // vessel injection object to him _Factory.CreateLogger <T> () LogError (. " Here Error ILoggerFactory " ); // T is the current class type _Factory.LogWarning ($ " Normal Log " );
Used in the controller, injection ILoggerFactory (:) namespace is feasible, log4net.ILog log is not
1 a using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory; // must be the name the control, in order to successfully inject 2 // a using ILoggerFactory = log4net.Repository.Hierarchy.ILoggerFactory; // this is wrong, injection failed 3 4 public class defaultController: the Controller . 5 { . 6 Private ILoggerFactory _Factory = null ; . 7 Private ILogger <defaultController> _logger = null ; . 8 // log4net.ILog log, does not support the injection, . 9 public DefaultController(ILoggerFactory factory,ILogger<DefaultController> logger ) 10 { 11 this._Factory = factory; 12 this._logger = logger; 13 } 14 }