配置文件log4net_config.xml中的内容如下:
该配置文件可以用于往文本文件及sql server数据库中记录日志(启用哪一个由下面的配置决定):
日志类封装:
///
/// 日志记录类(记录到数据库)
///
public static class LogisTracToSqlDB
{
private static readonly log4net.ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const string LOG4NET_CONFIG = "log4net_config.xml";
static LogisTracToSqlDB()
{
try
{
ConfigureLoad();
}
catch { }
}
///
/// 输出日志
///
///
public static void WriteLog(string sInfo)
{
m_log.Error(sInfo);
}
///
/// 记录debug信息
///
///
public static void WriteLog(Exception e)
{
WriteLog(e.ToString());
//WriteLog("--------------------------------------[本次异常开始]--------------------------------------");
//WriteLog("Message : " + e.Message);
//WriteLog("Source : " + e.Source);
//WriteLog("StackTrace : " + e.StackTrace);
//WriteLog("TargetSite : " + e.TargetSite);
//WriteLog("--------------------------------------[本次异常结束]--------------------------------------\r\n");
}
///
/// 配置log4net环境
///
private static void ConfigureLoad()
{
XmlDocument doc = new XmlDocument();
//使用当前dll路径
string sPath = FilesOperate.GetAssemblyPath();
if (!sPath.EndsWith("\\"))
{
sPath += "\\";
}
sPath += LOG4NET_CONFIG;
doc.Load(@sPath);
XmlElement myElement = doc.DocumentElement;
log4net.Config.XmlConfigurator.Configure(myElement);
}
}
///
/// 日志记录类(记录到文本文件中)
///
public static class LogisTrac
{
private static readonly string LOG_DIR = "日志";
private static readonly string LOG_FILE = LOG_DIR + "\\log" + System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
private const string LOG4NET_CONFIG = "log4net_config.xml";
private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(typeof(LogisTrac));
static LogisTrac()
{
try
{
ConfigureLoad();
}
catch { }
}
///
/// 返回ILog接口
///
private static log4net.ILog Log
{
get
{
return m_log;
}
}
///
/// 输出日志
///
///
public static void WriteLog(string sInfo)
{
m_log.Error(sInfo);
}
///
/// 记录debug信息
///
///
public static void WriteLog(Exception e)
{
WriteLog("--------------------------------------[本次异常开始]--------------------------------------");
WriteLog("Message : " + e.Message);
WriteLog("Source : " + e.Source);
WriteLog("StackTrace : " + e.StackTrace);
WriteLog("TargetSite : " + e.TargetSite);
WriteLog("--------------------------------------[本次异常结束]--------------------------------------\r\n");
}
///
/// 配置log4net环境
///
private static void ConfigureLoad()
{
XmlDocument doc = new XmlDocument();
//使用当前dll路径
string sPath = FilesOperate.GetAssemblyPath();
if (!sPath.EndsWith("\\"))
{
sPath += "\\";
}
//查看Log文件夹是否存在,如果不存在,则创建
string sLogDir = sPath + LOG_DIR;
if (!Directory.Exists(sLogDir))
{
Directory.CreateDirectory(sLogDir);
}
string sLogFile = sPath + LOG_FILE;
sPath += LOG4NET_CONFIG;
doc.Load(@sPath);
XmlElement myElement = doc.DocumentElement;
//修改log.txt的路径
XmlNode pLogFileAppenderNode = myElement.SelectSingleNode("descendant::appender[@name='LogFileAppender']/file");
// Create an attribute collection from the element.
XmlAttributeCollection attrColl = pLogFileAppenderNode.Attributes;
attrColl[0].Value = sLogFile;
log4net.Config.XmlConfigurator.Configure(myElement);
}
}
///
/// 文件 操作
///
public static class FilesOperate
{
///
/// 获取App的当前路径 \\结束
///
///
public static string getAppPath()
{
return GetAssemblyPath();
}
///
/// 获取Assembly的运行路径 \\结束
///
///
public static string GetAssemblyPath()
{
string sCodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
sCodeBase = sCodeBase.Substring(8, sCodeBase.Length - 8); // 8是 file:// 的长度
string[] arrSection = sCodeBase.Split(new char[] { '/' });
string sDirPath = "";
for (int i = 0; i < arrSection.Length - 1; i++)
{
sDirPath += arrSection[i] + Path.DirectorySeparatorChar;
}
return sDirPath;
}
///
/// 文件夹复制
///
/// 原始路径
/// 目标路径
///
public static bool CopyDirectory(string sSourceDirName, string sDestDirName)
{
if (string.IsNullOrEmpty(sSourceDirName) || string.IsNullOrEmpty(sDestDirName))
{
return false;
}
//不复制.svn文件夹
if (sSourceDirName.EndsWith("svn"))
{
return true;
}
if (sSourceDirName.Substring(sSourceDirName.Length - 1) != Path.DirectorySeparatorChar.ToString())
{
sSourceDirName = sSourceDirName + Path.DirectorySeparatorChar;
}
if (sDestDirName.Substring(sDestDirName.Length - 1) != Path.DirectorySeparatorChar.ToString())
{
sDestDirName = sDestDirName + Path.DirectorySeparatorChar;
}
#region 复制函数
if (Directory.Exists(sSourceDirName))
{
if (!Directory.Exists(sDestDirName))
{
Directory.CreateDirectory(sDestDirName);
}
foreach (string item in Directory.GetFiles(sSourceDirName))
{
File.Copy(item, sDestDirName + System.IO.Path.GetFileName(item), true);
}
foreach (string item in Directory.GetDirectories(sSourceDirName))
{
CopyDirectory(item, sDestDirName + item.Substring(item.LastIndexOf(Path.DirectorySeparatorChar) + 1));
}
}
return true;
#endregion
}
///
/// 启动其他的应用程序
///
/// 应用程序名称
/// 应用程序工作目录
/// 命令行参数
/// 窗口风格
public static bool StartProcess(string file, string workdirectory, string args, ProcessWindowStyle style)
{
try
{
Process pMyProcess = new Process();
ProcessStartInfo pStartInfo = new ProcessStartInfo(file, args);
pStartInfo.WindowStyle = style;
pStartInfo.WorkingDirectory = workdirectory;
pMyProcess.StartInfo = pStartInfo;
pMyProcess.StartInfo.UseShellExecute = false;
pMyProcess.Start();
return true;
}
catch (Exception ex)
{
//LogAPI.debug(ex);
return false;
}
}
///
/// 获得本地计算机名
///
///
public static string GetComputerName()
{
return Dns.GetHostName();
}
///
/// 获得计算机IP地址
///
///
public static string GetIPAddress()
{
try
{
string sComputerName;
sComputerName = GetComputerName();
string sIpAddress = "";
IPAddress[] addr = Dns.GetHostAddresses(sComputerName);
//for (int i = 0; i < addr.Length; i++)
//{
// sIpAddress += addr[i].ToString() + " ";
//}
sIpAddress = addr[0].ToString();
return sIpAddress;
}
catch (Exception ep)
{
//LogAPI.debug(ep);
return "127.0.0.1";
}
}
///
/// 描述:创建目录
///
///
public static bool CreateFolder(string sFolder)
{
//如果临时文件夹不存在,则创建该文件夹
if (!Directory.Exists(sFolder))
{
Directory.CreateDirectory(sFolder);
}
return true;
}
}
log4net 基础篇:
点击打开链接
源码下载地址:
点击打开链接