ASP.NET Core 3
參考
NLog.dll
NLog.Web.AspNetCore.dll
新建NLog.config組態檔
<?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" internalLogLevel="Info" internalLogFile="${basedir}/log/internal.log"> <!-- enable asp.net core layout renderers --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <target xsi:type="File" name="INFO-web" fileName="${basedir}/log/info${shortdate}.log" layout="${longdate}|${uppercase:${level}}|${message} |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> <target xsi:type="File" name="Error-web" fileName="${basedir}/log/error${shortdate}.log" layout="${longdate}|${uppercase:${level}}|${message} |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> <target xsi:type="File" name="allfile" fileName="${basedir}/log/all${shortdate}.log" layout="${longdate}|${uppercase:${level}}|${message} |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger name="Microsoft.*" maxlevel="Info" final="true" /> <logger name="*" minlevel="Info" writeTo="INFO-web" /> <logger name="*" minlevel="Error" writeTo="Error-web" /> </rules> </nlog>
屬性說明
配置元素
targets –定義日志目標/輸出
rules –定義日志路由規則
extensions –從* .dll檔案加載NLog擴展名
include–包括外部組態檔
variable –設定配置變數的值
目標
name –目標名稱
type–目標型別–例如“檔案”,“資料庫”,“郵件”,使用名稱空間時,此屬性命名為xsi:type
日志級別(降序排列)
Fatal
Error
Warn
Info
Debug
Trace
規則
name –記錄器名稱過濾器-可能包含通配符(*和?)
minlevel –最低級別的日志
maxlevel –記錄的最大級別
level –單級登錄
levels -以逗號分隔的要記錄級別的串列
writeTo –以逗號分隔的要寫入的目標串列
final –最終規則匹配后未處理任何規則
enabled-設定為false禁用規則而不洗掉它
ruleName-規則識別符號,允許使用Configuration.FindRuleByName和查找規則 Configuration.RemoveRuleByName,
列:
<target xsi:type="File" name="INFO-web" fileName="${basedir}/log/${shortdate}.log"
layout="${longdate}|${uppercase:${level}}|${message} |url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
${basedir}:專案路徑/log/${shortdate}:日期.log
longdate:寫入時間
message:記錄的日志資訊
更改Program
public static void Main(string[] args) { var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();//配置Nlog檔案 try { logger.Debug("infoMain"); } catch (Exception ex) { logger.Error(ex, "Stopped program because of exception"); } CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .ConfigureLogging(logging=> { logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Trace); }).UseNLog();//注入NLog }
控制器
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("LogInformation"); _logger.LogError("LogError"); return View(); } }
日志檔案位置:bin\Debug\netcoreapp3.1
謹以此用于記錄,不足之處請多指教
參考文章
https://github.com/NLog/NLog/wiki/Configuration-file#configuration-file-locations
https://www.cnblogs.com/dflying/archive/2006/12/15/593158.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/236841.html
標籤:.NET Core
