Log4net usage record and log4net record

Source: Internet
Author: User

Log4net usage record and log4net record

1. Reference log4net. dll in the program

2. Add-create the configuration file Log4Net. config and select "always copy" in "Copy to output directory" in the file attributes. The file content is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- Log configuration --> <configSections> <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> </configSections> <log4net> <root> </root> <! -- Only one logger is instantiated in the program and its name is LogHelper --> <logger name = "LogHelper"> <! -- Output ALL information. Only Info and Error are used here --> <level value = "ALL"/> <! -- Two media names, corresponding to errors and information --> <appender-ref = "ErrorRollingFileAppender"/> <appender-ref = "InfoRollingFileAppender"/> </logger> <! -- Info media settings --> <appender name = "InfoRollingFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- Defines the file storage location. Info and Error are stored in different folders, easy to understand --> <file value = "Log \ Info \"/> <appendToFile value = "true"/> <rollingStyle value = "Date"/> <datePattern value = "yyyyMMdd'.txt '"/> <staticLogFileName value = "false"/> <param name = "MaxSizeRollBackups" value = "100"/> <layout type = "log4net. layout. patternLayout "> <! -- Output format --> <! -- Example: record time: 14:03:00, 540 thread ID: [1] Log Level: ERROR content --> <conversionPattern value = "% newline % n record time: % date % n thread ID: [% thread] % n log level: %-5 level % n % m % n "/> </layout> <! -- Output level INFO --> <filter type = "log4net. filter. levelRangeFilter "> <param name =" LevelMin "value =" INFO "/> <param name =" LevelMax "value =" INFO "/> </filter> </appender> <! -- Error media settings --> <appender name = "ErrorRollingFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- Define the file storage location --> <file value = "Log \ Error \"/> <appendToFile value = "true"/> <rollingStyle value = "Date"/> <datePattern value = "yyyyMMdd'.txt '"/> <staticLogFileName value = "false"/> <param name = "MaxSizeRollBackups" value = "100"/> <layout type = "log4net. layout. patternLayout "> <! -- Output format --> <! -- Example: record time: 13:59:00, 560 thread ID: [1] Log Level: INFO Information Content --> <conversionPattern value = "% newline % n record time: % date % n thread ID: [% thread] % n log level: %-5 level % n % m % n "/> </layout> <! -- Set the output level ERROR --> <filter type = "log4net. filter. levelRangeFilter "> <param name =" LevelMin "value =" ERROR "/> <param name =" LevelMax "value =" ERROR "/> </filter> </appender> </log4net> </configuration>

3. Add code to AssemblyInfo. cs to call Log4Net. config:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

4. Implement LogHelper with the following code:

Public class LogHelper {private static log4net. ILog log = log4net. logManager. getLogger ("LogHelper "); /// <summary> /// sort the exception information /// </summary> /// <param name = "error"> </param> /// <returns> </returns> private static string Msg (Exception error) {string str = ""; if (error! = Null) {str = string. format ("exception type: {0} \ r \ n exception message: {1} \ r \ n exception information: {2} \ r \ n", error. getType (). name, error. message, error. stackTrace);} return str ;} /// <summary> /// output exception information /// </summary> /// <param name = "t"> </param> /// <param name = "ex"> </param> public static void LogError (Exception ex) {log. error (Msg (ex ));} /// <summary> /// output the log information /// </summary> /// <param name = "str"> </param> public static void LogInfo (string str) {log. info (str );}}

5. Now, you can easily use Log4Net to record information in the code.

Try {throw new Exception ("this is a new Exception");} catch (Exception ex) {LogHelper. logError (ex);} finally {LogHelper. logInfo ("this is Info ");}

 

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.