当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core与NLog集成的完整步骤

ASP.NET Core与NLog集成的完整步骤

2018年08月08日  | 移动技术网IT编程  | 我要评论

爱死了昨天吉他谱,最新网络歌手,数码果

前言

一直很喜欢 nlog 的简洁和扩展性,所以准备将 asp.net core 提供的默认日志提供程序替换成 nlog。

nlog 是一个跨平台的 .net 日志组件。

nlog 遵从 bsd license,即允许商业应用且完全开放源代码。任何人都可以免费使用并对其进行测试,然后通过邮件列表反馈问题以及建议。

下面话不多说了,来一起看看详细的介绍吧。

步骤 1

在项目的project.json中添加依赖nlog.extensions.logging:

"dependencies": {
 "nlog.extensions.logging": "1.0.0-*"
}

或者通过nuget程序包管理器添加。

步骤 2

在asp.net core的启动类startup的configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)方法中添加:

// using nlog.extensions.logging;

loggerfactory.addnlog();
//needed for non-netstandard platforms: configure nlog.config in your project root
env.configurenlog("nlog.config");

步骤 3

在项目目录下添加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"
  throwconfigexceptions="true"
  internalloglevel="warn"
  internallogtotrace="true"
  internallogfile="appdata/logs/nloginternal.log">

 <!-- 
 - 日志文件被放置于 appdata/logs 文件夹中,文件名为 {日志类目}.log 或 {日志类目}.err
 - 日志按天存档,放置于 appdata/logs/archives/{日志类目} 文件夹中,文件名为 {日期}.log 或 {日期}.err
 -->
 <targets>
 <!-- 通过 system.diagnostics.trace 输出由 ef 6 产生的数据库日志可以在 vs 输出窗口看到 -->
 <target name="xxx.entities.appdbcontext.databaselog.trace"
   xsi:type="trace"
   layout="${message}"
   />
 <!-- 在控制台输出由 ef 6 产生的数据库日志 -->
 <target name="xxx.entities.appdbcontext.databaselog.console"
   xsi:type="console"
   layout="${message}"
   />
 <!-- 在日志文件输出由 ef 6 产生的数据库日志 -->
 <target name="xxx.entities.appdbcontext.databaselog.file"
   xsi:type="file"
   layout="${message}"
   encoding="utf-8"
   archivenumbering="date"
   archiveevery="day"
   archivedateformat="yyyy-mm-dd"
   archivefilename="appdata/logs/archives/xxx.entities.appdbcontext.database/{#}.log"
   filename="appdata/logs/xxx.entities.appdbcontext.database.log" 
   />
 <!-- 常规的 trace 输出,调试时可以在 vs 输出窗口看到 -->
 <target name="trace"
   xsi:type="trace"
   layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}: ${newline}${message}${onexception:inner=${newline}${exception:format=tostring}}${newline}" />
 <!-- 常规的控制台输出 -->
 <target name="console" 
   xsi:type="console"
   layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}: ${newline}${message}${onexception:inner=${newline}${exception:format=tostring}}${newline}" />
 <!-- 常规的日志文件输出 -->
 <target name="log_file"
   xsi:type="file"
   layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}: ${newline}${message}${onexception:inner=${newline}${exception:format=tostring}}${newline}"
   encoding="utf-8"
   archivenumbering="date"
   archiveevery="day"
   archivedateformat="yyyy-mm-dd"
   archivefilename="appdata/logs/archives/${filesystem-normalize:inner=${logger}}/{#}.log"
   filename="appdata/logs/${filesystem-normalize:inner=${logger}}.log" />

 <!-- 约定以 err 为文件后缀的日志文件记录了程序输出的警告或者错误。 -->
 <target name="error_log_file"
   xsi:type="file"
   layout="[${longdate}] ${pad:padding=-5:inner=${level:uppercase=true}} ${logger}: ${newline}${message}${onexception:inner=${newline}${exception:format=tostring}}${newline}"
   encoding="utf-8"
   archivenumbering="date"
   archiveevery="day"
   archivedateformat="yyyy-mm-dd"
   archivefilename="appdata/logs/archives/${filesystem-normalize:inner=${logger}}/{#}.err"
   filename="appdata/logs/${filesystem-normalize:inner=${logger}}.err" />

 </targets>

 <rules>
  <!-- 记录所有日志级别不低于 warn 的日志到日志文件 -->
  <logger name="*" minlevel="warn" writeto="error_log_file" />
  <!-- 记录 ef 生成的 sql 语句 -->
  <logger name="xxx.entities.appdbcontext.databaselog" minlevel="debug" 
    writeto="xxx.entities.appdbcontext.databaselog.trace,xxx.entities.appdbcontext.databaselog.console,xxx.entities.appdbcontext.databaselog.file" final="true" />
 <!-- 除非调试需要,把 .net core 程序集的 debug 输出都屏蔽 -->
  <logger name="microsoft.*" minlevel="info" writeto="console,trace,log_file" final="true" />
  <!-- 除非调试需要,把系统的 debug 输出都屏蔽 -->
  <logger name="system.*" minlevel="info" writeto="console,trace,log_file" final="true" />
  <!-- 记录应用程序的 debug 输出 -->
  <logger name="myapplication.*" minlevel="debug" writeto="trace,console,log_file" />
 </rules>
</nlog>

nlog 配置文件属性解读:

  • autoreload 是否监视配置文件的变化并自动加载。
  • throwconfigexceptions 是否在配置出错时抛出异常。
  • internalloglevel nlog 内部日志级别。
  • internallogtotrace 是否将 nlog 内部日志输出到 trace。
  • internallogfile nlog 内部日志输出到文件的路径。

步骤 4

在project.json文件中的publishoptions.include节内添加"nlog.config":

"publishoptions": {
 "include": [
 "wwwroot",
 "views",
 "areas/**/views",
 "appsettings.json",
 "web.config",

 "nlog.config"
 ]
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网