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>