ASP.NET Core使用NLog输出日志记录

这篇文章介绍了ASP.NET Core使用NLog输出日志记录的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ASP.NET Core 中的日志记录

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1

日志级别:Trace -》Debug-》 Information -》Warning-》 Error-》 Critical

级别包含范围由大到小 ,如 Trace 就包含了所有信息。

基础用法

public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("你访问了首页"); _logger.LogWarning("警告信息"); _logger.LogError("错误信息"); return View(); } }

日志事件 ID

    public class LoggingEvents { public const int GenerateItems = 1000; public const int ListItems = 1001; public const int GetItem = 1002; public const int InsertItem = 1003; public const int UpdateItem = 1004; public const int DeleteItem = 1005; public const int GetItemNotFound = 4000; public const int UpdateItemNotFound = 4001; }
	_logger.LogWarning(LoggingEvents.GetItemNotFound, "GetById({ID}) NOT FOUND", 100);

结果:
warn: TodoApi.Controllers.TodoController[4000]
GetById(100) NOT FOUND

NLog 输出到文件

https://github.com/NLog/NLog.web/wiki

asp.net core 2

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

Create a nlog.config file.

     

复制到输出目录:始终复制

Update program.cs

这里 nlog-all-.log 是记录所有日志,nlog-own-.log 记录跳过Microsoft 开头的类库输出的相关信息,剩下的信息。

参考:https://www.0133.cn/article/142992.htm

到此这篇关于ASP.NET Core使用NLog输出日志记录的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是ASP.NET Core使用NLog输出日志记录的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程