How to Use nlog in winform and Asp.net

Source: Internet
Author: User
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:

  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>

Use the following in the log writing class:

  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 button#click (Object sender, eventargs E)
  18. {
  19. Log. Info ("hi ");
  20. }
  21. }
  22. }

3. Use webform and Asp.net

Add nlog. dll as well, but put the configuration file in Web. config this time.
As follows:

  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>

Note: I put the log file under app_data. This cannot be downloaded.

The usage is as follows: 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. }

For more detailed usage, refer:

  1. Nlog Doc http://www.nlog-project.org/
  2. Nlog article series http://www.cnblogs.com/dflying/archive/2006/12/15/593158.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.