The log component of the project is a must-have and is required in any project. This facilitates the early development test and facilitates project maintenance later in the project. A good log component of a C # project is log4net, and I'll take a rough look at how the program projects log4net in the configuration.
The first step is to install log4net in the project. Open the project, locate the "References" button in the project directory in "Solution Explorer " and click "Manage NuGet packages (N) ..." to pop up a management window, search to log4net click Install.
The second step is to configure the following in the Web. config (or app. config) file:
<name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
<log4net> <!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, all - <!--Set Root Logger level-to-ERROR and its appenders - <Root> < Levelvalue= "All" /> <Appender-refref= "Sysappender" /> </Root> <!--Print only messages of level DEBUG or above in the packages - <Loggername= "Weblogger"> < Levelvalue= "DEBUG" /> </Logger> <Appendername= "Sysappender"type= "log4net." Appender.rollingfileappender,log4net "> <paramname= "File"value= "app_data/" /> <paramname= "Appendtofile"value= "true" /> <paramname= "Rollingstyle"value= "Date" /> <paramname= "Datepattern"value= "" Logs_"yyyymmdd". txt" " /> <paramname= "Staticlogfilename"value= "false" /> <Layouttype= "log4net." Layout.patternlayout,log4net "> <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" /> </Layout> </Appender> <Appendername= "ConsoleApp"type= "log4net." Appender.consoleappender,log4net "> <Layouttype= "log4net." Layout.patternlayout,log4net "> <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" /> </Layout> </Appender> </log4net>
The third step is to add the following code to the Application_Start method in the Global.asax.cs file:
// Configure Log4log4net. Config.XmlConfigurator.Configure (new System.IO.FileInfo (Server.MapPath ("~/web.config ")));
Fourth step, add a line of code to the AssemblyInfo.cs in the project:
// log4net reading the configuration from the configuration file " Config " true)]
The final step is how the project code is applied, and the following is a simple use:
Log4net. ILog log = log4net. Logmanager. GetLogger (GetType ()); log. info (" This is a log message record "); log. Error (" This is the wrong message ");
The following figure is the log generated in the project:
Keep Learning and growing. *** *****************************
Configuration and use of the logging tool log4net