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

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
日志服务 SLS,月写入数据量 50GB 1个月
简介: 配置文件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 .c

配置文件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 基础篇: 点击打开链接
源码下载地址: 点击打开链接 




相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
1月前
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
|
15天前
|
SQL 关系型数据库 MySQL
MySQL事务日志-Undo Log工作原理分析
事务的持久性是交由Redo Log来保证,原子性则是交由Undo Log来保证。如果事务中的SQL执行到一半出现错误,需要把前面已经执行过的SQL撤销以达到原子性的目的,这个过程也叫做"回滚",所以Undo Log也叫回滚日志。
MySQL事务日志-Undo Log工作原理分析
|
22天前
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
75 18
|
20天前
|
JSON 安全 API
.net 自定义日志类
在.NET中,创建自定义日志类有助于更好地管理日志信息。示例展示了如何创建、配置和使用日志记录功能,包括写入日志文件、设置日志级别、格式化消息等。注意事项涵盖时间戳、日志级别、JSON序列化、线程安全、日志格式、文件处理及示例使用。请根据需求调整代码。
42 13
|
22天前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
57 12
|
20天前
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
|
20天前
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
|
20天前
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
|
1月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
48 5
|
1月前
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
118 13