NLog 在winform和asp.net下使用快速攻略

來源:互聯網
上載者:User
NLog 在winform和webform下使用快速攻略

                       Greenerycn 2008-10-25

一、軟體

網站: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
內容如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  4. <targets>
  5.     <target name="console" xsi:type="ColoredConsole" 
  6.            layout="${date:format=HH/:mm/:ss}|${level}|${stacktrace}|${message}"/>
  7.     <target name="file" xsi:type="File" fileName="${basedir}/log.txt" 
  8.             layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}"/>
  9.   </targets>
  10.   <rules>
  11.     <logger name="*" minlevel="debug"  writeTo="console"></logger>
  12.     <logger name="*" minlevel="debug"  writeTo="file"></logger>
  13.   </rules>
  14. </nlog>

在你要寫日誌的類中如下使用:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace nlogDemo
  9. {
  10.     public partial class Form1 : Form
  11.     {
  12.         NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             log.Info("hi");
  20.         }
  21.     }
  22. }

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

同樣添加nlog.dll,不過這次的設定檔放到web.config中
如下:

  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <configSections>
  4.       <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  5.   </configSections>
  6.   <appSettings/>
  7.     <connectionStrings/>
  8.     <system.web>
  9.         <compilation debug="false">
  10.         </compilation>
  11.         <authentication mode="Windows"/>
  12.     </system.web>
  13.   <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  14.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  15.     <targets>
  16.       <target name="file" xsi:type="File" fileName="${basedir}/App_Data/log.txt"
  17.             layout="[${date:format=yyyy-MM-dd HH/:mm/:ss}][${level}] ${message} ${exception}" />
  18.     </targets>
  19.     <rules>
  20.       <logger name="*" minlevel="Debug" writeTo="file" />
  21.     </rules>
  22.   </nlog>
  23. </configuration>

注意:我把記錄檔放到了App_Data下面了。這裡的話不能下載的。

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using DevExpress.XtraCharts;
  7. public partial class _Default : System.Web.UI.Page 
  8. {
  9.     NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.         log.Info("hi");
  13.     }   
  14. }

更加詳細的使用可以參考:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.