码迷,mamicode.com
首页 > 其他好文 > 详细

NLog的使用

时间:2014-08-28 22:35:26      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:blog   http   使用   io   ar   for   文件   数据   2014   

bubuko.com,布布扣

 

1 先安装NLog程序包 如上图 :

2 建了一个调用的类 NLogHelper 内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NLog;

namespace VideoConvert
{
    public static class LogHelper
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public static void Write(LogLevel logLevel, string message)
        {
            logger.Log(logLevel, message);
        }
    }

3 新建NLog的配置文件(也可以在app.config或web.config里添加信息)NLog.config 路径在项目的根目录  控制台程序在exe目录下  内容举例如下:

<?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">

  <!--
  See http://nlog-project.org/wiki/Configuration_file
  for information on customizing logging rules and outputs.
   -->
  <targets async="true">
    <target xsi:type="File" name="logfile" fileName="${basedir}/logs/${shortdate}.log"   //路由对应的文件地址
            layout="${longdate} ${uppercase:${level}} ${message}" />
    <target xsi:type="File" name="errorlogfile" fileName="${basedir}/logs/error_${shortdate}.log"
              layout="${longdate} ${uppercase:${level}} ${message}" />
    <target xsi:type="File" name="signinfile" fileName="${basedir}/logs/${shortdate}.log"
              layout="${longdate},${message}" />
  </targets>
  <rules>
    <logger name="*" levels="Info" writeTo="logfile" />    //错误类型对应的不同路由
    <logger name="*" levels="Error" writeTo="errorlogfile" />
    <logger name="*" levels="Trace" writeTo="signinfile" />
  </rules>
</nlog>

 

4 就可以在程序中使用 2步中的   LogHelper.Write(LogLevel.Trace, string.Format("Message: ")); 来记录日志 

     loglevel.trace 对应 config文件中的levels=Trace   可以有多种

 

我使用的原因:

  进程重定向输出的两种方式 一种同步输出 一种异步输出  (同步造成了死锁  异步造成日志文件访问冲突)

        //WriteSWFError(p.StandardError.ReadToEnd());  //同步  数据量大 输出管道满 导致无法读写造成死锁 不可用
                    //WriteSWFError(p.StandardOutput.ReadToEnd());
                    p.BeginOutputReadLine();  //异步
                    p.BeginErrorReadLine();

NLog的使用

标签:blog   http   使用   io   ar   for   文件   数据   2014   

原文地址:http://www.cnblogs.com/orangeLemon/p/NLog.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!