Log system log information to Notepad and the database using Log4net

Source: Internet
Author: User
Tags oracleconnection system log log4net



First, use log4net to log to Notepad steps



1. Add the Log4net.dll file to the project reference



2, write the logger class as follows:


/// <summary>
    /// logger
    /// </summary>
    Public class LogWriter
    {
        /// <summary>
        /// Record debugging information
        /// </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>
        /// Record error message
        /// </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>
        /// Record fatal error
        /// </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>
        /// Record general information
        /// </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>
        /// Record warning message
        /// </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, add the Log4net.config file contents to the root directory of the Web program as shown below
<?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, use the following call to generate a log folder in the root directory of the application, and in the folder will generate a time-named log file



LogWriter.Info ("Please select the authentication method Info");
             LogWriter.Warn ("Please select the authentication method Warn");
             LogWriter.Fatal ("Please select the authentication method Fatal");
             LogWriter.Error("Please select authentication method Error");
             LogWriter.Debug ("Please select authentication mode Debug");


Second, the log file will be saved to the database using Log4net



Refer to the link click to open the link (note: Here is the SQL Server database configured)



To save to Oracle or another database, please click on the link to open the link, where you can query to use Log4net to save the log information to a different database configuration file information.



One of the things I did when I saved the log information to the Oracle database was that we were using Oracle.DataAccess.dll, but in a lot of the examples it was System.Data.OracleClient.dll, All causes in the configuration


<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  Should be changed to
<connectionType value="Oracle.DataAccess.Client.OracleConnection, Oracle.DataAccess.Client, Version=4.112.3.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
Be sure to pay attention to the accuracy of Version and PublicKeyToken here, otherwise it will not be able to insert into the database. 








Related Article

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.