The DotNet Core Console program uses NLog and dotnetnlog

Source: Internet
Author: User
Tags dotnet

The DotNet Core Console program uses NLog and dotnetnlog

Reference: https://github.com/NLog/NLog/wiki/Tutorial

Steps:

1. Use Nuget to install NLog. Extensions. Logging

 

Install-Package NLog.Extensions.Logging

 

2. Write the code (no error is reported when you run the code in this step, but there is no log output because the configuration file is not set)

3. Compile the configuration file

Add the NLog. config file under the project and set it to copy to the running directory. Paste the volume to the internal, and re-run the program to view the output to the log of file.txt.

    <?xml version="1.0" encoding="utf-8" ?>    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">          <targets>        <target name="logfile" xsi:type="File" fileName="file.txt" />      </targets>          <rules>        <logger name="*" minlevel="Info" writeTo="logfile" />      </rules></nlog>

4. Add an output source, output logs to the screen, and modify the configuration file according to the following content.

    <?xml version="1.0" encoding="utf-8" ?>    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">          <targets>        <target name="logfile" xsi:type="File" fileName="file.txt" />        <target name="console" xsi:type="Console" />      </targets>          <rules>        <logger name="*" minlevel="Info" writeTo="logfile" />        <logger name="*" minlevel="Info" writeTo="console" />      </rules></nlog>

5. If you want to send an email, install NLog. MailKit

Install-Package NLog.MailKit

6. Add the settings for sending messages in the configuration file (refer to the https://github.com/nlog/NLog/wiki/Mail-target)

 

    <?xml version="1.0" encoding="utf-8" ?>    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          internalLogFile="d:\Nlog_log.txt" internalLogLevel="Error">      <variable name="smtpServer" value="***"/>      <variable name="smtpUserName" value="***"/>      <variable name="smtpPassword" value="***"/>      <variable name="from" value="***"/>      <variable name="to" value="***"/>          <targets>        <target name="logfile" xsi:type="File" fileName="logs\${date:format=yyyyMMdd}_log.txt" layout="${date:format=yyyy-MM-dd HH\:mm\:ss} ${message}" />        <target name="console" xsi:type="Console" layout="${date:format=yyyy-MM-dd HH\:mm\:ss} [${level}] ${message}"/>        <target name="infoMail" xsi:type="Mail"                  smtpServer="${smtpServer}"                  smtpUserName="${smtpUserName}"                  smtpPassword="${smtpPassword}"                  from="${from}"                  to="${to}"                  subject="info log"                  body="${message}"                  html="true"/>        <target name="errorMail" xsi:type="Mail"                  smtpServer="${smtpServer}"                  smtpUserName="${smtpUserName}"                  smtpPassword="${smtpPassword}"                  from="${from}"                  to="${to}"                  subject="error log"                  body="${message}"/>      </targets>          <rules>        <logger name="***" minlevel="Info" writeTo="infoMail" />        <logger name="***" minlevel="Error" writeTo="logfile" />        <logger name="Main" minlevel="Info" writeTo="console" />        <logger name="Main" minlevel="Error" writeTo="errorMail" />      </rules>          <extensions>        <add assembly="NLog.MailKit"/>      </extensions></nlog>

 

Related Article

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.