在ASP.NET中,如何输出日志呢?有一个NLog的库可以很方便的实现。只需要三步就可以完成:
1.引入Nlog相关的dll;
2.在项目的配置文件中,进行如下配置:
<configuration>
<configSections>
<!--Nlog-->
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<!--Nlog-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="logDirectory" value="${basedir}/logs"/>
<targets>
<default-target-parameters xsi:type="File" keepFileOpen="false"/>
<target name="file" xsi:type="File" fileName="${logDirectory}/${shortdate}.txt" layout="${longdate} | ${level} | ${message}"/>
<target name="file1" xsi:type="console" layout="${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file,file1"/>
</rules>
</nlog>
</configuration>
3.如此就可以在项目中使用如下代码了:
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();