下载log4net.dll程序添加引用
官方网站:http://logging.apache.org/log4net/
官方给出的示例:http://logging.apache.org/log4net/release/config-examples.html
以下是log4net.cofig xml配置文件信息和调试程序:
log4net记录日志到SqlServer
<bufferSize value="100" /> 当缓存达到100kb时候存入数据库
<!--数据库中创建日志表 CREATE TABLE [dbo].[Log] ( [Id] [int] IDENTITY (1, 1) NOT NULL, [Date] [datetime] NOT NULL, [Thread] [varchar] (255) NOT NULL, [Level] [varchar] (50) NOT NULL, [Logger] [varchar] (255) NOT NULL, [Message] [varchar] (4000) NOT NULL, [Exception] [varchar] (2000) NULL ) --> <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <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> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> <root> <!--启用数据库记录日志--> <appender-ref ref="AdoNetAppender_SQLServer" /> </root> </log4net> </configuration>
log4net记录日志到文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <!--定义输出到文件中--> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <!--定义文件存放位置--> <file value="LogFileAppender_log.txt" /> <appendToFile value="true" /> <datePattern value="yyyyMMdd-HH:mm:ss" /> <layout type="log4net.Layout.PatternLayout"> <!--输出格式--> <!--样例:2010-11-17 15:50:23,443 [9] (D:/CSProjects/Log4NetDemo/Log4NetDemo/Program.cs:27) FATAL Log4NetDemo.Program [(null)] - fatal System.Exception: 在发生了一个致命错误,Exception Id:548828745--> <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:文件:所在行%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" /> </layout> </appender> <root> <!--记录日志到文件--> <appender-ref ref="LogFileAppender" /> </root> </log4net> </configuration>
log4net 使用Rolling方式记录日志按照日期记录到文件(运行两次程序可看出效果)
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name="RollingLogFileAppender_DateFormat" type="log4net.Appender.RollingFileAppender"> <file value="RollingLogFileAppender_DateFormat_log.txt" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <!--<datePattern value="yyyyMMdd-HHmm" />--> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread](%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <!--使用Rolling方式记录日志按照日期--> <appender-ref ref="RollingLogFileAppender_DateFormat" /> </root> </log4net> </configuration>
log4net 使用Rolling方式记录日志
每个日志文件最大100KB,生成的日志文件名会是log.txt.1,log.txt.2 ...log.txt.10
如果记录的日志超过10个,会从log.txt.1开始覆盖
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="RollingFileAppender_log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <!--使用Rolling方式记录日志开始覆盖--> <appender-ref ref=RollingFileAppender /> </root> </log4net> </configuration>
log4net 定义输出到控制台命令行中
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] (%file:%line) %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <!-- 定义输出到控制台命令行中--> <appender-ref ref="ConsoleAppender" /> </root> </log4net> </configuration>
测试程序如下:
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; //此处注意添加以下 [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Random random = new Random(); for (int i = 0; i < 1000; i++) { //创建日志记录组件实例 //ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); ILog log=log4net.LogManager.GetLogger(typeof(Program)); //记录错误日志 log.Error("error", new Exception("在这里发生了一个异常,Error Number:"+random.Next())); //记录严重错误 log.Fatal("fatal", new Exception("在发生了一个致命错误,Exception Id:"+random.Next())); //记录一般信息 log.Info("提示:系统正在运行"); //记录调试信息 log.Debug("调试信息:debug"); //记录警告信息 log.Warn("警告:warn"); } //Console.WriteLine("日志记录完毕。"); Console.Read(); } } }
以下附测试程序:测试程序
时间: 2024-10-18 10:57:14