Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数据库中,
关于nlog和log4net的使用选择不做过多讨论,这里说说nlog的简单使用,
log4net使用地址:http://blog.csdn.net/xuwei_xuwei/article/details/29179221
1.NLog下载
下载对应的zip包即可
地址:https://github.com/NLog/NLog/releases/tag/2.1.0
2.winform中使用
添加nlog.dll的引用,然后在nlog.dll的文件夹下创建nlog.dll.nlog
添加如下内容:
<?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" >
<!-- 日志目标 -->
<targets>
<target name="console" xsi:type="ColoredConsole"
layout="${date:format=HH/:mm/:ss}|${level}|${stacktrace}|${message}"/>
<target name="file" xsi:type="File" fileName="${basedir}/log.txt"
layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="debug" writeTo="console"></logger>
<logger name="*" minlevel="debug" writeTo="file"></logger>
</rules>
</nlog>
代码中使用
NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Info("hello ,world");
3.asp.net中使用
配置文件web.config中做如下配置
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false">
</compilation>
<authentication mode="Windows"/>
</system.web>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 日志目标 -->
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/App_Data/log.txt"
layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>
代码中调用
NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Info("hello ,world");
nlog官网地址:http://www.nlog-project.org/
关于nlog和log4net的使用选择不做过多讨论,这里说说nlog的简单使用,
log4net使用地址:http://blog.csdn.net/xuwei_xuwei/article/details/29179221
1.NLog下载
下载对应的zip包即可
地址:https://github.com/NLog/NLog/releases/tag/2.1.0
2.winform中使用
添加nlog.dll的引用,然后在nlog.dll的文件夹下创建nlog.dll.nlog
添加如下内容:
<?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" >
<!-- 日志目标 -->
<targets>
<target name="console" xsi:type="ColoredConsole"
layout="${date:format=HH/:mm/:ss}|${level}|${stacktrace}|${message}"/>
<target name="file" xsi:type="File" fileName="${basedir}/log.txt"
layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="debug" writeTo="console"></logger>
<logger name="*" minlevel="debug" writeTo="file"></logger>
</rules>
</nlog>
代码中使用
NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Info("hello ,world");
3.asp.net中使用
配置文件web.config中做如下配置
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false">
</compilation>
<authentication mode="Windows"/>
</system.web>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 日志目标 -->
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/App_Data/log.txt"
layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>
代码中调用
NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
log.Info("hello ,world");
nlog官网地址:http://www.nlog-project.org/