log4net

使用

把Log4Net.Dll复制到项目的根目录下,然后添加Log4Net的引用。
在web.config文件中新增引用

<configuration>
    <configSections>
        <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
        ...

增加一个写日志文件的类 loghelper.cs


using log4net;
using System;

public static class LogFactory
{
    /// 
    /// 错误日志的标签
    /// 
    public const string Error = "ErrorLogger";

    /// 
    /// 日志记录的标签
    /// 
    public const string Info = "InfoLogger";

    /// 
    /// 初始化
    /// 
    public static void LogInitialize()
    {
        string config = "~/log4net.config";
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(config)));
        Log4NetHelper.Info("测试日志!!!!");
    }
}

///  日志辅助类 log4net
/// 
public static class Log4NetHelper
{
    ///  错误日志
    /// 
    /// 
    public static void Error(string msg)
    {
        var log = LogManager.GetLogger(LogFactory.Error);
        log.Error(msg);
    }

    ///  错误日志,带异常
    /// 
    /// 
    /// 
    public static void Error(string msg, Exception ex)
    {
        var log = LogManager.GetLogger(LogFactory.Error);
        log.Error(msg, ex);
    }

    ///  日志记录
    /// 
    /// 
    public static void Info(string msg)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.Info(msg);
    }

    ///  日志记录
    /// 
    /// 
    /// 
    public static void Info(string msg, Exception ex)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.Info(msg, ex);
    }

    ///  Debug日志记录
    /// 
    /// 
    public static void Debug(string msg)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.Debug(msg);
    }

    ///  Debug日志记录
    /// 
    /// 
    /// 
    public static void Debug(string msg, Exception ex)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.Debug(msg, ex);
    }

    ///  Format日志记录
    /// 
    /// 
    /// 
    public static void Format(string format, object arg0)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.InfoFormat(format, arg0);
    }

    ///  Format日志记录
    /// 
    /// 
    /// 
    /// 
    public static void Format(string format, object arg0, object arg1)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.InfoFormat(format, arg0, arg1);
    }

    ///  Format日志记录
    /// 
    /// 
    /// 
    /// 
    /// 
    public static void Format(string format, object arg0, object arg1, object arg2)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.InfoFormat(format, arg0, arg1, arg2);
    }

    ///  Format日志记录
    /// 
    /// 
    /// 
    /// 
    public static void Format(IFormatProvider provider, string format, params object[] args)
    {
        var log = LogManager.GetLogger(LogFactory.Info);
        log.InfoFormat(provider, format, args);
    }
}

在项目的根目录中配置一份log4net.config文件。


<log4net debug="false">
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="Log/cq.log"/>
    <param name="datePattern" value="YY-MM-dd HH:mm"/>
    <param name="AppendToFile" value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
    layout>
  appender>
  <appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
    layout>
  appender>
  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
    layout>
  appender>
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log\cq"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value="_yyyyMMdd'.log'"/>
    <staticLogFileName value="false"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c - %m%n"/>
    layout>
  appender>
  <root>
    <level value="INFO"/>
    
    <appender-ref ref="RollingLogFileAppender"/>
  root>
log4net>

将这个文件的属性页打开,高级->复制到输出目录->始终复制。在这个文件中可以配置很多的appender的信息,这里是按照日期切换文件。

在Global.asax文件中,增加初始化逻辑。

LogFactory.LogInitialize();

日志:

Log4NetHelper.Info(String.Format("LoginCheck uid: {0}", uid));
引用
  • 官网

你可能感兴趣的:(c#)