您的位置:首页 > 其它

Net Core使用日志 NLog

2017-07-10 15:29 274 查看
  一:使用Nuget执行命令下载NLog相关依赖

    

              Install-Package NLog.Extensions.Logging -Pre

              Install-Package NLog.Web.AspNetCore

     

  二.1:创建好Nlog配置文件



          新建一个xml文件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="nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

<target xsi:type="File" name="ownFile-web" fileName="nlog-my-${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>


  二.2:Startup.cs中添加使用的服务

     



public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

loggerFactory.AddNLog();//添加NLog
env.ConfigureNLog("nlog.config");//读取Nlog配置文件

//..............
}


      



   [b]三:使用日志
[/b]

       



//获得日志的实例
static Logger Logger = LogManager.GetCurrentClassLogger();

public IActionResult Index()
{
Logger.Info("普通信息日志-----------");
Logger.Debug("调试日志-----------");
Logger.Error("错误日志-----------");
Logger.Fatal("异常日志-----------");
Logger.Warn("警告日志-----------");
Logger.Trace("跟踪日志-----------");
Logger.Log(NLog.LogLevel.Warn, "Log日志------------------");

return View();
}


      日志的位置默认是在bin\Debug下面

       


          其中nlog-all包含的内比较多,nlog-my中就只包含了我们记录日志的内容,看上去比较清晰

          






        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: