C# 使用Log4Net记录日志(进阶篇)

配置文件log4net_config.xml中的内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<!-- 
		.NET application configuration file     
		This file must have the exact same name as your application with .config appended to it. 
		
		For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config. 
		It must also be in the same directory as the application. 
	-->
<!-- This section contains the log4net configuration settings -->
<log4net>
	<!-- Define some output appenders -->
	<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
		<file value="rolling-log.txt" />
		<appendToFile value="true" />
		<maxSizeRollBackups value="10" />
		<maximumFileSize value="100" />
		<rollingStyle value="Size" />
		<staticLogFileName value="true" />
		<layout type="log4net.Layout.PatternLayout">
			<header value="[Header]
" />
			<footer value="[Footer]
" />
			<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
		</layout>
	</appender>
	<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
		<file value="log.txt" />
		<!-- Example using environment variables in params -->
		<!-- <file value="${TMP}\log-file.txt" /> -->
		<sppendToFile value="true" />
		<!-- An alternate output encoding can be specified -->
		<!-- <encoding value="unicodeFFFE" /> -->
		<layout type="log4net.Layout.PatternLayout">
			<header value="[Header]
" />
			<footer value="[Footer]
" />
			<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
		</layout>
		<!-- Alternate layout using XML			
			<layout type="log4net.Layout.XMLLayout" /> -->
	</appender>
	<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
		</layout>
	</appender>
	<appender name="NetSendAppender" type="log4net.Appender.NetSendAppender">
		<threshold value="ERROR" />
		<server value="SQUARE" />
		<recipient value="nicko" />
		<layout type="log4net.Layout.PatternLayout">
			<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
		</layout>
	</appender>
	<!-- Example of how to configure the AdoNetAppender to connect to MS Access -->
	<appender name="ADONetAppender_Access" type="log4net.Appender.AdoNetAppender">
		<connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Work\cvs_root\log4net-1.2\access.mdb;User Id=;Password=;" />
		<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
		<parameter>
			<parameterName value="@log_date" />
			<dbType value="String" />
			<size value="255" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@thread" />
			<dbType value="String" />
			<size value="255" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%thread" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@log_level" />
			<dbType value="String" />
			<size value="50" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%level" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@logger" />
			<dbType value="String" />
			<size value="255" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%logger" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@message" />
			<dbType value="String" />
			<size value="1024" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%message" />
			</layout>
		</parameter>
	</appender>
	<!-- Example of how to configure the AdoNetAppender to connect to MS SQL Server -->
	<appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
		<bufferSize value="1" />
		<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
		<connectionString value="data source=192.168.110.110;initial catalog=DBtest;integrated security=false;persist security info=True;User ID=sa;Password=saPassword" />
		<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
		<parameter>
			<parameterName value="@log_date" />
			<dbType value="DateTime" />
			<!-- 
				<layout type="log4net.Layout.PatternLayout">
					<conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
				</layout> 
				-->
			<layout type="log4net.Layout.RawTimeStampLayout" />
		</parameter>
		<parameter>
			<parameterName value="@thread" />
			<dbType value="String" />
			<size value="255" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%thread" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@log_level" />
			<dbType value="String" />
			<size value="50" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%level" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@logger" />
			<dbType value="String" />
			<size value="255" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%logger" />
			</layout>
		</parameter>
		<parameter>
			<parameterName value="@message" />
			<dbType value="String" />
			<size value="4000" />
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%message" />
			</layout>
		</parameter>
	</appender>
	<!-- Setup the root category, add the appenders and set the default level -->
	<root>
		<level value="DEBUG" />
		<appender-ref ref="LogFileAppender" />
		<appender-ref ref="ADONetAppender_SqlServer" />
		<!--<appender-ref ref="ConsoleAppender" />-->
	</root>
</log4net>
该配置文件可以用于往文本文件及sql server数据库中记录日志(启用哪一个由下面的配置决定):

<appender-ref ref="LogFileAppender" />
<appender-ref ref="ADONetAppender_SqlServer" />

日志类封装:

/// <summary>
    /// 日志记录类(记录到数据库)
    /// </summary>
    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 { }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="sInfo"></param>
        public static void WriteLog(string sInfo)
        {
            m_log.Error(sInfo);
        }
        /// <summary>
        /// 记录debug信息
        /// </summary>
        /// <param name="e"></param>
        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");
        }

        /// <summary>
        /// 配置log4net环境
        /// </summary>
        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);
        }
    }
    /// <summary>
    /// 日志记录类(记录到文本文件中)
    /// </summary>
    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 { }
        }

        /// <summary>
        /// 返回ILog接口
        /// </summary>
        private static log4net.ILog Log
        {
            get
            {
                return m_log;
            }
        }

        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="sInfo"></param>
        public static void WriteLog(string sInfo)
        {
            m_log.Error(sInfo);
        }


        /// <summary>
        /// 记录debug信息
        /// </summary>
        /// <param name="e"></param>
        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");
        }

        /// <summary>
        /// 配置log4net环境
        /// </summary>
        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);
        }

    }

    /// <summary>
    /// 文件 操作
    /// </summary>
    public static class FilesOperate
    {
        /// <summary>
        /// 获取App的当前路径 \\结束
        /// </summary>
        /// <returns></returns>
        public static string getAppPath()
        {
            return GetAssemblyPath();
        }

        /// <summary>
        /// 获取Assembly的运行路径 \\结束
        /// </summary>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 文件夹复制
        /// </summary>
        /// <param name="sSourceDirName">原始路径</param>
        /// <param name="sDestDirName">目标路径</param>
        /// <returns></returns>
        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
        }


        /// <summary> 
        /// 启动其他的应用程序 
        /// </summary> 
        /// <param name="file">应用程序名称</param> 
        /// <param name="workdirectory">应用程序工作目录</param> 
        /// <param name="args">命令行参数</param> 
        /// <param name="style">窗口风格</param> 
        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;
            }
        }

        /// <summary>
        /// 获得本地计算机名
        /// </summary>
        /// <returns></returns>
        public static string GetComputerName()
        {
            return Dns.GetHostName();
        }

        /// <summary>
        /// 获得计算机IP地址
        /// </summary>
        /// <returns></returns>
        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";
            }
        }

        /// <summary>
        /// 描述:创建目录
        /// </summary>
        /// <returns></returns>
        public static bool CreateFolder(string sFolder)
        {
            //如果临时文件夹不存在,则创建该文件夹
            if (!Directory.Exists(sFolder))
            {
                Directory.CreateDirectory(sFolder);
            }
            return true;
        }
    }


log4net 基础篇: 点击打开链接
源码下载地址: 点击打开链接 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值