關(guān)于.Net?6?添加NLog的方法
創(chuàng)建一個.Net 6 Demo項目
引入NLog包

添加項目配置文件nlog.config
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" >
<!-- the targets to write to -->
<targets>
<!--單個文件過大會導(dǎo)致寫入效率下降,可配置單個文件的最大容量-->
<target name="File" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File"
fileName="${basedir}/logs/${shortdate}.log"
layout="${date:yyyy-MM-dd HH\:mm\:ss} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}"
archiveAboveSize="10240"
archiveEvery="Day"
/>
</target>
<!-- write logs to file -->
<!--<target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />-->
<!--<target xsi:type="Console" name="logconsole"
layout="${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}" />-->
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--<logger name="*" levels="Debug,Info,Warn,Error" writeTo="File" />-->
<logger name="*" minlevel="Debug" maxlevel="Error" writeTo="File" />
</rules>
</nlog>layout布局:
${longdate} 格式:2022-01-21 17:00:28.4860

后面的毫秒不是我想要的,可以自定義:date:yyyy-MM-dd HH\:mm\:ss

NLog等級
Trace:最常見的記錄信息,一般是普通輸出
Debug:同樣是記錄信息,出現(xiàn)的頻率比Trace少,一般是調(diào)試程序
Info:信息類型的消息
Warn:警告消息
Error:錯誤消息
自上而下,等級遞增
指定特定等級:level="Warn"
指定多個等級:levels="Debug,Info" 以逗號分隔
指定等級范圍:minlevel="Info" maxlevel="Error"
<rules>
<!--<logger name="*" levels="Debug,Info,Warn,Error" writeTo="File" />-->
<logger name="*" minlevel="Debug" maxlevel="Error" writeTo="File" />
</rules> 添加工具類 Logger
using NLog;
namespace TestNet6.Utilities
{
public class Logger
{
NLog.Logger _logger { get; set; }
private Logger(NLog.Logger logger)
{
_logger = logger;
}
public Logger(string name) : this(LogManager.GetLogger(name))
public static Logger Default { get; private set; }
static Logger()
Default = new Logger(LogManager.GetCurrentClassLogger());
#region Dedub
public void Debug(string msg, params object[] args)
_logger.Debug(msg, args);
public void Debug(string msg, Exception e)
_logger.Debug(e, msg);
#endregion
#region Info
public void Info(string msg, params object[] args)
_logger.Info(msg, args);
public void Info(string msg, Exception e)
_logger.Info(e, msg);
#region Trace
public void Trace(string msg, params object[] args)
_logger.Trace(msg, args);
public void Trace(string msg, Exception e)
_logger.Trace(e, msg);
#region Warn
public void Warn(string msg, params object[] args)
_logger.Warn(msg, args);
public void Warn(string msg, Exception e)
_logger.Warn(e, msg);
#region Error
public void Error(string msg, params object[] args)
_logger.Error(msg, args);
public void Error(string msg, Exception e)
_logger.Error(e, msg);
}
}添加測試Controller
using Microsoft.AspNetCore.Mvc;
using TestNet6.Utilities;
namespace TestNet6.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
public string Test()
{
Logger.Default.Info("", Request);
return "Test String";
}
}
}最后為了路由有效,還需要添加路由映射
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
//添加路由映射
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();OK,運行測試


到此這篇關(guān)于.Net 6 添加NLog的文章就介紹到這了,更多相關(guān).Net 6 添加NLog內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET使用xslt將xml轉(zhuǎn)換成Excel
本文介紹利用Excel軟件生成格式,提取和精簡之后制作成xslt文件,將xml導(dǎo)入,以xslt為模板,生成新的Excel文件的過程。2016-05-05
合并網(wǎng)頁中的多個script引用實現(xiàn)思路及代碼
為了更好的進行封裝,每個實現(xiàn)不同功能的js代碼應(yīng)該有自己的js文件,這樣如果一個網(wǎng)頁中引用了多個js文件會很難管理,所以就出現(xiàn)了合并js這以說,感興趣的朋友不妨參考下本文希望對你有所幫助2013-02-02

