首页 > 其他分享 >Nlog日志

Nlog日志

时间:2022-12-27 09:33:29浏览次数:44  
标签:exception layout NLog Nlog 日志 true logger

第一步:下载Newget包

 

 

第二步: 新建配置文件 nlog.config (xml文件 名字保持一直)

<?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-AspNetCore.txt">

<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>

<!-- the targets to write to -->
<targets>
<!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-AspNetCore-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" />

<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}" />

<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
<target xsi:type="Console" name="lifetimeConsole" layout="${MicrosoftConsoleLayout}" />
</targets>

<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />

<!--Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />

<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>

第三步:修改Progrom

using NLog;
using NLog.Web;

var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Debug("init main");

try
{

// Add services to the container.
builder.Logging.ClearProviders();
builder.Host.UseNLog();

 

}
catch (Exception exception)
{
// NLog: catch setup errors
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}

第四步:在程序中引用 (Microsoft.Extensions.Logging命名空间)

//构造函数注入
private readonly ILogger<UserController> _logger;

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

[HttpGet]
public IActionResult Get()
{
//测试
_logger.LogInformation("测试Nlog");
_logger.LogError( "异常测试");
_logger.LogDebug("啊实打实");
return Ok("ok");
}

 

标签:exception,layout,NLog,Nlog,日志,true,logger
From: https://www.cnblogs.com/ak-yn11/p/17007347.html

相关文章

  • 【问题记录】【SpringBoot】【Swagger】启动的时候,有一堆Swagger冲突的日志,看着不爽 G
    1 问题描述swagger版本,启动时候报一堆冲突,虽然没什么影响但是看着不是很爽,以及默认的Swagger主页会有别的Controller信息。<springfox-swagger2.version>2.9.2</s......
  • IIS日志存放目录
    ​用户每打开一次网页,iis都会记录用户IP、访问的网页地址、访问时间、访问状态等信息,这些信息保存在iis日志文件里,方便网站管理员掌握网页被访问情况和iis服务器运行......
  • 【日志框架】SLF4J教程、Log4j漏洞、Logback与log4j比较
    一、参考资料​​SLF4J简介-SLF4J教程™​​​​logback和log4j比较,前者是后者改良,logback配置详解_zbajie001的博客-​​Log4j爆“核弹级”漏洞,Flink、Kafka等至少十多个......
  • MapReduce实战之过滤日志及自定义日志输出路径案例(自定义OutputFormat)
    1)需求      过滤输入的log日志中是否包含atguigu      (1)包含atguigu的网站输出到e:/atguigu.log      (2)不包含atguigu的网站输出到e:/other.log2)输入......
  • 一个简单的c++写日志类
    在开发当中有的直接调试很方便,有的就不太好调试,所以我经常使用写日志以及输出Debug信息来进行开发。尽管我比较喜欢使用以输出Debug的信息的方式来跟踪程序。因为直接一句......
  • Sql Server 压缩数据库日志文件的方法解析
    SqlServer日志_log.ldf文件太大,数据库文件有500g,日志文件也达到了500g,占用磁盘空间过大,且可能影响程序性能,需要压缩日志文件,下面小编给大家讲解下SqlServer压缩数据......
  • 基于AD Event日志监测域内信息探测行为
    01、简介当攻击者获得内网某台域内服务器的权限,就会以此为起始攻击点,尽可能地去收集域的信息,以获取域控权限作为内网的终极目标。例如,攻击者会在内网中收集域管理员用户列......
  • QT实现异步日志
    #include<QCoreApplication>#include<QDateTime>#include<QDir>#include<QFile>#include<QProcess>#include<QTextStream>#include<QTimer>#defineROLLSIZE1000*......
  • Android--观察APP运行日志以及APP的工程目录结构解释
    运行日志Log:d--便于跟踪调试APP开发基础APP的运行环境第一种情况,就是在Androidstudio软件客户端上面使用模拟器运行APP第二种情况,就是使用真实的手机运行APP程序......
  • LIS之nlogn做法
    一.LIS题目传送门给定一个长为\(n\)的序列\(a_i\),求这个序列的最长单调上升子序列长度。\(1\)≤\(a_i\)≤\(n\)≤\(10^5\)二.解析\(n^2\)解法在此不再赘述,显然此方......