This article is for the practical driver to quickly get started log4net the most basic functions, a total of 4 steps, 3 minutes to fix.
One, add Log4net.dll reference, can use NuGet installation or direct reference file
Second, add the configuration
Add the following configuration in App. Config or Web. config. Note <configsections> must be placed at the top.
1 <Configuration>2 <configsections>3 < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net " />4 </configsections>5 <log4net>6 <!--defining the output to a file -7 <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">8 <!--define where files are stored -9 <filevalue= "Logs\"/>Ten <Appendtofilevalue= "true"/> One <Rollingstylevalue= "Date"/> A <Datepatternvalue= "' log-' yyyyMMdd '. txt '"/> - <Staticlogfilenamevalue= "false"/> - <Layouttype= "log4net." Layout.patternlayout "> the <!--output Format - - <Conversionpatternvalue= "%ndatetime:%d%nthread:%t%npriority:%p%nmessage:%m%n"/> - </Layout> - </Appender> + <Root> - <!--record log as file Form - + <Appender-refref= "Rollinglogfileappender"/> A </Root> at </log4net> - </Configuration>
Iii. Project AssemblyInfo.cs Add the following configuration to initialize the log4net configuration file. This step must not be missed, do not miss, do not miss out.
[assembly:log4net. Config. xmlconfigurator (Watch = true)]
Iv. using the Log function
1 usinglog4net;2 3 namespaceSample4 {5 classlogtest6 {7 StaticILog logger = Logmanager.getlogger ("LogName");8 9 Public Static voidWriteinfo ()Ten { OneLogger. Info ("Write log Test"); A } - } -}
Log4net the fastest experience