ASP. NET Core and NLog integration complete steps
- 2021-10-25 06:22:12
- OfStack
Preface
1Zhi likes the simplicity and extensibility of NLog, so he is ready to replace the default logging provider provided by ASP. NET Core with NLog.
NLog is a cross-platform. Net logging component.
NLog complies with BSD license, which allows commercial applications and is fully open source. Anyone can use it for free, test it, and then feed back questions and suggestions through the mailing list.
The following words are not much to say, let's take a look at the detailed introduction.
Step 1
Add dependency NLog. Extensions. Logging in the project's project. json:
"dependencies": {
"NLog.Extensions.Logging": "1.0.0-*"
}
Or through the NuGet package manager.
Step 2
In ASP. NET Core startup class Startup
Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
Method to add:
// using NLog.Extensions.Logging;
loggerFactory.AddNLog();
//needed for non-NETSTANDARD platforms: configure nlog.config in your project root
env.ConfigureNLog("nlog.config");
Step 3
Add the nlog. config file under 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"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="Warn"
internalLogToTrace="true"
internalLogFile="AppData/Logs/NLogInternal.log">
<!--
- The log file is placed in the AppData/Logs Folder, the file name is { Log category }.log Or { Log category }.err
- The log is archived on a daily basis and placed in AppData/Logs/Archives/{ Log category } Folder, the file name is { Date }.log Or { Date }.err
-->
<targets>
<!-- Pass System.Diagnostics.Trace Output by EF 6 The resulting database log can be found in the VS Output window to see -->
<target name="XXX.Entities.AppDbContext.DatabaseLog.Trace"
xsi:type="Trace"
layout="${message}"
/>
<!-- Output in the console by EF 6 Generated database log -->
<target name="XXX.Entities.AppDbContext.DatabaseLog.Console"
xsi:type="Console"
layout="${message}"
/>
<!-- Output in the log file by EF 6 Generated database log -->
<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"
/>
<!-- Conventional Trace Output, you can debug in the VS Output window to see -->
<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}" />
<!-- General 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" />
<!-- Agreed to err The log file with the file suffix 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 log levels not lower than Warn Log to log file -->
<logger name="*" minlevel="Warn" writeTo="ERROR_LOG_FILE" />
<!-- Record EF Generated SQL Statement -->
<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" />
<!-- Unless necessary for debugging, put .NET Core The of the assembly Debug Outputs are shielded -->
<logger name="Microsoft.*" minLevel="Info" writeTo="CONSOLE,TRACE,LOG_FILE" final="true" />
<!-- Unless necessary for debugging, put the system's Debug Outputs are shielded -->
<logger name="System.*" minLevel="Info" writeTo="CONSOLE,TRACE,LOG_FILE" final="true" />
<!-- Record the application's Debug Output -->
<logger name="MyApplication.*" minlevel="Debug" writeTo="TRACE,CONSOLE,LOG_FILE" />
</rules>
</nlog>
Interpretation of NLog Configuration File Attributes;
Whether autoReload monitors configuration file changes and loads automatically. Whether throwConfigExceptions throws an exception when a configuration error occurs. internalLogLevel NLog internal log level. Whether internalLogToTrace outputs NLog internal logs to Trace. internalLogFile NLog the path to the file from the internal log output.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"
]
}
Summarize