您的位置:首页 > 编程语言 > ASP

在ASP.NET中如何输出日志

2016-03-03 12:54 621 查看
在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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: