ASP. NET Core development-logging using Nlog to write log files

Source: Internet
Author: User

The ASP. NET Core development-logging uses Nlog to write log files.

NLog can be applied to. NET core and ASP.

The ASP. NET core has built-in log support that can be easily exported to the console.

Learn about the use of logging components, using Nlog to write logs to a file record.

Logging use

Create a new ASP. NET Core project, for convenience, I select the Web application and change the authentication to not authenticate.

When new, the corresponding class library is automatically referenced. So we can use Logger directly.

Use of Logger in controller

     Public classHomecontroller:controller {Private ReadOnlyIlogger_logger;  PublicHomeController (iloggerlogger) {_logger=logger; }         PublicIactionresult Index () {_logger. Loginformation ("you visited the homepage"); _logger. Logwarning ("warning Message"); _logger. LogError ("error Message"); returnView (); }         PublicIactionresult About () {viewdata["Message"] ="Your Application description page."; returnView (); }         PublicIactionresult Contact () {viewdata["Message"] ="Your Contact page."; returnView (); }         PublicIactionresult Error () {returnView (); }    }

objects can be used directly using DI.

You will find the log information output is garbled, here we want to specify the output format.

Need to add System.Text.Encoding.CodePages reference

Install-package System.text.encoding.codepages-pre

And then in Startup.cs-> Configure

         Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory)        {            Encoding.registerprovider (codepagesencodingprovider.instance);

This will not appear garbled in the console display.

Log level: Trace-"debug-" Information-"warning-" error-"Critical

The level contains a range from large to small, as Trace contains all the information.

NLog use

NLog is used in ASP. NET Core.

1. Add a reference.

Install-package Nlog.extensions.logging-pre

2. Add the nlog.config file in the project.

<?XML version= "1.0" encoding= "Utf-8"?><Nlogxmlns= "Http://www.nlog-project.org/schemas/NLog.xsd"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Autoreload= "true"Internalloglevel= "Warn"Internallogfile= "Internal-nlog.txt">  <!--define various log targets -  <Targets>    <!--write logs to file -    <TargetXsi:type= "File"name= "Allfile"FileName= "Nlog-all-${shortdate}.log"Layout= "${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />    <TargetXsi:type= "File"name= "Ownfile-web"FileName= "Nlog-own-${shortdate}.log"Layout= "${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception} " />    <TargetXsi:type= "Null"name= "Blackhole" />  </Targets>  <rules>    <!--All logs, including from Microsoft -    <Loggername="*"MinLevel= "Trace"WriteTo= "Allfile" />    <!--Skip Microsoft Logs and so logs only own logs -    <Loggername= "microsoft.*"MinLevel= "Trace"WriteTo= "Blackhole"Final= "true" />    <Loggername="*"MinLevel= "Trace"WriteTo= "Ownfile-web" />  </rules></Nlog>

3. In Startup.cs-"Configure

         Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory)        {            loggerfactory.addnlog (); // Add Nlog

Run the program and you will find that the project has more than two files to prove successful execution.

The nlog-all-*.log here is to record all the logs, the Nlog-own-*.log record skips the information about the output of the class library at the beginning of Microsoft, and the rest of the information.

4. Release (dotnet publish) Considerations

Add Nlog.config to the Publishoptions node of the Project.json

  "publishoptions": {    "include": [      "wwwroot",      " views",      "Appsettings.json",      "Web. config",      "Nlog.config"//plus nlog configuration file    ]  },

Github:https://github.com/linezero/blog/tree/master/netcorelogging

If you think this article is helpful to you, please click " recommend ", thank you.

Reference page:

Http://www.yuanjiaocheng.net/Struts/first.html

Http://www.yuanjiaocheng.net/mvc/area-in-asp.net-mvc.html

Http://www.yuanjiaocheng.net/mvc/tempdata-in-asp.net-mvc.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-dbcontext.html

Http://www.yuanjiaocheng.net/CSharp/csharp-partial-class.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-middleware.html

Http://www.yuanjiaocheng.net/webapi/web-api-controller.html

Http://www.yuanjiaocheng.net/mvc/mvc-helper-RadioButton.html

Http://www.yuanjiaocheng.net/CSharp/first.html

Http://www.yuanjiaocheng.net/mvc/mvc-controller.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-exception.html

ASP. NET Core development-logging using Nlog to write log files

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.