log4net使用

参考地址:

《C# log4net 的配置》 

《C#使用log4net记录日志》

 

项目引用nuget包log4net

log4net使用_第1张图片

 

 

App.config文件中加入引用

在configuration节点中加入以下内容

    
      
      
      
        "ALL" />
        ref ref="SysAppender" />
      
      
      "WebLogger">
        "DEBUG" />
      
      "SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
        "File" value="log/" />
        "AppendToFile" value="true" />
        "RollingStyle" value="Date" />
        "DatePattern" value=""Logs_"yyyyMMdd".txt"" />
        "StaticLogFileName" value="false" />
        "log4net.Layout.PatternLayout,log4net">
          "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        
      
      "consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
        "log4net.Layout.PatternLayout,log4net">
          "ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
        
      
    
View Code

 

复杂的配置文件


  
  "logerror">
    "ALL" />
    ref ref="ErrorAppender" />
  
  
  "loginfo">
    "ALL" />
    ref ref="InfoAppender" />
  
  
  "ErrorAppender" type="log4net.Appender.RollingFileAppender">
    "File" value="Log\\LogError\\" />
    "AppendToFile" value="true" />
    "MaxSizeRollBackups" value="100" />
    "MaxFileSize" value="10240" />
    "StaticLogFileName" value="false" />
    "DatePattern" value="yyyyMMdd".htm"" />
    "RollingStyle" value="Date" />
    
    "log4net.Layout.PatternLayout">
      "ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>"  />
    
  
  
  "InfoAppender" type="log4net.Appender.RollingFileAppender">
    "File" value="Log\\LogInfo\\" />
    "AppendToFile" value="true" />
    "MaxFileSize" value="10240" />
    "MaxSizeRollBackups" value="100" />
    "StaticLogFileName" value="false" />
    "DatePattern" value="yyyyMMdd".htm"" />
    "RollingStyle" value="Date" />
    
    "log4net.Layout.PatternLayout">
      "ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>"  />
    
  
View Code

 

log4net使用_第2张图片

AssemblyInfo.cs文件中加入内容

log4net使用_第3张图片

 

 

添加一个帮助类

public class LogHelper
    {
        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }

        public static void WriteLog(string info, Exception ex)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(info, ex);
            }
        }
    }

 

调用帮助类

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string a = "FF";
                int b = Convert.ToInt32(a);
            }
            catch (Exception ex){
                LogHelper.WriteLog(ex.Message.ToString(), ex);
            }
        }

 

你可能感兴趣的:(log4net使用)