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

最新文章

  1. Idea 开发 web项目
  2. Webkit 文字和背景效果
  3. Silverlight TabItem选中,未选中样式设置
  4. 推荐:一个个人开发者搞app赚钱之后的总结!有图有真相。
  5. android下的数据存储
  6. Message,MessageQueue,Looper,Handler ——由view.post(runnable想到的)
  7. C# CheckedListBox控件的使用方法
  8. HTML5 服务器发送事件(Server-Sent Events)介绍
  9. 进度条(ProgressBar)的功能与用法
  10. 虎牙直播弹幕转换字幕格式 基于Node.js 的 huya-danmu
  11. openssl命令行将pfx格式转.key和.crt文件,Apache适用
  12. oracle字符串提取记录
  13. sql语句进阶教程
  14. Java多线程编程模式实战指南之Promise模式
  15. python检测网络延迟
  16. 静态方法@staticmethod
  17. 解决ios10以上版本缩放问题
  18. iOS彩票项目--第六天,运用MVC思想搭建设置界面(非storyboard方法)
  19. MySQL Workbench 6.3CE 菜单汉化 xml
  20. sql server数据库查询超时报错

热门文章

  1. java邮箱发送
  2. 多租户通用权限设计(基于casbin)
  3. 安利一下workflowy和Dynalist
  4. Oracle 11g全表扫描以Direct Path Read方式执行
  5. SCOI2019酱油记
  6. C# .NET 0命令行安装Windows服务程序
  7. js数组中随机选取一个数值!!
  8. linux 下安装 php kafka 扩展
  9. Java中newInstance()和new()区别
  10. Java 静态内部类的加载时机