First, download the Log4net.dll (Baidu Cloud inside) file, and then add references in the project.
Second, add the Log4net.dll parameter in the AssemblyInfo.cs file.
" Web. config " " Config " true)]
Iii. configuring Web. config
<configuration> <configSections> <section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler,log4net"/> </configSections> <log4net> <!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, all-and <!--Set Root logger level-to-ERROR and its appenders-<root> <level value=" All"/> <appender-ref ref="Sysappender"/> </root> <!--Print Only messages of the level DEBUG or aboveinchThe packages--and <logger name="Weblogger"> <level value=" All"/> </logger>
//This configuration is to write the log to a text file<appender name="Sysappender"Type="log4net. Appender.rollingfileappender,log4net"> <param name="File"Value="logger/"/>//Log storage location (the value here is a logger, which indicates that a file named logger is created in the project folder.) It can also be value= "C:\log.txt")<param name="Appendtofile"Value="true"/>//whether to append to file<param name="Rollingstyle"Value="Date"/>//Transform in the form of a date<param name="Datepattern"Value="" Logs_"yyyymmdd". txt""/>//Generate format; generate one log per day<param name="Staticlogfilename"Value="false"/>//log file name, whether fixed or not<layout type="log4net. Layout.patternlayout,log4net"> <param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n"/>//These 3 lines represent the format of the log output, if you do not like the style, you can view the following style , modify it yourself<param name="Header"Value="& #xA,----------------------header--------------------------& #xA;"/> <param name="Footer"Value="& #xA,----------------------footer--------------------------& #xA;"/> </layout> </appender>
//This configuration is to write the log to a text file
</log4net> <system.web> <compilation debug="true"/> </system.web > </configuration>
Output style:
%M (message): Output log message, such as Ilog.debug (...) A message for the output
%n (New line): NewLine
%d (datetime): Outputs the moment at which the current statement runs
%r (Run time): The number of milliseconds that the output program consumes from running to executing to the current statement
%T (thread ID): The thread ID where the current statement is located
%p: The current priority level of the log, which is debug, INFO, WARN ... such as
%c (Class): The name of the current log object, for example:
%f (file): The filename in which the output statement resides.
%l: The line number where the output statement is located.
% Number: Indicates the minimum length of the item, if not enough, padding with a space, such as "%-5level" indicates that the minimum width of the level is 5 characters, and if the actual length is not 5 characters, fill with a space.
With these things, you can combine your favorite output format content.
Create a Global.asax page, or use it in a try catch in the method you need
Right-click project → add → new project → Select the global application class in the Web.
In the Global.asax.cs file, locate the Application_Error method, and add the code that writes the error log.
C # uses log4net to log logs