C# winform使用log4net步骤

1.使用工程中引入log4net的dll

2.主工程的app.config中增加log4net配置


<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="AdjustMeter.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  configSections>
  <system.diagnostics>
    <sources>
      
      <source name="DefaultSource" switchName="DefaultSwitch">
        <listeners>
          <add name="FileLog"/>
          
          
        listeners>
      source>
    sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    switches>
    <sharedListeners>
      <add name="FileLog"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
           initializeData="FileLogWriter"/>
      
      
    sharedListeners>
  system.diagnostics>
  <applicationSettings>
    <AdjustMeter.My.MySettings>
      <setting name="HL_Meter_WebReference_Service" serializeAs="String">
        <value>http://172.16.23.63/webjmj/Service.asmxvalue>
      setting>
    AdjustMeter.My.MySettings>
  applicationSettings>
  <log4net>
    <root>
      
      
      
      <level value="ALL" />
      <appender-ref ref="InfoAppender" />
      <appender-ref ref="DebugAppender" />
      <appender-ref ref="ErrorAppender" />
    root>
    
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      
      <file value="TermLog\\Info\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd'.txt'"/>
      <staticLogFileName value="false"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%t]%-5p %c - %m%n"/>
      layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      filter>
    appender>
    <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
      
      <file value="TermLog\\Frames\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd'.txt'"/>
      <staticLogFileName value="false"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%t]Frames %c - %m%n"/>
      layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="DEBUG" />
      filter>
    appender>
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      
      <file value="TermLog\\Error\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd'.txt'"/>
      <staticLogFileName value="false"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%t]%-5p %c - %n%m%n%n"/>
      layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="ERROR" />
        <param name="LevelMax" value="ERROR" />
      filter>
    appender>
  log4net>
configuration>

具体哪个文件属于哪个级别,通过appener中的filter过滤

3.代码中使用

using System;
using System.Collections.Generic;
using System.Text;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace TermComm
{
    public class LogHelper
    {
        public enum LogType
        {
            //Debug=0,
            Error=1,
            //Fatal=2,
            Info=3,
            //Warn=4
        }

        public static void WriteErrorLog(Exception ex, Type t)
        {
            WriteLog(LogHelper.LogType.Error, t, "StackTrace:" + System.Environment.NewLine + ex.StackTrace +
                            System.Environment.NewLine +
                            "TargetSite:" + ex.TargetSite +
                            System.Environment.NewLine +
                            "Message:" + ex.Message);
        }

        /// 
        /// 输出日志
        /// 
        /// 
        /// 
        /// 
        public static void WriteLog(LogType logType, Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);

            switch (logType)
            {
                case LogType.Error:
                    if (CommPara.OutputErr)
                    {
                        log.Error(msg);
                    }
                    break;
                case LogType.Info:
                    if (CommPara.OutputInfo)
                    {
                        log.Info(msg);
                    }
                    break;
                default:
                    break;
            }
        }

        /// 
        /// 记录帧
        /// 
        /// 
        /// 
        public static void WriteFrame(Type t, byte[] frames)
        {
            if (!CommPara.OutputFrame) return;

            if (frames != null && frames.Length > 0)
            {
                log4net.ILog log = log4net.LogManager.GetLogger(t);

                string strFrames = string.Empty;
                for (int i = 0; i < frames.Length; i++)
                {
                    strFrames += frames[i].ToString("X2") + " ";
                }

                log.Debug(strFrames);
            }
        }
    }
}

你可能感兴趣的:(log4net)