asp.net core NLog将日志写到文件
1、安装Nlog包
Install-Package NLog.Extensions.Logging -Pre
2、在项目添加nlog.config文件

2.1、nlog.config
<?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"> <!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="Loggers/${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="ownFile-web" fileName="Loggers/${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" /> <target xsi:type="Null" name="blackhole" />
</targets> <rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
3、在项目中添加project.json 配置文件

3.2、project.json 文件内容
{
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config",
"Config/nlog.config" //加上nlog配置文件
]
}
}
4、在Startup.cs 中Configure方法添加如下代码
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactor)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} //此方法被LogManager.LoadConfiguration替代
//loggerFactor.ConfigureNLog("Config/nlog.config");
//加载Nlog的nlog.config配置文件
LogManager.LoadConfiguration("Config/nlog.config");
//添加NLog
loggerFactor.AddNLog(); app.UseMvcWithDefaultRoute(); app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
5、Controller 调用Nlog 方法
public class BlogController : Controller
{
private TestDBContext dBContext;
private readonly ILogger<BlogController> logger;
public BlogController(TestDBContext _dBContext, ILogger<BlogController> _logger)
{
dBContext = _dBContext;
logger = _logger;
} public IActionResult Index()
{
logger.LogInformation("你访问了首页55555");
logger.LogWarning("警告信息55555");
logger.LogError("错误信息55555"); var list = dBContext.Blog.Select(a => new BlogViewModel() {
CreateTime = a.CreateTime,
Id = a.BlogId,
Title = a.Title,
Url = a.Url
}).ToList();
return View(list);
}
}
6、运行项目成功,查看log
log 目录在bin 目录下


7、NLog GitHub 项目
https://github.com/linezero/Blog/tree/master/NETCoreLogging
asp.net core NLog将日志写到文件的更多相关文章
- Asp.Net Core NLog 将日志输出到数据库以及添加LayoutRenderer的支持
在这之前打算用Apache的Log4Net,但是发现其AdoNetAppender方法已经不存在了,无法使用配置文件直接输出到数据库了,因此我便改用了NLog框架. 一.对项目添加NLog 通过Nug ...
- ASP.NET Core 2.1 : 十二.内置日志、使用Nlog将日志输出到文件
应用离不开日志,虽然现在使用VS有强大的调试功能,开发过程中不复杂的情况懒得输出日志了(想起print和echo的有木有),但在一些复杂的过程中以及应用日常运行中的日志还是非常有用. ASP.NET ...
- 第七节:Asp.Net Core内置日志和整合NLog(未完)
一. Asp.Net Core内置日志 1. 默认支持三种输出方式:控制台.调试(底部输出窗口).EventSource,当然也可以在Program类中通过logging.ClearProviders ...
- 玩转ASP.NET Core中的日志组件
简介 日志组件,作为程序员使用频率最高的组件,给程序员开发调试程序提供了必要的信息.ASP.NET Core中内置了一个通用日志接口ILogger,并实现了多种内置的日志提供器,例如 Console ...
- ASP.NET Core 中的日志记录
目录 内置日志的使用 使用Nlog 集成ELK 参考 内置日志的使用 Logger 是 asp .net core 的内置 service,所以我们就不需要在ConfigureService里面注册了 ...
- ASP.NET Core 异常处理与日志记录
1. ASP.NET Core 异常处理与日志记录 1.1. 异常处理 1.1.1. 异常产生的原因及处理 1.1.2. ASP.NET Core中启动开发人员异常页面 1.2. 日志记录 1.2.1 ...
- asp.net core 集成 log4net 日志框架
asp.net core 集成 log4net 日志框架 Intro 在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 Logg ...
- asp.net core 系列 13 日志
一.概述 ASP.NET Core 支持适用于各种内置和第三方日志记录, 供程序的日志记录 API,本文介绍了如何将日志记录 API 与内置提供程序一起使用.对于第三方日志记录提供程序使用,文章最后有 ...
- (14)ASP.NET Core 中的日志记录
1.前言 ASP.NET Core支持适用于各种内置和第三方日志记录提供应用程序的日志记录API.本文介绍了如何将日志记录API与内置提供应用程序一起使用. 2.添加日志提供程序 日志记录提供应用程序 ...
随机推荐
- windows下编译Grafana前端
本次介绍一下Windows环境源码编译步骤. 准备 安装Go 1.8.1 安装NodeJS LTS 安装Git 安装golang开发环境: 参考链接:https://www.cnblogs.com/ ...
- timers模块
timers模块 var timers = require('timers'); function A() { //将A对象注册到定时器里 timers.enroll(); //进行激活,如果不激活, ...
- MacOS 10.13.6 下装xcode 流程
1.最好先安装brew https://github.com/Homebrew/brew/releases 自动安装脚本 /usr/bin/ruby -e "$(curl -fsSL htt ...
- upstream timed out (110: Connection timed out) while reading response header from upstream, client:
遇到的问题 之前没配置下面这段,访问时候偶尔会出现 504 gateway timeout,由于偶尔出现,所以不太好排查 proxy_connect_timeout 300s;proxy_read_t ...
- Java获取Linux和Window系统CPU、内存和磁盘总使用率的情况
这是一个工具类,获取的内容: CPU使用率:得到的是当前CPU的使用情况,这是算出的是两次500毫秒时间差的CPU使用率 内存使用率:[1 - 剩余的物理内存/(总的物理内存+虚拟内存) ] * 1 ...
- python基础 常见用法
1.python计时器timeit模块 1)timeit 模块定义了接收两个参数的Timer类,两个参数都是字符串. 参数1:要计时的语句或者函数 参数2:为参数1构建环境的导入语句 2)Timer对 ...
- Python scrapy爬虫数据保存到MySQL数据库
除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...
- Linux centos ansible
创建m01.backup.nfs.web01.web02 m01(172.16.1.61).backup(172.16.1.41).nfs(172.16.1.31).web01(172.16.1.7) ...
- 用WPS查看两篇word文档异同之处
写的合同,后期又有修改,电脑里同样名字的合同有好几个版本,不知道有什么不同,怎么办? 打开wps-->[审阅]-->[比较],剩下的按照提示很容易,略...
- HDU 2174 Bridged Marble Rings
题目:Bridged Marble Rings 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2174 题意:如图,要把所有灰色球移动到上圈,每次操作可以转 ...