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将日志写到文件的更多相关文章

  1. Asp.Net Core NLog 将日志输出到数据库以及添加LayoutRenderer的支持

    在这之前打算用Apache的Log4Net,但是发现其AdoNetAppender方法已经不存在了,无法使用配置文件直接输出到数据库了,因此我便改用了NLog框架. 一.对项目添加NLog 通过Nug ...

  2. ASP.NET Core 2.1 : 十二.内置日志、使用Nlog将日志输出到文件

    应用离不开日志,虽然现在使用VS有强大的调试功能,开发过程中不复杂的情况懒得输出日志了(想起print和echo的有木有),但在一些复杂的过程中以及应用日常运行中的日志还是非常有用. ASP.NET ...

  3. 第七节:Asp.Net Core内置日志和整合NLog(未完)

    一. Asp.Net Core内置日志 1. 默认支持三种输出方式:控制台.调试(底部输出窗口).EventSource,当然也可以在Program类中通过logging.ClearProviders ...

  4. 玩转ASP.NET Core中的日志组件

    简介 日志组件,作为程序员使用频率最高的组件,给程序员开发调试程序提供了必要的信息.ASP.NET Core中内置了一个通用日志接口ILogger,并实现了多种内置的日志提供器,例如 Console ...

  5. ASP.NET Core 中的日志记录

    目录 内置日志的使用 使用Nlog 集成ELK 参考 内置日志的使用 Logger 是 asp .net core 的内置 service,所以我们就不需要在ConfigureService里面注册了 ...

  6. ASP.NET Core 异常处理与日志记录

    1. ASP.NET Core 异常处理与日志记录 1.1. 异常处理 1.1.1. 异常产生的原因及处理 1.1.2. ASP.NET Core中启动开发人员异常页面 1.2. 日志记录 1.2.1 ...

  7. asp.net core 集成 log4net 日志框架

    asp.net core 集成 log4net 日志框架 Intro 在 asp.net core 中有些日志我们可能想输出到数据库或文件或elasticsearch等,如果不自己去实现一个 Logg ...

  8. asp.net core 系列 13 日志

    一.概述 ASP.NET Core 支持适用于各种内置和第三方日志记录, 供程序的日志记录 API,本文介绍了如何将日志记录 API 与内置提供程序一起使用.对于第三方日志记录提供程序使用,文章最后有 ...

  9. (14)ASP.NET Core 中的日志记录

    1.前言 ASP.NET Core支持适用于各种内置和第三方日志记录提供应用程序的日志记录API.本文介绍了如何将日志记录API与内置提供应用程序一起使用. 2.添加日志提供程序 日志记录提供应用程序 ...

随机推荐

  1. Runnable和Callable之间的区别

    Runnable和Callable之间的区别 1.Runnable任务执行后没有返回值:Callable任务执行后可以获得返回值 2.Runnable的方法是run(),没有返回值:Callable的 ...

  2. iis问题

    1.8.5版本的iis配置完成,不能打开网页显示503.原因是应用程序池,加载用户配置文件失败,然后应用程序池关闭了.关闭它就可以正常打开了. server2008,打不开网页,打开url显示目录的原 ...

  3. 一。Hibernate 开发流程

    一.hibernate和mybatis都是orm产品1.orm:object-realation-mapping对象关系映射 二.开发步骤1.导入相关jar 包括hibernate和oracle的驱动 ...

  4. SpringBoot+AOP整合

    SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...

  5. JS学习笔记:(一)浏览器页面渲染机制

    浏览器的内核主要分为渲染引擎和JS引擎.目前市面上常见的浏览器内核可以分为这四种:Trident(IE).Gecko(火狐).Blink(Chrome.Opera).Webkit(Safari).这里 ...

  6. servlet(6) 链接数据库

    一.servlet链接mysql步骤: 1.注册驱动器:Class.forName("com.mysql.jdbc.Driver"); 加载类并执行下面的静态语句块,将Driver ...

  7. Linux查看用户所属用户组

    1.查看当前用户所属用户组 [oracle@serverhl ~]$ groups oinstall dba 2.查看<user1>, <user2> 和 <user3& ...

  8. 本机Jenkins的使用

    1.启动jenkins: 命令:java -jar D:\toolspackage\jenkins\jenkins.war  打开jenkins网页:http://localhost:8080/ 2. ...

  9. git学习01- 下载安装&初始化库&提交

    1.windows下安装git,git官网下载安装包安装 2.本地创建一个目录,在目录下创建1个文本 readme.txt 3.cmd进入到该目录,执行git init,初始化git仓库 4.添加文件 ...

  10. 01Design and Analysis Algorithm Using Python-程振波

    1.(p14)比较两个数的大小 a = int(input('num:')) b = int(input('num:')) def getMax(a,b): if a>b : print('Th ...