NLog在MVC中使用

  NLog在MVC中使用

在site根目录新建NLog.config

<?xml version="1.0"?>
<configuration>
  <nlog throwExceptions="false">
    <variable name="logDirectory" value="${basedir}/logs" />
    <targets async="true">
      <!--<default-target-parameters
        type="File"
        archiveAboveSize="50485760"
        maxArchiveFiles="50"
        archiveNumbering="Rolling"
        keepFileOpen="false"
        layout="${longdate} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"/>-->
      <target name="f1"
              type="File"
              fileName="${logDirectory}/${shortdate}.html">
        <layout>
          <![CDATA[
              level:${level}<br />
              longdate:${longdate}<br />
              message:${message}<br />
          ]]>
        </layout>
      </target>
      <target name="f2"
           type="Mail"
           header="----------"
           footer="----------"
           layout="${longdate} ${level} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"
           html="false"
           addNewLines="true"
           subject="${message}"
           to=""
           from=""
           body="${longdate} ${level} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"
           smtpUserName="UserName"
           enableSsl="false"
           smtpPassword="Password"
           smtpAuthentication="Basic"
           smtpServer="smtp.hooyes.com"
           smtpPort="25" />
    </targets>
    <rules>
      <logger name="aotu" levels="Debug,Error" writeTo="f1" />
      <!--<logger name="*" minlevel="Error" writeTo="f2" />-->
    </rules>
  </nlog>
</configuration>
View Code

 

<?xml version="1.0"?>
<configuration>
  <nlog throwExceptions="false">
    <variable name="logDirectory" value="${basedir}/logs" />
    <targets async="true">
      <!--<default-target-parameters
        type="File"
        archiveAboveSize="50485760"
        maxArchiveFiles="50"
        archiveNumbering="Rolling"
        keepFileOpen="false"
        layout="${longdate} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"/>-->
      <target name="f1"
              type="File"
              header="------------------------------Start------------------------------"
              footer="------------------------------End------------------------------"
              fileName="${logDirectory}/${shortdate}.txt"
              layout="${longdate} [${level}]]] ${message}"
              />
      <target name="f2"
           type="Mail"
           header="----------"
           footer="----------"
           layout="${longdate} ${level} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"
           html="false"
           addNewLines="true"
           subject="${message}"
           to=""
           from=""
           body="${longdate} ${level} ${callsite} ${message} ${exception:format=Message, Type, ShortType, ToString, Method, StackTrace}"
           smtpUserName="UserName"
           enableSsl="false"
           smtpPassword="Password"
           smtpAuthentication="Basic"
           smtpServer="smtp.hooyes.com"
           smtpPort="25" />
    </targets>
    <rules>
      <logger name="aotu" levels="Debug,Error" writeTo="f1" />
      <!--<logger name="*" minlevel="Error" writeTo="f2" />-->
    </rules>
  </nlog>
</configuration>

添加LogHelper class

/***********************************************************************
 *  Copyright (C) 2013 Newegg Corporation
 *  All rights reserved.
 * 
 *  Author:  Aotu Li (Aotu.E.Li@Newegg.com)
 *  Date:    12/7/2013 3:59:11 PM
 *  Usage: 
 * 
 *  RevisionHistory
 *  Date         Author               Description
 * 
 * ***********************************************************************/

using NLog;

namespace Mvc_NLogDEMO
{
    public class LogHelper
    {
        private static Logger logger;
        private static string logRuleName = "aotu";
        static LogHelper()
        {
            logger = LogManager.GetLogger(logRuleName);
            logger.LoggerReconfigured += logger_LoggerReconfigured;
        }

        static void logger_LoggerReconfigured(object sender, System.EventArgs e)
        {
            logger = LogManager.GetLogger(logRuleName);
        }

        public static void WirtError(string message)
        {
            logger.Log(LogLevel.Error, message);
        }

        public static void WirtDebug(string message)
        {
            logger.Log(LogLevel.Debug, message);
        }
    }
}
View Code

 

public class LogHelper
    {
        private static Logger logger;
        private static string logRuleName = "aotu";
        static LogHelper()
        {
            logger = LogManager.GetLogger(logRuleName);
            logger.LoggerReconfigured += logger_LoggerReconfigured;
        }

        static void logger_LoggerReconfigured(object sender, System.EventArgs e)
        {
            logger = LogManager.GetLogger(logRuleName);
        }

        public static void WirtError(string message)
        {
            logger.Log(LogLevel.Error, message);
        }

        public static void WirtDebug(string message)
        {
            logger.Log(LogLevel.Debug, message);
        }
    }

参考文档:https://github.com/nlog/nlog/wiki

posted @ 2013-12-07 18:05  1280  阅读(1028)  评论(0编辑  收藏  举报
不想当将军的厨师不是好裁缝
友荐云推荐
QQ:63219702