<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!--添加自定義節點:log4net type:解析類名,程序集名(log4net.dll)-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<!--<level value="DEBUG"/>-->
<!--控制級別,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF ALL表示允許所有的日志請求。OFF是拒絕所有的請求。-->
<!--比如定義級別為INFO,則INFO級別向下的級別,比如DEBUG日志將不會被記錄-->
<level value="ALL"/>
<!--根據log級別記錄到不同的日志文件-->
<appender-ref ref="ErrorLog" />
</root>
<!--Error-->
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="Log/"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--日期的格式,每天換一個文件記錄,如不設置則永遠只記錄一天的日志,需設置-->
<DatePattern value="yyyyMMdd"_Error.log"" />
<!--日志文件名是否為靜態-->
<StaticLogFileName value="false"/>
<!--多線程時采用最小鎖定-->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<!--布局(向用戶顯示最后經過格式化的輸出信息)-->
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date 線程ID:[%thread] 日志級別:%-5level 出錯類:%logger property:[%property{NDC}] - 錯誤描述:%message %newline" />
</layout>
<securityContext type="log4net.Util.WindowsSecurityContext">
<credentials value="Process" />
</securityContext>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="ERROR" />
</filter>
</appender>
<!--Error-->
</log4net>
</configuration>
using GrabNovelApi;
using log4net;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
namespace Grab.api
{
public static class LogHelper
{
private static readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>();
/// <summary>
/// 獲取記錄器
/// </summary>
/// <param name="source">soruce</param>
/// <returns></returns>
private static ILog GetLogger(Type source)
{
if (Loggers.ContainsKey(source))
{
return Loggers[source];
}
else
{
ILog logger = LogManager.GetLogger(Startup.repository.Name,source);
Loggers.TryAdd(source, logger);
return logger;
}
}
/* Log a message object */
/// <summary>
/// 調試信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Debug(object source, string message)
{
Debug(source.GetType(), message);
}
/// <summary>
/// 調試信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="ps">ps</param>
public static void Debug(object source, string message, params object[] ps)
{
Debug(source.GetType(), string.Format(message, ps));
}
/// <summary>
/// 調試信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Debug(Type source, string message)
{
ILog logger = GetLogger(source);
if (logger.IsDebugEnabled)
{
logger.Debug(message);
}
}
/// <summary>
/// 關鍵信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Info(object source, object message)
{
Info(source.GetType(), message);
}
/// <summary>
/// 關鍵信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Info(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsInfoEnabled)
{
logger.Info(message);
}
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Warn(object source, object message)
{
Warn(source.GetType(), message);
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Warn(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsWarnEnabled)
{
logger.Warn(message);
}
}
/// <summary>
/// 錯誤信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Error(object source, object message)
{
Error(source.GetType(), message);
}
/// <summary>
/// 錯誤信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Error(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsErrorEnabled)
{
logger.Error(message);
}
}
/// <summary>
/// 失敗信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Fatal(object source, object message)
{
Fatal(source.GetType(), message);
}
/// <summary>
/// 失敗信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
public static void Fatal(Type source, object message)
{
ILog logger = GetLogger(source);
if (logger.IsFatalEnabled)
{
logger.Fatal(message);
}
}
/* Log a message object and exception */
/// <summary>
/// 調試信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Debug(object source, object message, Exception exception)
{
Debug(source.GetType(), message, exception);
}
/// <summary>
/// 調試信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Debug(Type source, object message, Exception exception)
{
GetLogger(source).Debug(message, exception);
}
/// <summary>
/// 關鍵信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Info(object source, object message, Exception exception)
{
Info(source.GetType(), message, exception);
}
/// <summary>
/// 關鍵信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Info(Type source, object message, Exception exception)
{
GetLogger(source).Info(message, exception);
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Warn(object source, object message, Exception exception)
{
Warn(source.GetType(), message, exception);
}
/// <summary>
/// 警告信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Warn(Type source, object message, Exception exception)
{
GetLogger(source).Warn(message, exception);
}
/// <summary>
/// 錯誤信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Error(object source, object message, Exception exception)
{
Error(source.GetType(), message, exception);
}
/// <summary>
/// 錯誤信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Error(Type source, object message, Exception exception)
{
GetLogger(source).Error(message, exception);
}
/// <summary>
/// 失敗信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Fatal(object source, object message, Exception exception)
{
Fatal(source.GetType(), message, exception);
}
/// <summary>
/// 失敗信息
/// </summary>
/// <param name="source">source</param>
/// <param name="message">message</param>
/// <param name="exception">ex</param>
public static void Fatal(Type source, object message, Exception exception)
{
GetLogger(source).Fatal(message, exception);
}
}
}