C#--NLog日志组件使用(NLogHelper类)
1,引用NLog.dll
2,添加配置文件NLog.config
【2.1】配置文件
<?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="Trace"
internalLogFile="D:\work\log.txt">-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true">
<targets>
<!-- Log in a separate thread, possibly queueing up to
5000 messages. When the queue overflows, discard any
extra messages-->
<!-- write logs to file 【方式1】:存入文件-->
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${level:uppercase=true} ${event-context:item=Action} ${message} ${event-context:item=Amount} ${stacktrace}" />
</target>
<!-- write log message to database 【方式2】:存入数据库 --><!--
<target name="db" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
<target type="Database" dbProvider="mssql" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=EFinance;Persist Security Info=True;User ID=sa;Password=123456;">
<commandText>
INSERT INTO Log(Timestamp,Level,Message,Action,Amount,StackTrace) VALUES(@time_stamp, @level, @message, @action, @amount, @stacktrace);
</commandText>
--><!-- database connection parameters --><!--
<parameter name="@time_stamp" layout="${date}" />
<parameter name="@level" layout="${level:uppercase=true}" />
<parameter name="@message" layout="${message}" />
<parameter name="@action" layout="${event-context:item=Action}" />
<parameter name="@amount" layout="${event-context:item=Amount}" />
<parameter name="@stacktrace" layout="${stacktrace}" />
</target>
</target>
--><!--write log message to Visual Studio Output 【方式3】:debug调试,显示在输出--><!--
<target name="debugger" xsi:type="Debugger" layout="NLog: ${date:format=HH\:mm\:ss} | ${level:uppercase=true:padding=-5} | ${message}" />-->
</targets>
<rules>
<!--rules:规则配置-->
<!--TRACE,DEBUG,INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Trace" writeTo="debugger" />
<!--INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Info" writeTo="db" />
<!--DEBUG,INFO,WARN,ERROR,FATAL-->
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
【2.2】 特别注意,NLog.config的属性》复制到输出目录》始终复制

3,NLogHelper类
namespace AutomaticStoreMotionDal
{
public class NLogHelper
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
#region Debug
public static void Debug(string msg, params object[] args)
{
logger.Debug(msg, args);
}
public static void Debug(string msg, Exception err)
{
logger.Debug(err, msg);
}
#endregion
#region Info
public static void Info(string msg, params object[] args)
{
logger.Info(msg, args);
}
public static void Info(string msg, Exception err)
{
logger.Info(err, msg);
}
#endregion
#region Warn
public static void Warn(string msg, params object[] args)
{
logger.Warn(msg, args);
}
public static void Warn(string msg, Exception err)
{
logger.Warn(err, msg);
}
#endregion
#region Trace
public static void Trace(string msg, params object[] args)
{
logger.Trace(msg, args);
}
public static void Trace(string msg, Exception err)
{
logger.Trace(err, msg);
}
#endregion
#region Error
public static void Error(string msg, params object[] args)
{
logger.Error(msg, args);
}
public static void Error(string msg, Exception err)
{
logger.Error(err, msg);
}
#endregion
#region Fatal
public static void Fatal(string msg, params object[] args)
{
logger.Fatal(msg, args);
}
public static void Fatal(string msg, Exception err)
{
logger.Fatal(err, msg);
}
#endregion
}
}
4,项目中使用
NLogHelper.Info("登录成功");
5,查看日志


浙公网安备 33010602011771号