.NET技术-3.0. 日志插件 log4net
.NET技术-3.0. 日志插件 log4net
背景:框架 NetFramework3.5
1. 新建控制台程序
2. 程序包管理器控制台中 增加 Nuget包
Install-Package log4net
3. 项目根目录中 增加名称为 log4net.config 的配置文件
文件属性选项目卡中设置 复制到输出目录改成“始终复制”
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!-- 将日志写入到本地txt文件中 --> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径--> <param name= "File" value= "Logs\"/> <!--是否是向文件中追加日志--> <param name= "AppendToFile" value= "true"/> <!--log保留天数--> <param name= "MaxSizeRollBackups" value= "10"/> <!--日志文件名是否是固定不变的--> <param name= "StaticLogFileName" value= "false"/> <!--日志文件名格式为:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd"/> <!--日志根据日期滚动--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n%d 耗时%r毫秒 进程ID:%t 行号:%L 当前日志对象名称:%c 日志当前级别:%p 输出语句所在文件名:%F 日志消息:%m" /> </layout> </appender> <!-- 控制台前台显示日志 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red" /> </mapping> <mapping> <level value="WARN" /> <foreColor value="Yellow" /> </mapping> <mapping> <level value="INFO" /> <foreColor value="White" /> </mapping> <mapping> <level value="DEBUG" /> <foreColor value="Blue" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n%d 程序运行时间:%r毫秒 进程ID:%t 行号:%L 当前日志对象名称:%c 日志当前级别:%p 输出语句所在文件名:%F 日志消息:%m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <!-- 将日志写入到SQLServer中 --> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="0" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=.\MSSQL2008R2;initial catalog=MeiTuan;integrated security=false;persist security info=True;User ID=sa;Password=111111" /> <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> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="RollingLogFileAppender"/> <appender-ref ref="AdoNetAppender"/> </root> </log4net> </configuration>
4. 打开项目里Properties文件夹下的AssemblyInfo.cs文件。往里面追加以下内容:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
5. 调用方式
class Program { static void Main(string[] args) { InitLog4Net(); var logger = LogManager.GetLogger(typeof(Program)); logger.Info("消息"); logger.Warn("警告"); logger.Error("异常"); logger.Fatal("错误"); Console.ReadLine(); } private static void InitLog4Net() { var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); XmlConfigurator.ConfigureAndWatch(logCfg); } }
6 在SQLServer中创建一张日志表(往对应配置文件中的连接字符串里面的数据库新增表):(未测)
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 )
ok,配置完成,可查看日志。
参考:https://blog.csdn.net/xc917563264/article/details/79498313