Overview
The Log4net library is the next very excellent open source logging component of .Net. It is a tool that helps programmers output log information to various targets (console, file, database, etc.).
It has the characteristics of: supporting most frameworks, outputting logs to multiple goals, hierarchical logging systems, using XML configuration, dynamic configuration, modular and scalable design, flexibility, and high performance.
The behavior of a logger is divided into 5 types of log levels, with priority from high to low:
1. FATAL (fatal error): Record errors in the system that can be used completely lose function, service stops, system crashes, etc. that make the system unable to continue running. For example, if the database cannot be connected, the system will experience a dead loop.
2. ERROR (general error): errors such as recording the system that causes system instability, some functions become confusing or some functions fail. For example, the data field is empty, the data operation cannot be completed, and the operation occurs exception, etc.
3. WARN: The information that records the system that does not affect the system's continued operation, but does not meet the normal operation conditions of the system, which may cause system errors. For example, the record content is empty, the data content is incorrect, etc.
4. INFO (General Information): Record the basic information that users should know during the system operation. For example, the service starts running, the function has opened an account, etc.
5. DEBUG (debug information): records all information, content or output of some key data content used by the system to debug.
We can control the switches to the corresponding level of log information in the application. For example, when the INFO level is defined, all DEBUG level log information in the application will not be printed out.
Advantages of Log4net
It can provide an accurate environment for the application to runtime, allowing developers to find bugs in the application as soon as possible
Once the Log output code is added to the program, the log information can be generated and output during the program running without manual intervention.
Log information can be output to different places (consoles, files, etc.) for future research purposes.
practice
How to configure log4net to make log classification logs into different log files
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type=".Log4NetConfigurationSectionHandler, log4net" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> </startup> <log4net> <root> <priority value="ALL" /> <appender-ref ref="RollingLogFileAppender" /> <appender-ref ref="RollingLogWarnFileAppender"/> <appender-ref ref="RollingLogFatalFileAppender"/> <appender-ref ref="RollingLogInfoFileAppender"/> <!--<appender-ref ref="ColoredConsoleAppender" />--> </root> <appender name="RollingLogFileAppender" type=""> <param name="File" value="Log\\Error\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy_MM_dd'.log'" /> <param name="RollingStyle" value="Date" /> <layout type=""> <conversionPattern value="%r %date [%thread] %-5level- %message%newline" /> </layout> <filter type=""> <levelMin value="ERROR" /> <levelMax value="ERROR" /> </filter> </appender> <appender name="RollingLogWarnFileAppender" type=""> <param name="File" value="Log\\Warn\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy_MM_dd'.log'" /> <param name="RollingStyle" value="Date" /> <layout type=""> <conversionPattern value="%r %date [%thread] %-5level- %message%newline" /> </layout> <filter type=""> <levelMin value="WARN" /> <levelMax value="WARN" /> </filter> </appender> <appender name="RollingLogFatalFileAppender" type=""> <param name="File" value="Log\\Fatal\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy_MM_dd'.log'" /> <param name="RollingStyle" value="Date" /> <layout type=""> <conversionPattern value="%r %date [%thread] %-5level- %message%newline" /> </layout> <filter type=""> <levelMin value="FATAL" /> <levelMax value="FATAL" /> </filter> </appender> <appender name="RollingLogInfoFileAppender" type=""> <param name="File" value="Log\\Info\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyy_MM_dd'.log'" /> <param name="RollingStyle" value="Date" /> <layout type=""> <conversionPattern value="%r %date [%thread] %-5level- %message%newline" /> </layout> <filter type=""> <levelMin value="INFO" /> <levelMax value="INFO" /> </filter> </appender> <!--<appender name="ColoredConsoleAppender" type=""> <mapping> <level value="FATAL" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="WARN" /> <foreColor value="Yellow" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Green" /> </mapping> <layout type=""> <conversionPattern value="%r %date [%thread] %-5level- %message%newline" /> </layout> </appender>--> </log4net> </configuration>
Use code
private static readonly ILog logInfo = ("loginfo"); static LoggingHelper() { IntiLocalLog(); } /// <summary> /// Log critical error log /// </summary> /// <param name="message">Message content</param> /// <param name="ex">Exception information</param> /// <param name="isWriteDebug">Is it output in output</param> public static void Fatal(string message, Exception ex = null, bool isWriteDebug = true) { (() => { (message, ex); }).ContinueWith((t) => { ("Log serious error message logging error" + ().Name); }, ); } /// <summary> /// Log error log /// </summary> /// <param name="message">Message content</param> /// <param name="ex">Exception information</param> /// <param name="isWriteDebug">Is it output in output</param> public static void Error(string message, Exception ex = null, bool isWriteDebug = true) { (() => { (message, ex); }).ContinueWith((t) => { ("Log exception information logging error" + ().Name); }, ); } /// <summary> /// Log warning log /// </summary> /// <param name="message">Message content</param> /// <param name="ex">Exception information</param> /// <param name="isWriteDebug">Is it output in output</param> public static void Warn(string message, Exception ex = null, bool isWriteDebug = true) { (() => { (message, ex); }).ContinueWith((t) => { ("Log warning message logging error" + ().Name); }, ); } /// <summary> /// Record information log /// </summary> /// <param name="message">Message content</param> /// <param name="ex">Exception information</param> /// <param name="isWriteDebug">Is it output in output</param> public static void Info(string message, Exception ex = null, bool isWriteDebug = true) { (() => { (message, ex); }).ContinueWith((t) => { ("Log information logging error" + ().Name); }, ); } /// <summary> /// Record the debug log /// </summary> /// <param name="message">Message content</param> public static void Debug(string message) { (() => { (message); }).ContinueWith((t) => { ("Log debug information logging error" + ().Name); }, ); } #region Private Method private static void IntiLocalLog() { (); } #endregion
This is the article about C# configuration log4net to record log classification into different log files. For more related C# log4net log classification content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!