欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

.NetCore項目如何添加日志功能詳解-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān).Net Core項目如何添加日志功能詳解,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)公司是一家專業(yè)從事網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計公司,作為專業(yè)的成都網(wǎng)站建設(shè)公司,創(chuàng)新互聯(lián)公司依托強(qiáng)大的技術(shù)實力、以及多年的網(wǎng)站運營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計開發(fā)服務(wù)!

一、微軟內(nèi)置的日志組件

在.Net Core中使用模板新建的Web Api項目時,會自動加入日志功能。只需要在控制器中注入ILogger就可以了。命名空間為:Microsoft.Extensions.Logging

.Net Core項目如何添加日志功能詳解

會發(fā)現(xiàn)只有Error被打印到了控制臺,Trace沒有被打印。那是因為在appsetting.json中配置了Logging>Console>Default的等級為Debug,日志的等級大于等于Debug才會輸出到控制臺。在這里說一下LogLevel:Trace<Debug<Information<Warning<Error<Critical<None

當(dāng)打開appsettings.development.json文件你會發(fā)現(xiàn)跟appsettings.json配置不同。如下:

{
 "Logging": {
 "IncludeScopes": false,
 "LogLevel": {
 "Default": "Debug",
 "System": "Information",
 "Microsoft": "Information"
 }
 }
}

例如:

"System": "Information"表示命名空間以System開頭的類中且日志等級大于等于Information才會輸出到控制臺。

"Default": "Debug"表示除以System和Microsoft開頭的命名空間日志等級大約等于Debug才會輸出到控制臺。

這里說明一下到底是在什么時候,讀取了appsettings.json中的配置了了? 其實是在Program中WebHost.CreateDefaultBuilder(arge)

打開源碼發(fā)現(xiàn)

.Net Core項目如何添加日志功能詳解

當(dāng)然我們可以不用微軟提供的默認(rèn)配置

public class Program
 {
 public static void Main(string[] args)
 {
  //指定配置文件路徑
  var configBuilder = new ConfigurationBuilder()
     .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.json", true, true)
    .AddJsonFile($"appsettings.{EnvironmentName.Development}.json", true, true);

  var config = configBuilder.Build();
  
  var host = new WebHostBuilder()
   .UseKestrel()
   .UseStartup<Startup>()
   .UseContentRoot(Directory.GetCurrentDirectory())
   .UseUrls(config["AppSettings:Url"])//設(shè)置啟動時的地址
   .Build();
  host.Run();
 }
 }

配置文件為:

{
 "AppSettings": {
 "Url": "http://0.0.0.0:6000"
 },
 "Logging": {
 "IncludeScopes": false,
 "Debug": {
 "LogLevel": {
 "Default": "Info"
 }
 },
 "Console": {
 "LogLevel": {
 "Default": "Warning"
 }
 }
 }
}

StartUp為:

public class Startup
 {
 public IConfiguration Configuration { get; private set; }
 public Startup(IHostingEnvironment env)//在構(gòu)造函數(shù)中注入 IHostingEnvironment 
 {
  Configuration = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile($"appsettings.json")
    .Build();
 }
 public void ConfigureServices(IServiceCollection services)
 {
  services.AddMvc();
 }

 public void Configure(IApplicationBuilder app,
  IHostingEnvironment env,
  ILoggerFactory loggerFactory)
 {
  if (env.IsDevelopment())
  {
  app.UseDeveloperExceptionPage();
  }
  //添加控制臺輸出
  loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  loggerFactory.AddDebug();

  app.UseMvc();
 }
 }

但是微軟提供的內(nèi)置的日志組件沒有實現(xiàn)將日志記錄到文件、數(shù)據(jù)庫上。下面介紹NLog

二、NLog

首先使用NuGet添加NLog,然后在Startup的Configure中添加以下代碼


public void Configure(IApplicationBuilder app,
  IHostingEnvironment env,
  ILoggerFactory loggerFactory)
 {
  if (env.IsDevelopment())
  {
  app.UseDeveloperExceptionPage();
  }
  //添加控制臺輸出
  loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  loggerFactory.AddDebug();

  loggerFactory.AddNLog();//添加NLog
  NLog.LogManager.LoadConfiguration($@"{env.ContentRootPath}/nlog.config");//指定NLog的配置文件

  app.UseMvc();
 }

配置NLog的配置文件

<?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="Warn"
 internalLogFile="internal-nlog.txt">-->
 <targets>
 <target name="allfile" xsi:type="File" fileName="./logs/${shortdate}/all.log" layout="${longdate}|${message} ${exception}" />
 <target name="debugfile" xsi:type="File" fileName="./logs/${shortdate}/debug.log" layout="${longdate}|${message} ${exception}" />
 <target name="infofile" xsi:type="File" fileName="./logs/${shortdate}/info.log" layout="${longdate}|${message} ${exception}" />
 <target name="warnfile" xsi:type="File" fileName="./logs/${shortdate}/warn.log" layout="${longdate}|${message} ${exception}" />
 <target name="errorfile" xsi:type="File" fileName="./logs/${shortdate}/error.log" layout="${longdate}|${message} ${exception}" />
 <target name="fatalfile" xsi:type="File" fileName="./logs/${shortdate}/fatal.log" layout="${longdate}|${message} ${exception}" />
   <target name="network" xsi:type="Network" address="udp://chinacloudapp.cn:4561" layout="Development|${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />//將日志通過網(wǎng)絡(luò)輸出
 <target name="debuge" xsi:type="Console"/>//將日志輸出到控制臺
 </targets>

 <rules>
 <logger name="*" minlevel="Trace" writeTo="allfile,debuge" />
 <logger name="*" level="Info" writeTo="infofile" />
 <logger name="*" level="debug" writeTo="debugfile" />
 <logger name="*" level="warn" writeTo="warnfile" />
 <logger name="*" level="error" writeTo="errorfile" />
 <logger name="*" level="fatal" writeTo="fatalfile" />
 
 </rules>
</nlog>

xsi:type=“File”存儲日志為文件格式 ,

xsi:type="Console"表示為控制臺輸出。

fileName="./logs/${shortdate}/all.log"表示存儲文件路徑。


layout="${longdate}|${message} ${exception}"表示為文件內(nèi)容的布局。


rules標(biāo)簽下面表示,對應(yīng)等級的日志寫到對應(yīng)target中。如

<logger name="*" level="Info" writeTo="infofile" />表示等級為Info的日志寫到target名稱為infofile的文件中。


<logger name="*" minlevel="Trace" writeTo="allfile,debuge" />表示日志等級大于Trace的日志寫到target名稱為allfile和debuge(控制臺輸出)中。


同樣在使用的時候,只需要在用到的地方注入ILogger,就可以使用了。

關(guān)于“.Net Core項目如何添加日志功能詳解”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

本文名稱:.NetCore項目如何添加日志功能詳解-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article42/ehghc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化網(wǎng)站設(shè)計公司軟件開發(fā)建站公司營銷型網(wǎng)站建設(shè)網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營