承君此诺,必守一生!
2014年09月01日

1. 在项目中引用NLog.dll

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

  <!-- make sure to set 'Copy To Output Directory' option for this file -->
  <!-- go to http://nlog-project.org/wiki/Configuration_file for more information -->

  <targets>
        <target name="file" xsi:type="File" fileName="${basedir}/Logs/Log.txt" layout="${message}" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

3. 在项目中添加一个LogHelper类

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

namespace MyService.Helpers
{
    /// <summary>
    /// A helper used to note log message
    /// </summary>
    public class LogHelper
    {
        #region Properties
        /// <summary>
        /// Logger object
        /// </summary>
        private static Logger logger = LogManager.GetCurrentClassLogger();
        #endregion


        #region Methods
        /// <summary>
        /// Method to note info message
        /// </summary>
        /// <param name="logContent">message</param>
        public static void Info(String logContent)
        {
            logger.Info(logContent);
        }
        #endregion
    }
}

4.需要记录日志的地方直接使用

LogHelper.Event("log content");


 

posted on 2015-04-16 10:41  Viki.Feng  阅读(184)  评论(0编辑  收藏  举报