Use Apache log4net

First, set Nuget.

Tools → → Nuget Package Manager Package Manager disposed → NuGet Package Manager → → check package source nuget.org https://www.nuget.org/api/v2/→ determined.

Second, add the log4net Nuget package.

NuGet package Tools → Nuget → Package Manager management solutions

 

 Third, add a profile

Right-click the project → Add → New Item → application configuration file (web or winform ......) and modify the name: value log4net.config → Right-click generated out of the log4net.config → Properties → "Copy to Output Directory" of change as if newer copy. 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <!-- Set root logger level to ERROR and its appenders -->
    <!--<root>
      <level value="ALL" />
      <appender-ref ref="SysAppender" />
    </root>
    -->
    <!--Error日志-->
    <logger name="LogError">
      <level value="ERROR" />
      <appender-ref ref="RollingLogFileAppender" />
    </logger>
    <!--Info日志-->
    <logger name="LogInfo">
      <level value="INFO" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <!--错误日志-->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log\\LogError\\" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <staticLogFileName value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <!--每条日志末尾的文字说明-->
        <!--输出格式-->
        <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
        <!-- <param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p &lt;BR&gt;%n异 常 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n &lt;HR Size=1&gt;"  />-->
      </layout>
    </appender>
    <!--Info日志-->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\LogInfo\\" />
      <param name="AppendToFile" value="true" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>
 
Fourth, the format specification

a message output from the message log, as ILog.Debug (...) output:% m (Message) 
% n-(new new Line): linefeed 
% d (datetime): output time of the current statement running 
% r (run time): Output program is consumed from the operation to execute the current statement is the number of milliseconds 
% t (thread id): current statement where the thread ID 
% the p-(priority): the current priority level from which logs that DEBUG, INFO, WARN ... and so 
% c (class ): the current name of the log object, for example: 
% f (file): filename where output statements. 
% l (line): the line number where the output statement. 
% Number: indicates that the minimum length, if not, then padded with spaces, such as "% -5level" represents the minimum width level is five characters long, if the actual length of the spaces not filled places five characters.

Fifth, in order to facilitate the call you can add a class Help
 public class Logger
    {
        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("LogInfo");
        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("LogError");
        public static void Info(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }
        public static void ErrorLog(string errorMsg, Exception ex = null)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(errorMsg, ex);
            }
        }
    }
V. Form configuration settings or configuration files used in the Global.asax log4net global file
  log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
Sixth, then you can use
  Example 1:
    Logger.Info ( "Software start");
  Example 2:
     the try
                {
                    spReceive1.Open ();
                }
                the catch (Exception E)
                {
                    Logger.ErrorLog ( "open the scanning head 1", E);
                    MessageBox.Show ( "scan head 1 open failed!");
                }
 
 

Guess you like

Origin www.cnblogs.com/-liuming-/p/11913468.html