1、新建Logs的Class类;代码如下:
public class ApiLogs
{
public static int Log_Level { get; set; }
private static bool initialized = false;
private static readonly ILog log = LogManager.GetLogger("ApiLogs");
static ApiLogs()
{
if (!initialized)
{
Initialize();
initialized = true;
}
}
private static void Initialize()
{ FileInfo fi = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
if (fi.Exists)
{
XmlConfigurator.ConfigureAndWatch(fi);
}
if (!LogManager.GetRepository().Configured)
{
string filePath = (Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetCallingAssembly().GetName().CodeBase).LocalPath), "Carpa.config"));
fi = new FileInfo(filePath);
if (fi.Exists)
{
XmlConfigurator.ConfigureAndWatch(fi);
}
else
{
Trace.TraceWarning("配置文件 {0} 不存在", fi.FullName);
}
}
} /// <summary>
/// 调试输出
/// </summary>
/// <param name="message"></param>
public static void Debug(object message)
{
if (Log_Level >= )
{
log.Debug(message);
}
} /// <summary>
/// 调试输出
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Debug(object message, Exception exception)
{
if (Log_Level >= )
{
log.Debug(message, exception);
}
} /// <summary>
/// 格式化调试输出
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void DebugFormat(string format, params object[] args)
{
if (Log_Level >= )
{
log.DebugFormat(format, args);
}
} /// <summary>
/// 信息输出
/// </summary>
/// <param name="message"></param>
public static void Info(object message)
{
if (Log_Level >= )
{
log.Info(message);
}
} /// <summary>
/// 信息输出
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Info(object message, Exception exception)
{
if (Log_Level >= )
{
log.Info(message, exception);
}
} /// <summary>
/// 格式化信息输出
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void InfoFormat(string format, params object[] args)
{
if (Log_Level >= )
{
log.InfoFormat(format, args);
}
} /// <summary>
/// 警告输出
/// </summary>
/// <param name="message"></param>
public static void Warn(object message)
{
if (Log_Level >= )
{
log.Warn(message);
}
} /// <summary>
/// 警告输出
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Warn(object message, Exception exception)
{
if (Log_Level >= )
{
log.Warn(message, exception);
}
} /// <summary>
/// 格式化警告输出
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void WarnFormat(string format, params object[] args)
{
if (Log_Level >= )
{
log.WarnFormat(format, args);
}
} /// <summary>
/// 错误输出
/// </summary>
/// <param name="message"></param>
public static void Error(object message)
{
if (Log_Level >= )
{
log.Error(message);
}
} /// <summary>
/// 错误输出
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Error(object message, Exception exception)
{
if (Log_Level >= )
{
log.Error(message, exception);
}
} /// <summary>
/// 格式化错误输出
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void ErrorFormat(string format, params object[] args)
{
if (Log_Level >= )
{
log.ErrorFormat(format, args);
}
} /// <summary>
/// 致命输出
/// </summary>
/// <param name="message"></param>
public static void Fatal(object message)
{
log.Fatal(message);
} /// <summary>
/// 致命输出
/// </summary>
/// <param name="message"></param>
/// <param name="exception"></param>
public static void Fatal(object message, Exception exception)
{
log.Fatal(message, exception);
} /// <summary>
/// 格式化致命输出
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void FatalFormat(string format, params object[] args)
{
log.FatalFormat(format, args);
} /// <summary>
/// 是否启用调试输出
/// </summary>
public static bool IsDebugEnabled
{
get { return log.IsDebugEnabled; }
} /// <summary>
/// 是否启用信息输出
/// </summary>
public static bool IsInfoEnabled
{
get { return log.IsInfoEnabled; }
} /// <summary>
/// 是否启用信息输出
/// </summary>
public static bool IsWarnEnabled
{
get { return log.IsWarnEnabled; }
} /// <summary>
/// 是否启用错误输出
/// </summary>
public static bool IsErrorEnabled
{
get { return log.IsErrorEnabled; }
} /// <summary>
/// 是否启用致命输出
/// </summary>
public static bool IsFatalEnabled
{
get { return log.IsFatalEnabled; }
}
}
2、定义Log4net.config配制文件
<?xml version="1.0" encoding="gb2312" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections> <log4net>
<appender name="DebugInfoAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="data\CarpaLog_Info.txt" />
<appendToFile value="true" />
<maximumFileSize value="1024KB"/>
<maxSizeRollBackups value=""/>
<CountDirection value=""/>
<RollingStyle value="Size"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender> <appender name="WarnErrorFatalAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="data\CarpaLog_Error.txt" />
<appendToFile value="true" />
<RollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
</appender> <appender name="DebugAppender" type="Carpa.Logging.Appender.DebugAppender">
<layout type="Carpa.Logging.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %type - %message%newline" />
</layout>
</appender> <appender name="ApiInfoAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="data\ApiLog.txt" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<appendToFile value="true" />
<maximumFileSize value="5MB"/>
<maxSizeRollBackups value=""/>
<CountDirection value=""/>
<RollingStyle value="Size"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level - %message%newline" />
</layout> <!--<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="ApiLogs" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />--> </appender> <logger name="ApiLogs">
<level value="ALL" />
<appender-ref ref="ApiInfoAppender" />
</logger>
<logger name="test.Logging.Log">
<level value="INFO" />
<appender-ref ref="FileAppender" />
<appender-ref ref="DebugInfoAppender" />
<appender-ref ref="WarnErrorFatalAppender" />
</logger>
</log4net>
</configuration>
核心说明:通过该类logManager.GetLogger("Name")的Name参数来区分,并定义配制文件中logger中name与Getlogger的Name名称一致