Nlog is used in ASP. NET core and encapsulated as a public log method

Source: Internet
Author: User

1, installation Nlog

"NLog.Extensions.Logging": "1.0.0-RTM-ALPHA4"

2, Configuration Nlog

 Public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory)        {            Loggerfactory.addconsole (configuration.getsection ("Logging"));            Loggerfactory.adddebug ();             // Configure Nlog             Loggerfactory.addnlog ();            Env. Configurenlog ("Nlog.config");            App. Useapplicationinsightsrequesttelemetry ();

3, Nlog.config

<?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"throwconfigexceptions= "true"Internalloglevel= "Debug"Internallogtotrace= "true">  <Targets>    <Targetname= "LogFile"Xsi:type= "File"FileName= "Logs/${shortdate}.log"Layout= "${longdate} [${level:uppercase=true}] ${callsite:classname=true:methodname=true:skipframes=1} ${message} ${ Exception} @${callsite:filename=true:includesourcepath=true} " />    <Targetname= "Console"Xsi:type= "Coloredconsole"Layout= "${longdate} [${level:uppercase=true}] ${callsite:classname=true:methodname=true:skipframes=1} ${message} ${ Exception} @${callsite:filename=true:includesourcepath=true} "/>    <TargetXsi:type= "Null"name= "Blackhole" />  </Targets>  <rules>    <!--The debug output of the. NET Core assembly is masked by the Trace-"debug-" Information-"warning-" error-"Critical", unless required for debugging -    <Loggername= "microsoft.*"MinLevel= "Trace"WriteTo= "Blackhole"Final= "true" />    <!--The debug output of the system is shielded unless required for commissioning -    <Loggername= "system.*"MinLevel= "Trace"WriteTo= "Blackhole"Final= "true" />        <Loggername="*"MinLevel= "Debug"WriteTo= "Logfile,console" />  </rules></Nlog>

Nlog anomaly Level: Trace-"debug-" Information-"warning-" error-"Critical"

Note the log for component output from Microsoft and system can be removed from the configuration file, or the log will be very numerous

For more configuration of NLog, please refer to: https://github.com/NLog/NLog/wiki/Callsite-layout-renderer

4, encapsulated into a public method

usingNLog;usingSystem;usingSystem.Diagnostics;namespaceUFX. tools{ Public classLoghelper {Private Static ReadOnlyLogger log = Logmanager.getlogger ("");  Public Static voidError (ObjectMSG, Exception exp =NULL)        {            if(exp = =NULL) log. Error ("#"+msg); Elselog. Error ("#"+ msg +"  "+exp.        ToString ()); }         Public Static voidDebug (ObjectMSG, Exception exp =NULL)        {            if(exp = =NULL) log. Debug ("#"+msg); Elselog. Debug ("#"+ msg +"  "+exp.        ToString ()); }         Public Static voidInfo (ObjectMSG, Exception exp =NULL)        {            if(exp = =NULL) log. Info ("#"+msg); Elselog. Info ("#"+ msg +"  "+exp.        ToString ()); }         Public Static voidWarn (ObjectMSG, Exception exp =NULL)        {            if(exp = =NULL) log. Warn ("#"+msg); Elselog. Warn ("#"+ msg +"  "+exp.        ToString ()); }    }}

5. Use direct Loghelper.debug ("") in other methods.

But you'll find that all of the exceptions in the log output are from Tools.loghelper, so that you can't accurately return the method of the exception, where the code is located, and in the previous version of. NET core, we can find the calling method through StackFrames, but. Net The core method doesn't work.

Fortunately Nlog already for us to consider this problem, the configuration file can directly define the method name that needs to be printed, the number of frames that need to be searched online

${callsite:classname=true:methodname=true:skipframes=1}

For more configuration Please refer to: https://github.com/NLog/NLog/wiki/Callsite-layout-renderer

Using nlog;using system;using system.diagnostics;
Namespace UFX.        tools{public class Loghelper {private static readonly Logger log = Logmanager.getlogger (""); public static void Error (Object msg, Exception exp = null) {if (exp = = null) log.            Error ("#" + msg); else log. Error ("#" + msg + "" + exp.)        ToString ()); }
public static void Debug (Object msg, Exception exp = null) {if (exp = = null) log.            Debug ("#" + msg); else log. Debug ("#" + msg + "" + exp.)        ToString ()); }
public static void Info (Object msg, Exception exp = null) {if (exp = = null) log.            Info ("#" + msg); else log. Info ("#" + msg + "" + exp.)        ToString ()); }

public static void Warn (Object msg, Exception exp = null) {if (exp = = null) log.            Warn ("#" + msg); else log. Warn ("#" + msg + "" + exp.)        ToString ()); }    }}

Nlog is used in ASP. NET core and encapsulated as a public log method

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.