. NET Core Consolidation Log Log4net

Source: Internet
Author: User
Tags exception handling log4net

The. NET core framework itself is embedded in the log system. Instantiate Microsoft.Extensions.Logging.LoggerFactory start
Use the Iloggerfactory extension method to specify the provider you want to use. The code is as follows:

public static void Main (string[] args = null)
{
iloggerfactory loggerfactory = new Loggerfactory ()
. Addconsole ()
.  Adddebug ();
ILogger logger = loggerfactory.createlogger<program> ();
Logger. Loginformation (
"This is a test of the emergency broadcast System.");
}
But out-of-the-box ILogger implementations (Microsoft.Extensions.Logging.Logger) do not have such exception handling, so if an exception occurs, you will need to invoke the code to handle it, and to do so, call LOGGER.LOGX each time. A common way to resolve this problem might be to wrap the logger to catch exceptions. Basically the output is on the console. And no output TXT file was found. You can't see anything on IIS when it's published.
So the decision is still to integrate log4net. First, add the Log4net package to the NuGet, adding the configuration file. Then add the following code to the startup file:

     public static iloggerrepository repository {get; set;}         public Startup (Ihostingenvironme NT Env)         {            var builder = new Configurationbuilder () & nbsp              . Setbasepath (env. Contentrootpath)                 Addjsonfile ("Appsettings.json", Optional:false, Reloadonchange:true)               &N BSP;. Addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", Optional:true)                .
Addenvironmentvariables ();             Configuration = Builder.
Build ();            //(Jacky  2017-06-27)             Repository
= Logmanager.createrepository ("Netcorerepository");             Xmlconfigurator.configure (repository, New FileInfo ("Log4net.config"));        }

Call log

var log = Logmanager.getlogger (repository. Name, typeof (Startup));
Log. Info ("test");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.