国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

.Net Core學習教程之在Mvc中簡單的使用日志組件

來源:懂視網 責編:小采 時間:2020-11-27 22:35:01
文檔

.Net Core學習教程之在Mvc中簡單的使用日志組件

.Net Core學習教程之在Mvc中簡單的使用日志組件:前言 本文是基于 .Net Core 2.0,只是蜻蜓點水,并非深入淺出。給大家介紹了關于.Net Core在Mvc中使用日志組件的相關內容,分享出供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧 目錄 使用內置的日志組件 簡單過渡到第三方組件 - NLog
推薦度:
導讀.Net Core學習教程之在Mvc中簡單的使用日志組件:前言 本文是基于 .Net Core 2.0,只是蜻蜓點水,并非深入淺出。給大家介紹了關于.Net Core在Mvc中使用日志組件的相關內容,分享出供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧 目錄 使用內置的日志組件 簡單過渡到第三方組件 - NLog

前言

本文是基于 .Net Core 2.0,只是蜻蜓點水,并非深入淺出。給大家介紹了關于.Net Core在Mvc中使用日志組件的相關內容,分享出供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧

目錄

使用內置的日志組件

簡單過渡到第三方組件 - NLog 

使用內置的日志

下面使用控制器 HomeController.cs 進行演示。

需要 using Microsoft.Extensions.Logging;

方案一:

public class HomeController : Controller
 {
 private readonly ILogger _logger ;

 public HomeController(ILoggerFactory loggerFactory)
 {
 _logger = loggerFactory.CreateLogger(typeof(HomeController));
 }
 }

方案二:

public class HomeController : Controller
 {
 private readonly ILogger _logger ;

 public HomeController(ILogger<HomeController> logger)
 {
 _logger = logger;
 }
 }

方案三:

public class HomeController : Controller
 {
 private readonly ILogger _logger ;

 public HomeController(ILogger logger)
 {
 _logger = logger;
 }
 }

三種都是通過注入的方式獲取日志記錄器對象,在過去,我們會自己獨立封裝類似這些 Debug、Info 和 Error 等不同日志等級的方法,現在我們看看內置的方法是如何使用的? 

在 HomeController 內添加 Index() 方法進行測試。

public IActionResult Index()
 {
 _logger.LogDebug($"測試:{DateTime.Now.ToString(CultureInfo.InvariantCulture)}");
 _logger.LogError($"測試:{DateTime.Now.ToString(CultureInfo.InvariantCulture)}");
 _logger.LogInformation($"測試:{DateTime.Now.ToString(CultureInfo.InvariantCulture)}");

 return Json(Guid.NewGuid());
 }

在輸出結果中我們可以看到,不同日志的等級在控制臺中會以不同的顏色進行標注。

 

每種級別的 Log 都有多個方法重載,如 LogInformation() ,示例演示的代碼中使用的是比較簡單一種,也就是最后一種。

//
 // 摘要:
 // Formats and writes an informational log message.
 //
 // 參數:
 // logger:
 // The Microsoft.Extensions.Logging.ILogger to write to.
 //
 // eventId:
 // The event id associated with the log.
 //
 // message:
 // Format string of the log message.
 //
 // args:
 // An object array that contains zero or more objects to format.
 public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args);
 //
 // 摘要:
 // Formats and writes an informational log message.
 //
 // 參數:
 // logger:
 // The Microsoft.Extensions.Logging.ILogger to write to.
 //
 // exception:
 // The exception to log.
 //
 // message:
 // Format string of the log message.
 //
 // args:
 // An object array that contains zero or more objects to format.
 public static void LogInformation(this ILogger logger, Exception exception, string message, params object[] args);
 //
 // 摘要:
 // Formats and writes an informational log message.
 //
 // 參數:
 // logger:
 // The Microsoft.Extensions.Logging.ILogger to write to.
 //
 // message:
 // Format string of the log message.
 //
 // args:
 // An object array that contains zero or more objects to format.
 public static void LogInformation(this ILogger logger, string message, params object[] args);

其它細節以及詳情,或者希望使用其它日志組件可參考官方文檔:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x

簡單過渡到第三方組件 - NLog

Nuget 安裝 NLog.Web.AspNetCore(目前 Nuget 最新為 4.4.1,但是官方的教程卻是 4.5 的,小編使用 4.4.1 進行演示)。如需 4.5+ 可參考官方:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

下面演示如何將內置的組件簡單的移植到 NLog 中。

先在根目錄創建配置文件 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="c:\temp\internal-nlog.txt">


 <!-- the targets to write to -->
 <targets>
 <!-- write logs to file -->
 <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
 layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

 <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
 <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
 layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|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" /> <!-- BlackHole without writeTo -->
 <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
 </rules>
</nlog>

修改 Startup.cs 類中的 Configure() 方法,其它地方都不需要做出任何修改。

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 {
 loggerFactory.AddNLog(); //添加NLog 
 env.ConfigureNLog("nlog.config"); //讀取Nlog配置文件 

 //... 
 }

啟動程序,你會發現:

總結

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

.Net Core學習教程之在Mvc中簡單的使用日志組件

.Net Core學習教程之在Mvc中簡單的使用日志組件:前言 本文是基于 .Net Core 2.0,只是蜻蜓點水,并非深入淺出。給大家介紹了關于.Net Core在Mvc中使用日志組件的相關內容,分享出供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧 目錄 使用內置的日志組件 簡單過渡到第三方組件 - NLog
推薦度:
標簽: 使用 學習 日志
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 婷婷伊人| 亚洲欧美一区二区三区九九九 | 国产欧美高清 | 日本孕妇与黑人xxxxxx | 精品一区二区三区免费视频 | 初撮六十路 | 亚洲国产成人久久一区www妖精 | 亚洲视频在线观看网站 | 精品视频在线观看免费 | 亚洲欧美国产另类 | 亚洲欧美自拍偷拍 | 欧美日韩亚洲区久久综合 | 91系列在线观看免费 | 欧美日本一区二区三区 | 国产日韩一区二区三区在线播放 | 国产精品免费视频播放 | 国产真实乱人偷精品 | 国产高清美女一级a毛片久久 | 亚洲欧美一区二区三区九九九 | 欧美区国产区 | 91中文字幕在线播放 | 激情欧美日韩一区二区 | 亚洲另类欧美日韩 | 国产成人a∨麻豆精品 | 国产午夜高清一区二区不卡 | 欧美日韩激情 | 九九九九热精品免费视频 | 亚洲一级毛片 | 亚洲欧美视频在线观看 | 亚洲视频在线一区 | 欧美高清日韩 | 视频一区二区免费 | 久久久国产一区二区三区 | 国产一区二区三区在线看 | 欧美性xxxxxx性 | 亚洲欧美另类专区 | 黄色免费一级视频 | 黑人群性xxx| 在线观看免费国产 | 成人精品视频在线观看完整版 | 国产在线观看网站 |