/*log4net.config*/
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout" value="DateTime %date{yyyy-MM-dd HH:mm:ss} LogType %-5level %message %newline" />
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date] [%-5level] [%logger] [%message] [%exception] %newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log" />
<param name="Encoding" value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd'.log'" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="DateTime %date{yyyy-MM-dd HH:mm:ss} LogType %-5level %message %newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="Info" />
<appender-ref ref="ConsoleAppender" />
<!--<appender-ref ref="FileAppender" />-->
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
/*LogHelper.cs*/
using log4net;
using log4net.Config;
using System;
using System.IO;
using System.Xml;
namespace MingYu.Utility.Log4Net
{
/// <summary>
/// 日志记录类(记录到文本文件中)
/// </summary>
public static class LogHelper
{
private static readonly string LOG_DIR = "log";
private static readonly string LOG_FILE = LOG_DIR + "\log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
private const string LOG4NET_CONFIG = "Log4Net\log4net.config";
private static string logFormats = "ModuleBig {0} ModuleSmall {1} Filter1 {2} Filter2 {3} Message {4}";
static LogHelper()
{
try
{
ConfigureLoad();
}
catch { }
}
/// <summary>
/// 返回ILog接口
/// </summary>
private static ILog Log { get; } = LogManager.GetLogger(typeof(LogHelper));
/// <summary>
/// 记录debug信息
/// </summary>
/// <param name="message"></param>
/// <param name="e"></param>
/// <param name="moduleBig"></param>
/// <param name="moduleSmall"></param>
/// <param name="filter1"></param>
/// <param name="filter2"></param>
public static void Error(string message, Exception e, string moduleBig = "Default", string moduleSmall = "Default", string filter1 = "Default", string filter2 = "Default")
{
string errorInfo = message;
if (null != e)
{
errorInfo += ("Message : " + e.Message);
errorInfo += ("Source : " + e.Source);
errorInfo += ("StackTrace : " + e.StackTrace);
errorInfo += ("TargetSite : " + e.TargetSite);
}
if (string.IsNullOrWhiteSpace(errorInfo))
{
return;
}
Log.Error(string.Format(@logFormats, moduleBig, moduleSmall, filter1, filter2, errorInfo));
}
/// <summary>
///
/// </summary>
/// <param name="ex"></param>
/// <param name="moduleBig"></param>
/// <param name="moduleSmall"></param>
/// <param name="filter1"></param>
/// <param name="filter2"></param>
public static void Error(Exception ex, string moduleBig = "Default", string moduleSmall = "Default", string filter1 = "Default", string filter2 = "Default")
{
if (null == ex)
{
return;
}
string errorInfo = "";
errorInfo += ("Msg : " + ex.Message);
errorInfo += ("Source : " + ex.Source);
errorInfo += ("StackTrace : " + ex.StackTrace);
Log.Error(string.Format(@logFormats, moduleBig, moduleSmall, filter1, filter2, errorInfo));
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="moduleBig"></param>
/// <param name="moduleSmall"></param>
/// <param name="filter1"></param>
/// <param name="filter2"></param>
public static void Info(string info, string moduleBig = "Default", string moduleSmall = "Default", string filter1 = "Default", string filter2 = "Default")
{
if (string.IsNullOrWhiteSpace(info))
{
return;
}
Log.Info(string.Format(@logFormats, moduleBig, moduleSmall, filter1, filter2, info));
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="moduleBig"></param>
/// <param name="moduleSmall"></param>
/// <param name="filter1"></param>
/// <param name="filter2"></param>
public static void Warning(string info = "", string moduleBig = "Default", string moduleSmall = "Default", string filter1 = "Default", string filter2 = "Default")
{
Log.Warn(string.Format(@logFormats, moduleBig, moduleSmall, filter1, filter2, info));
}
/// <summary>
/// 配置log4net环境
/// </summary>
private static void ConfigureLoad()
{
var repository = LogManager.CreateRepository(typeof(LogHelper));
var path = AppDomain.CurrentDomain.BaseDirectory;
XmlConfigurator.Configure(repository, new FileInfo(Path.Combine(path,"bin", "Log4Net", "log4net.config")));
}
}
}