zoukankan      html  css  js  c++  java
  • NLog使用方法

    一、软件

    网站:http://www.nlog-project.org/
    下载:http://sourceforge.net/project/showfiles.php?group_id=116456
    说明:如果是.Net 2.0使用, 请下载nlog-1.0-net-2.0.zip
          里面的bin目录下有多个,c# 使用nlog.dll
    文件:nlog.dll 
    大小:248K
    版本:1.0.0.505

    二、 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>
        <logger name="*" minlevel="debug" writeTo="console"></logger>
        <logger name="*" minlevel="debug" writeTo="file"></logger>
    </rules>
    </nlog>

    在你要写日志的类中如下使用:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace nlogDemo
    {
        public partial class Form1 : Form
        {
            NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                log.Info("hi");
            }
        }
    }

    三、WebForm,Asp.net下的使用方法

    同样添加nlog.dll,不过这次的配置文件放到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>
          <logger name="*" minlevel="Debug" writeTo="file" />
        </rules>
    </nlog>
    </configuration>

    注意:我把日志文件放到了App_Data下面了。这里的话不能下载的。

    使用方法如下,default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using DevExpress.XtraCharts;
    
    public partial class _Default : System.Web.UI.Page 
    {
        NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
        protected void Page_Load(object sender, EventArgs e)
        {
            log.Info("hi");
        }   
    }


    更加详细的使用可以参考:

    NLog Doc http://www.nlog-project.org/
    NLog文章系列 http://www.cnblogs.com/dflying/archive/2006/12/15/593158.html

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/greenerycn/archive/2008/10/25/3142806.aspx http://lierle.qzone.qq.com/

  • 相关阅读:
    NSGA3理解(NSGA3算法及其MATLAB版本实现)
    基于分解的多目标进化优化MOEA/D之切比雪夫方法代码
    基于分解的多目标进化优化MOEA/D三种聚合函数的理解
    NSGA-II in MATLAB 源码中文注释(1)(转载)
    我的个人总结
    Unity Networking API文档翻译(二):The High Level API
    Unity Networking API文档翻译(一):Networking概述
    Unity3D独立游戏开发日记(二):摆放建筑物
    Unity3D独立游戏开发日记(一):动态生成树木
    如何申请TexturePacker
  • 原文地址:https://www.cnblogs.com/lierle/p/2708124.html
Copyright © 2011-2022 走看看