1.在Nuget上搜索 NLog.Extensions.Logging 安装最新版

2.添加日志配置文件,在项目指定目录下添加配置文件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="c:\temp\internal-nlog.txt"> <!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${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.将nlog.config添加到project.json配置文件中(若配置文件在指定config目录下,可以添加config/*.config)

  "publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config",
"nlog.config"
]
},

4.在startup.cs文件Configure中添加

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
…………


5.记录日志方法
(1) public class HomeController : Controller
{
private static Logger Logger = LogManager.GetCurrentClassLogger();
public IActionResult Index()
{
Logger.Info("Index page says hello");
return View();
}
………… (2) public abstract class BaseController<T> : Controller
{
protected ILogger<T> Logger { get; set; }
    public BaseController(ILogger<T> logger, IOptions<ApplicationSiteConfig> appConfig)
{
this.Logger = logger;
}
…………


参考:https://github.com/NLog/NLog.Extensions.Logging 

最新文章

  1. 【Juicer】 一个高效、轻量的前端 (Javascript) 模板引擎
  2. iOS 因为reason: &#39;Pushing the same view controller instance more than once is not supported而奔溃(上)
  3. 理论经典:TCP协议的3次握手与4次挥手过程详解
  4. VS2015中GLAUX库的链接问题
  5. ios ZBar扫二维码奇奇怪怪的错误
  6. matlab使用reshape时按照列优先原则取元素和摆放元素
  7. [置顶] LLVM每日谈之十五 LLVM自带的examples
  8. Python 保存爬行动物捕捉网页
  9. javascript(js)创建对象的模式与继承的几种方式
  10. 兄弟连教育分享:用CSS实现鼠标悬停提示的方法
  11. Delphi Sysem.JSON 链式写法(转全能中间件)
  12. LeetCode算法题-Longest Palindrome(五种解法)
  13. [JXOI2018]游戏
  14. 简单QR分解之Gram-Schmit正交化&amp;&amp;Householder变换&amp;&amp;Givens Rotation变换&amp;&amp;计算步骤
  15. bzoj千题计划210:bzoj2642 | Poj3968 | UVALive 4992| hdu 3761 Jungle Outpost
  16. ApiPost的环境变量的定义和使用「ApiPost环境变量」
  17. Scala中的foreach forall exists map函数及其区别
  18. 找到多个与名为“xxx”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。
  19. docker stack 部署容器监控方案(cAdvisor、Prometheus、Grafana)
  20. sql developer连接mysql

热门文章

  1. JdbcTemplate中queryForObject的EmptyResultDataAccessException问题
  2. 在注册表中无Python3.5安装路径的情况下安装pywin32-
  3. Word2vec 模型载入(tensorflow)
  4. NewBluePill源码学习
  5. SpringMvc的创建流程以及2种加载配置文件的方式
  6. 2016 ccpc 网络选拔赛 F. Robots
  7. 向nginx发送reopen信号以重新打开日志文件
  8. Testing with a mocking framework (EF6 onwards)
  9. 五分钟理解一致性哈希算法(consistent hashing)
  10. Self-introduction (自我介绍)