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>