NLog使用

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/
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值