How to Use nlog in winform and webform
Greenerycn
I. Software
Website: http://www.nlog-project.org/
Download: http://sourceforge.net/project/showfiles.php? Group_id = 116456
Note: For. NET 2.0, download nlog-1.0-net-2.0.zip.
There are multiple bin directories, and C # uses nlog. dll
File: nlog. dll
Size: 248 K
Version: 1.0.0.505
Ii. Use in winform
Add nlog. dll references and create nlog. dll. nlog in the nlog. dll folder.
The content is as follows:
- <? 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>
Use the following in the log writing class:
- 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 button#click (Object sender, eventargs E)
- {
- Log. Info ("hi ");
- }
- }
- }
3. Use webform and Asp.net
Add nlog. dll as well, but put the configuration file in Web. config this time.
As follows:
- <? 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>
Note: I put the log file under app_data. This cannot be downloaded.
The usage is as follows: 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 ");
- }
- }
For more detailed usage, refer:
- Nlog Doc http://www.nlog-project.org/
- Nlog article series http://www.cnblogs.com/dflying/archive/2006/12/15/593158.html