Log4Net logging system using the information in the database and notepad

Disclaimer: This article is a blogger original article, shall not be reproduced without the consent of bloggers. https://blog.csdn.net/hxpjava1/article/details/32714855

First, the use Log4Net logging to step Notepad

        1, the log4net.dll files added to the project references

        2, write log records for example the following classes:

  /// <summary>
    /// 日志记录器
    /// </summary>
    public class LogWriter
    {
        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("Logger");
            if (log.IsDebugEnabled)
            {
                log.Debug(message);
            }
            log = null;
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("Logger");
            if (log.IsErrorEnabled)
            {
                log.Error(message);
            }
            log = null;
        }

        /// <summary>
        /// 记录致命错误
        /// </summary>
        /// <param name="message"></param>
        public static void Fatal(string message)
        {

            log4net.ILog log = log4net.LogManager.GetLogger("Logger");
            if (log.IsFatalEnabled)
            {
                log.Fatal(message);
            }
            log = null;
        }

        /// <summary>
        /// 记录一般信息
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("Logger");
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
            log = null;
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="message"></param>
        public static void Warn(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("Logger");
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
            log = null;
        } 

    }
         3, the contents of the file is added log4net.config web application file in the root folder for example, see
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <!--<root>
       
   <level value="WARN" /> 
   <appender-ref ref="rollingFile" /> 
   
    </root>-->
    <logger name="Logger">
      <level value="ALL" />
      <appender-ref ref="rollingFile" />
    </logger>
    <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
      <param name="File" type="" value="log/" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <datePattern value="yyyyMMdd'.txt'" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>
  4, for example, the following call can be generated in the root file folder of the application generates a log file folder when used, and in the folder name is generated at time log file     

            LogWriter.Info("请选择认证方式Info");
            LogWriter.Warn("请选择认证方式Warn");
            LogWriter.Fatal("请选择认证方式Fatal");
            LogWriter.Error("请选择认证方式Error");
            LogWriter.Debug("请选择认证方式Debug");

two. The use Log4Net to save the log files into the database

       Reference link Click to open the link (Note: The configuration here is the sql server database)

       If you want to save to Oracle or other databases, please link to click to open the link . Here you can query to use Log4Net to save the log information to profile information from different databases.

      Which I was doing to save log information to an Oracle database, encountered some problems, our system uses Oracle.DataAccess.dll, but very much the presentation given sample is System.Data.OracleClient.dll, all leading to in the configuration

 
  
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 应该更改为 
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess.Client, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
Here we must pay attention to the accuracy of Version and PublicKeyToken, otherwise it is not inserted into the database.

 
   
  

       

Guess you like

Origin www.cnblogs.com/ldxsuanfa/p/10958629.html
Recommended