Time：2019-11-21

Preface

I always like the simplicity and extensibility of NLog, so I am going to replace the default log provider provided by asp.net core with NLog.

NLog is a cross platform. Net logging component.

NLog complies with BSD license, which allows commercial application and full open source code. Anyone can use it for free and test it, and then feed back questions and suggestions through the mailing list.

Let’s have a look at the detailed introduction.

Step 1

Add the dependency nlog.extensions.logging in project.json of the project:


"dependencies": {
"NLog.Extensions.Logging": "1.0.0-*"
}

Or add it through nuget package manager.

Step 2

In the startup class of asp.net coreConfigure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)Method:


// using NLog.Extensions.Logging;

//needed for non-NETSTANDARD platforms: configure nlog.config in your project root
env.ConfigureNLog("nlog.config");

Step 3

Add nlog.config file in the project directory:

<?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"
throwConfigExceptions="true"
internalLogLevel="Warn"
internalLogToTrace="true"

<!--
-The log file is placed in the appdata / logs folder and is named {log category}. Log or {log category}. Err
-The logs are archived by day and placed in the appdata / logs / Archives / {log category} folder. The file name is {date}. Log or {date}. Err
-->
<targets>
<! -- output the database log generated by EF 6 through system.diagnostics.trace, which can be seen in the vs output window -- >
<target name="XXX.Entities.AppDbContext.DatabaseLog.Trace"
xsi:type="Trace"
layout="${message}" /> <! -- output the database log generated by EF 6 in the console -- > <target name="XXX.Entities.AppDbContext.DatabaseLog.Console" xsi:type="Console" layout="${message}"
/>
<! -- output the database log generated by EF 6 in the log file -- >
<target name="XXX.Entities.AppDbContext.DatabaseLog.File"
xsi:type="File"
layout="${message}" encoding="utf-8" archiveNumbering="Date" archiveEvery="Day" archiveDateFormat="yyyy-MM-dd" archiveFileName="AppData/Logs/Archives/XXX.Entities.AppDbContext.Database/{#}.log" fileName="AppData/Logs/XXX.Entities.AppDbContext.Database.log" /> <! -- regular trace output, which can be seen in vs output window during debugging -- > <target name="TRACE" xsi:type="Trace" layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}:${newline}${message}${onexception:inner=${newline}${exception:format=ToString}}${newline}" /> <! -- regular console output -- > <target name="CONSOLE" xsi:type="Console" layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}:${newline}${message}${onexception:inner=${newline}${exception:format=ToString}}${newline}" /> <! -- general log file output -- > <target name="LOG_FILE" xsi:type="File" layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}:${newline}${message}${onexception:inner=${newline}${exception:format=ToString}}${newline}" encoding="utf-8" archiveNumbering="Date" archiveEvery="Day" archiveDateFormat="yyyy-MM-dd" archiveFileName="AppData/Logs/Archives/${filesystem-normalize:inner=${logger}}/{#}.log" fileName="AppData/Logs/${filesystem-normalize:inner=${logger}}.log" /> <! -- the log file with err as the suffix of the contract records the warning or error output by the program > <target name="ERROR_LOG_FILE" xsi:type="File" layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}:${newline}${message}${onexception:inner=${newline}${exception:format=ToString}}${newline}" encoding="utf-8" archiveNumbering="Date" archiveEvery="Day" archiveDateFormat="yyyy-MM-dd" archiveFileName="AppData/Logs/Archives/${filesystem-normalize:inner=${logger}}/{#}.err" fileName="AppData/Logs/${filesystem-normalize:inner=\${logger}}.err" />

</targets>

<rules>
<! -- record all logs with a log level no lower than warn to the log file -- >
<logger name="*" minlevel="Warn" writeTo="ERROR_LOG_FILE" />
<! -- record the SQL statement generated by EF -- >
<logger name="XXX.Entities.AppDbContext.DatabaseLog" minlevel="Debug"
writeTo="XXX.Entities.AppDbContext.DatabaseLog.Trace,XXX.Entities.AppDbContext.DatabaseLog.Console,XXX.Entities.AppDbContext.DatabaseLog.File" final="true" />
<! -- mask the debug output of the. Net core assembly unless required for debugging -- >
<logger name="Microsoft.*" minLevel="Info" writeTo="CONSOLE,TRACE,LOG_FILE" final="true" />
<! -- mask the debug output of the system unless necessary for debugging -- >
<logger name="System.*" minLevel="Info" writeTo="CONSOLE,TRACE,LOG_FILE" final="true" />
<! -- record the debug output of the application -- >
<logger name="MyApplication.*" minlevel="Debug" writeTo="TRACE,CONSOLE,LOG_FILE" />
</rules>
</nlog>

NLog profile attribute interpretation:

• Whether throwconfiexexceptions throws an exception when there is a configuration error.
• Internallogfile NLog path of internal log output to file.

Step 4

Add “NLog. Config” in the publishoptions.include section of the project.json file:


"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config",

"nlog.config"
]
}

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

## How to search files or directories under CentOS

Search for files or directories: The search for files is great! Because we often need to know where that file is, let’s talk about how to search it! There are also quite excellent ones under Linux Search system! Usually find is not very common! Because the speed is slow, but also very hard disk! Usually […]