. Net Core uses nlog logging

Source: Internet
Author: User

See also: https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

Approximate steps:

    • NuGet references Nlog and NLog.Web.AspNetCore 4.5+
    • Create the Nlog.config file and partially modify it:
<?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= "Info"Internallogfile= "Logs\internal-nlog.txt">  <!--Enable ASP. NET Core layout renderers -  <Extensions>    <AddAssembly= "NLog.Web.AspNetCore"/>  </Extensions>  <!--The targets to write to -  <Targets>    <!--write logs to file -    <TargetXsi:type= "File"name= "Allfile"FileName= "Logs\nlog-all-${shortdate}.log"Layout= "${longdate}|${event-properties:item=eventid_id}|${uppercase:${level}}|${logger}|${message} ${exception: format=tostring} " />    <!--another file log, only own logs. Uses some ASP. NET Core renderers -    <TargetXsi:type= "File"name= "Ownfile-web"FileName= "Logs\nlog-own-${shortdate}.log"Layout= "${longdate}|${event-properties:item=eventid_id}|${uppercase:${level}}|${logger}|${message} ${exception: Format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action} " />  </Targets>  <!--rules to map from logger name to target -  <rules>    <!--All logs, including from Microsoft -    <Loggername="*"MinLevel= "Trace"WriteTo= "Allfile" />    <!--Skip non-critical Microsoft logs and so log only own logs -    <Loggername= "microsoft.*"Maxlevel= "Info"Final= "true" />    <!--blackhole without WriteTo -    <Loggername="*"MinLevel= "Trace"WriteTo= "Ownfile-web" />  </rules></Nlog>
    • Right-click Nlog.config, Properties, build action set to "content", Copy to output directory "always copy"
    • In Program.cs:
varLogger = NLog.Web.NLogBuilder.ConfigureNLog ("Nlog.config").            Getcurrentclasslogger (); Try{logger. Debug ("Init main"); varHostbuilder =Createwebhostbuilder (args); varHost =Hostbuilder.build (); Host.            Run (); }            Catch(Exception ex) {//nlog:catch Setup ErrorsLogger. Error (ex,"Stopped Program because of exception"); Throw; }            finally            {                //ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)NLog.LogManager.Shutdown (); }
    • Configure Appsettings.json. Note the settings in the configuration override the Setminimumlevel property in the code. So either delete "Default" using the configuration in the code or set it up correctly.
      {"    Logging": {"LogLevel": {"        Default": "            Trace",            "Microsoft": "Information"    }}}

      Note: If you have multiple environments, note appsetings. The configuration of the Development.json.

    • Use:
usingMicrosoft.Extensions.Logging; Public classhomecontroller:controller{Private ReadOnlyIlogger_logger;  PublicHomeController (iloggerlogger) {_logger=logger; }     PublicIactionresult Index () {_logger. Loginformation ("Index page says hello"); returnView (); }
    • Results are viewed. If your configuration is consistent with mine, the Internal-nlog.txt file will exist in the project's Logs folder. The output log will be present in the Bin\debug\netcoreapp2.1\logs directory with a date.

. Net Core uses nlog logging

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.