.Net Core 中使用NLog作为日志中间件
⒈安装相关依赖
NLog
NLog.Web.AspNetCore
⒉在项目的根目录中创建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="Info"
internalLogFile="c:\temp\internal-nlog.txt"> <!-- 启用asp.net核心布局渲染器- -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions> <!-- 要写入的目标 -->
<targets>
<!-- 将日志写入到文件中 -->
<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}" /> <!-- 另一个文件日志,只有自己的日志。使用一些ASP.NET核心渲染器 -->
<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>
<!--所有日志,包括来自Microsoft的日志-->
<logger name="*" minlevel="Trace" writeTo="allfile" /> <!--跳过非关键的Microsoft日志,因此只记录自己的日志-->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
⒊更改配置文件属性

⒋修改Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NLog.Web; namespace AutoMapperCore
{
public class Program
{
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception e)
{
logger.Error(e, "Stopped program because of exception");
throw;
}
finally
{
NLog.LogManager.Shutdown();
} } public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); }).UseNLog();
}
}
⒌配置appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Information"
}
},
"AllowedHosts": "*"
}
⒍在代码中注入ILogger写日志
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapperTest.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; namespace AutoMapperCore.Controllers
{
public class UsersController : Controller
{
private readonly IMapper _mapper;
private readonly ILogger<UsersController> _logger;
public UsersController(IMapper mapper, ILogger<UsersController> logger)
{
this._mapper = mapper;
this._logger = logger;
}
public IActionResult Index()
{
UsersInputDto input = new UsersInputDto()
{
id = , firstname = "fan", lastname = "qi", uname = "fanqisoft", pwd = "admin", enabled =
};
_logger.LogInformation("Dto转换实体对象成功!");
Users users = _mapper.Map<Users>(input);
return View();
}
}
}
.Net Core 中使用NLog作为日志中间件的更多相关文章
- ASP.NET Core:ASP.NET Core中使用NLog记录日志
一.前言 在所有的应用程序中,日志功能是不可或缺的模块,我们可以根据日志信息进行调试.查看产生的错误信息,在ASP.NET Core中我们可以使用log4net或者NLog日志组件来实现记录日志的功能 ...
- [转]asp.net5中使用NLog进行日志记录
本文转自:http://www.cnblogs.com/sguozeng/articles/4861303.html asp.net5中使用NLog进行日志记录 asp.net5中提供了性能强大的日志 ...
- .net core 中使用Log4net输出日志到Mysql数据库中
.net core 中使用Log4net输出日志到数据库中去 1.使用Nuget安装log4net 和 mysql.data 2.设置log4net 的配置文件 log4net.config 可以设置 ...
- 在.NET Core中使用Exceptionless分布式日志收集框架
一.Exceptionless简介 Exceptionless 是一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,ASP.NET Core,Web Api,Web Forms,WPF, ...
- Asp.Net Core中使用NLog记录日志
2019/10/28, Asp.Net Core 3.0, NLog 4.6.7, NLog.Web.AspNetCore 4.9.0 摘要:NLog在asp.net网站中的使用,NLog日志写入数据 ...
- ASP.NET Core中使用GraphQL - 第二章 中间件
前文:ASP.NET Core中使用GraphQL - 第一章 Hello World 中间件 如果你熟悉ASP.NET Core的中间件,你可能会注意到之前的博客中我们已经使用了一个中间件, app ...
- asp.net5中使用NLog进行日志记录
asp.net5中提供了性能强大的日志框架,本身也提供了几种日志记录方法,比如记录到控制台或者事件中等,但是,对大部分程序员来说,更喜欢使用类似log4net或者Nlog这种日志记录方式,灵活而强大. ...
- 在 ASP.NET Core 中使用 Serilog 进行日志记录
目录 从 NuGet 安装 Serilog 在 Main函数 中配置 Serilog 在项目中使用 Serilog 进行日志输出 从 NuGet 安装 Serilog 核心的包是 Serilog 和 ...
- Asp.net Core中使用NLog,并封装成公共的日志方法
1.安装NLog "NLog.Extensions.Logging": "1.0.0-rtm-alpha4" 2.配置NLog public void Conf ...
随机推荐
- ZOJ - 3591 NIM
ZOJ - 3591NIM 题目大意:给你n,s,w和代码,能生成长度为n的序列,问异或和不为0的子序列有多少个? 这是个挂羊头卖狗肉的题,和NIM博弈的关系就是要异或和不为0,一开始以博弈甚至循环节 ...
- Spring Boot教程(四十)使用Flyway来管理数据库版本
在上面的使用JdbcTemplate一文中,主要通过spring提供的JdbcTemplate实现对用户表的增删改查操作.在实现这个例子的时候,我们事先在MySQL中创建了用户表.创建表的过程我们在实 ...
- CodeForces–830A--二分,贪心
Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- python3 基本数据类型转换
'''基本数据类型1:虽然python中的变量不需要声明,但使用时必须赋值 1.整形变量 2.浮点型变量 3.字符型2:可以一个给多个变量赋值,也可以多个给多个变量赋值3:python3中有6个标准数 ...
- Pycharm查看源代码的问题
linear = torch.nn.Linear() # 无法查看Linear的源代码 from torch.nn import Linear linear = Linear() # 可以查看Line ...
- java判断一个字符串是否为数字型
摘自:https://blog.csdn.net/qq_42133100/article/details/92158507 方法一:用JAVA自带的函数(只能判断正整数 ) 2 public stat ...
- Why are C# structs immutable?
Compiler Error CS1612 Cannot modify the return value of 'expression' because it is not a variable cl ...
- nuros安全报告
We believe that great technology should benefit everyone. Self-driving vehicles promise to save live ...
- LC 967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- 内存数据库:Redis与Memcached的区别
Redis与Memcached的区别 传统MySQL+ Memcached架构遇到的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都 ...