Some time ago, the project needed to record various operation logs and errors during runtime, and its own implementation was also possible. However, based on time considerations, the open-source log4net was used. Log4net is a sister of log4j and is a very powerful log record assembly.
The usage is as follows:
1. Add log4net DLL reference
2. Add the following content to the <configuration> node in Web. config:
<Configuration>
<Configsections>
<! -- Add log4net reference to configuration options -->
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net>
<Appender name = "logfile" type = "log4net. appender. rollingfileappender, log4net">
<! -- <Param name = "file" value = "C: \ srvlog.txt"/> define a file to save logs. The generated file log.txt is in the same directory as the EXE file generated by the application. -->
<Param name = "file" value = "serverlog \ srvlog-"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "rollingstyle" value = "date"/>
<Param name = "datepattern" value = "yyyy-mm-DD & quot;. log & quot;"/>
<Param name = "staticlogfilename" value = "false"/>
<Layout type = "log4net. layout. patternlayout, log4net">
<! -- Define the output style -->
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C-% m % N"/>
<Param name = "Header" value = "& #13; & #10; ------------------------ serverlog -------------------------- & #13; & #10;"/>
<Param name = "footer" value = "& #13; & #10; ---------------------------------------------------------- & #13; & #10;"/>
</Layout>
</Appender>
<Appender>
<! -- A configuration file can have many appender, and an appender section is equivalent to a log output media or method. -->
</Appender>
<Logger name = "logapp">
<! -- Define the logger object name as logapp to facilitate use in code. <logger>
You can choose not to configure configuration items -->
<Level value = "all"/> <! -- Define the output information level to all levels, including fatal. Error. Warn. info. debug -->
</Logger>
<Root>
<! -- Define the log output mode and level -->
<Level value = "info"/>
<Appender-ref = "logfile"/> <! -- Select the file output. Note the appender name corresponding to the bold part. -->
</Root>
</Log4net>
</Configuration>
3. Read related configurations
Private Static ilog log = NULL;
Log4net. config. xmlconfigurator. Configure (New fileinfo (appdomain. currentdomain. basedirectory + "Web. config "));
Log = logmanager. getlogger (system. reflection. methodbase. getcurrentmethod (). declaringtype); // reflection is used here
4. Call related function log
Log. Info (content );