Asp. NET site Log4net Records

Source: Internet
Author: User
Tags log4net

The requirement of the project is to use the log4net record, and to record the user's login information and status, to add, modify, delete the operation log, then I use 4 texts per day to record the log


Steps:

1. Configuration files

<span style= "FONT-SIZE:14PX;" ><configuration> <configSections> <!--log4net connection--> <section name= "Log4net" Type= "Log4net. Config.log4netconfigurationsectionhandler,log4net "/> </configSections> <!--log4net configuration start -<log4net> <!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, All-and <!--Set Root logger l        Evel to ERROR and its appenders-<root> <level value= "INFO"/> </root> <logger name= "Syslogin" > <level value= "INFO"/> <appender-ref ref= "Systemlogin" /> </logger> <logger name= "Sysadd" > <level value= "INFO"/> < Appender-ref ref= "Systemadd"/> </logger> <logger name= "Sysupdate" > <level valu E= "INFO"/> <appender-ref ref= "systemupdate"/> </logger> <lOgger name= "Sysdelete" > <level value= "INFO"/> <appender-ref ref= "Systemdelete"/> </logger> <!--This is the log-in information-<appender name= "Systemlogin" type= "log4net.            Appender.rollingfileappender,log4net "> <!--file path--<param name=" file "value=" log/login/"/>            <param name= "Appendtofile" value= "true"/> <param name= "Rollingstyle" value= "Date"/> <!--a file only stores 1MB size, beyond automatically creating new--<param name= "maximumFileSize" value= "1MB"/> <para M name= "Datepattern" value= "" Login_logs_ "yyyyMMdd". txt "/> <!--is only allowed to write to one file--<param Nam E= "Staticlogfilename" value= "false"/> <!--minimum locking model to allow multiple processes to write to the same file--<param name= "Locking Model "Value=" log4net. Appender.FileAppender.MinimalLock "/> <layout type=" log4net. Layout.patternlayout,log4net "> <param namE= "Conversionpattern" value= "%message%n"/> <param name= "Header" value= "/> <par Am Name= "Footer" value= ""/> </layout> </appender> <!--This is record add-and-&lt ; Appender name= "Systemadd" type= "log4net. Appender.rollingfileappender,log4net "> <param name=" File "value=" log/operation/"/> <para M name= "Appendtofile" value= "true"/> <param name= "Rollingstyle" value= "Date"/> <!--a file only Store 1MB size, exceed auto Create new--<param name= "maximumFileSize" value= "1MB"/> <param name= "Datepattern "Value=" "Membermng_logs_" yyyyMMdd ". txt" "/> <!--only allowed to write to one file--<param name=" Staticlogfil Ename "value=" false "/> <!--minimum locking model to allow multiple processes to write to the same file--<param name=" Lockingmodel "value=" l Og4net. Appender.FileAppender.MinimalLock "/> <layout type=" log4net. Layout.patternlayout,lOg4net "> <param name=" Conversionpattern "value="%message%n "/> <param name=" Head          ER "value=" "/> <param name=" Footer "value=" "/> </layout> </appender> <!--This is the record modified--<appender name= "systemupdate" type= "log4net. Appender.rollingfileappender,log4net "> <param name=" File "value=" log/operation/"/> <para M name= "Appendtofile" value= "true"/> <param name= "Rollingstyle" value= "Date"/> <!--a file only Store 1MB size, exceed auto Create new--<param name= "maximumFileSize" value= "1MB"/> <param name= "Datepattern "Value=" "Customermng_logs_" yyyyMMdd ". txt" "/> <!--only allowed to write to one file--<param name=" Staticlogf Ilename "value=" false "/> <!--minimum locking model to allow multiple processes to write to the same file--<param name=" Lockingmodel "value= "Log4net.            Appender.FileAppender.MinimalLock "/><layout type= "log4net.                Layout.patternlayout,log4net "> <param name=" Conversionpattern "value="%message%n "/>        <param name= "Header" value= ""/> <param name= "Footer" value= ""/> </layout> </appender> <!--This is record deleted--<appender name= "Systemdelete" type= "log4net. Appender.rollingfileappender,log4net "> <param name=" File "value=" log/operation/"/> <para M name= "Appendtofile" value= "true"/> <param name= "Rollingstyle" value= "Date"/> <!--a file only Store 1MB size, exceed auto Create new--<param name= "maximumFileSize" value= "1MB"/> <param name= "Datepattern "Value=" "Customermng_logs_" yyyyMMdd ". txt" "/> <!--only allowed to write to one file--<param name=" Staticlogf Ilename "value=" false "/> <!--minimum locking model to allow multiple processes to write to the same file--<param name=" Lockingmodel "value= "Log4net. appender.fiLeappender.minimallock "/> <layout type=" log4net.                Layout.patternlayout,log4net "> <param name=" Conversionpattern "value="%message%n "/>        <param name= "Header" value= ""/> <param name= "Footer" value= ""/> </layout> </appender> <appender name= "ConsoleApp" type= "log4net. Appender.consoleappender,log4net "> <layout type=" log4net.            Layout.patternlayout,log4net "> <param name=" Conversionpattern "value="%d [%t]%-5p%c-%m%n "/> </layout> </appender> </log4net> <!--log4net configuration End--></conf Iguration></span>


2. Calling the class

Using Log4net; [Assembly:log4net. Config.xmlconfigurator (Watch = True)]namespace bll{//<summary>//Log4net record class///</summary> PU  Blic class Loghelper {//<summary>///Record login information///</summary> public static            void Writelogin (String message) {ILog Loginfo = Logmanager.getlogger ("Syslogin"); if (loginfo. isinfoenabled) {Loginfo.            Info (message); }}///<summary>///Record add information///</summary> public static void Writeadd (                String message) {try {ILog loginlog = Logmanager.getlogger ("Sysadd");                if (loginlog.isinfoenabled) {loginlog.info (message);          }} catch {}}///<summary>//Record modification information///</summary> public static void Writemodify (StrinG message) {try {ILog membermnglog = Logmanager.getlogger ("Sysupdate");                if (membermnglog.isinfoenabled) {membermnglog.info (message);         }} catch {}}///<summary>//Record deletion///</summary>  public static void Writedelete (String message) {try {ILog Customerlog                = Logmanager.getlogger ("Sysdelete");                if (customerlog.isinfoenabled) {customerlog.info (message); }} (Catch {}}}}

3. As for calling the class

Loghelper.writelogin ("Login Successful! 2015-03-31 Registered Person: admin ");

Then we can go to the project root path to find log/login/can view the TXT file




Asp. NET site Log4net Records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.