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>
<?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 ([email protected])
* 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);
}
}
}
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

04-18 12:54