Hello, log4net

Source: Internet
Author: User

Hello, log4net
----- Use log4net in C #

1. Download log4net (Google log4net)
2. Unzip log4net
3. Run vs to create a new C # windows application.
4. Add reference log4net
5. Create an application configuration file app. config (the specific content is attached to it)
6. Open form1.cs,
Add a line in namespace [Assembly: log4net. config. domconfigurator (watch = true)]
Add a static variable to the form1 class
Private Static readonly log4net. ilog log = log4net. logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype );

7. Add a button. Add a line of log. Warn ("Hello! ");

8. Run the program. Click the button.

OK, open bin/debug/log-file.txt, You can see "hello ".

Attachment. App. config

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<! -- Register a section handler for the log4net section -->
<Configsections>
<Section name = "log4net" type = "system. configuration. ignoresectionhandler"/>
</Configsections>
<Deleetask>
<! -- To enable internal log4net logging specify the following encryption ettings key -->
<! -- <Add key = "log4net. Internal. debug" value = "true"/> -->
</Appsettings>
<! -- This section contains the log4net configuration settings -->
<Log4net>
<! -- Define some output appenders -->
<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "log-file.txt"/>
<! -- Example Using environment variables in Params -->
<! -- <Param name = "file" value = "$ {TMP} // log-file.txt"/> -->
<Param name = "appendtofile" value = "true"/>
<! -- An alternate output encoding can be specified -->
<! -- <Param name = "encoding" value = "unicodefffe"/> -->
<Layout type = "log4net. layout. patternlayout">
<Param name = "Header" value = "[header]/R/N"/>
<Param name = "footer" value = "[footer]/R/N"/>
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x] & lt; % x {auth} & gt; -% m % N "/>
</Layout>
<! -- Alternate layout using XML
<Layout type = "log4net. layout. xmllayout"/> -->
</Appender>

<! -- Setup the root category, add the appenders and set the default level -->
<Root>
<Level value = "all"/>
<Appender-ref = "logfileappender"/>
<! -- <Appender-ref = "A"/> -->
</Root>

<! -- Specify the level for some specific categories -->
<Logger name = "slog4net. form1">
<! -- <Appender-ref = "B"/> -->
<Level value = "all"/>
<Appender-ref = "rollinglogfileappender"/>
</Logger>
</Log4net>
</Configuration>


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.