C # log4net Application

Source: Internet
Author: User
Tags log4net
ArticleDirectory
    • Add log4net to the Asp.net website
    • Download Sample Code

There are already many articles about log4net on the Internet.

We have been using it on the web. We used to do winform.ProgramConfiguration once failed! This time, according to the instructions on the Internet, the configuration was successful. We hereby record it!

Add log4net to the C # winform Program

1. Reference log4net. dll

2. Add app. config as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <log4net> <appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender "> <Param name =" file "value =" log \ log.txt "/> <Param name =" appendtofile "value =" true "/> <Param name =" maxsizerollbackups" value = "100"/> <Param name = "maximumfilesize" value = "2 MB"/> <Param name = "rollingstyle" value = "size"/> <Param name = ""staticlogfilename" value = "true"/> <layout type = "log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" %-5 p % d [% C] % m % N "/> </layout> </appender> <Root> <level value = "all"/> <appender-ref = "rollinglogfileappender"/> </root> </log4net> </configuration>

You can find the specific settings on the Internet.

3. Add properties/assemblyinfo. CS[Assembly: log4net. config. domconfigurator (watch = true)]

4. InCodeAdd logs

Define logPrivate Static log4net. ilog log = log4net. logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype );

Write logsLog. Info ("open form ");

5. If you want to record all unhandled exceptions, modify program. CS as follows: 

Using system; using system. collections. generic; using system. windows. forms; using system. reflection; namespace exceptiontest {static class program {static log4net. ilog log = log4net. logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype); // <summary> // main entry point of the application. /// </Summary> [stathread] Static void main () {// capture the unprocessed exception application. setunhandledexceptionmode (unhandledexceptionmode. catchexception); application. threadexception + = new system. threading. threadexceptioneventhandler (application_threadexception); appdomain. currentdomain. unhandledexception + = new unhandledexceptioneventhandler (currentdomain_unhandledexception); application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false); application. run (New form1 ();} static void application_threadexception (Object sender, system. threading. threadexceptioneventargs e) {log. error (E. exception); // throw new exception ("unknown thread exception", E. exception); MessageBox. show (E. exception. message, "thread exception", messageboxbuttons. OK, messageboxicon. error); application. exit ();} static void currentdomain_unhandledexception (Object sender, unhandledexceptioneventargs e) {exception EX = E. exceptionobject as exception; log. error (Ex); MessageBox. show (ex. message, "application exception", messageboxbuttons. OK, messageboxicon. error); application. exit ();}}}
Add log4net to the Asp.net website

1. Reference log4net. dll

2. Configure web. config as follows

<? XML version = "1.0"?> <Configuration> <configsections> <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net "/> </configsections> <etettings/> <connectionstrings/> <system. web> <compilation DEBUG = "true"/> <Authentication mode = "Windows"/> </system. web> <! -- Log --> <log4net> <appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender "> <Param name =" file "value =" D: \ test \ log.txt "/> <Param name =" appendtofile "value =" true "/> <Param name =" maxsizerollbackups "value =" 100 "/> <Param name =" maximumfilesize "value =" 2 MB "/> <Param name =" rollingstyle "value =" size "/> <Param name =" staticlogfilename "value =" true "/> <Layout type = "log4net. layout. patternlayout "> <Param name =" conversionpattern "value =" %-5 p % d [% C] % m % N "/> </layout> </appender> <Root> <level value = "all"/> <appender-ref = "rollinglogfileappender"/> </root> </log4net> </configuration>

3. Add global. asax as follows:

 
Private Static log4net. ilog log = log4net. logmanager. getlogger ("log4net test website"); void application_start (Object sender, eventargs e) {// The Code log4net that runs when the application starts. config. domconfigurator. configure (); log. info ("website launch");} void application_error (Object sender, eventargs e) {// code exception EX = httpcontext when an unprocessed error occurs. current. server. getlasterror (); log. error ("unprocessed exceptions", ex );}

Record all site unprocessed exceptions to log4net.

4. How to Use

Define logPrivate Static log4net. ilog log = log4net. logmanager. getlogger (methodbase. getcurrentmethod (). declaringtype );

Write logsLog. Info ("website startup ");

Download Sample Code

Download: http://files.cnblogs.com/zjfree/ErrorLog.rar

environment: win2003 + vs2005 + C #

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.