1.引用log4net dll文件
2.创建log4net.config文件并配置文件信息
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- Author:GaoBingBing--> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <!--写入到数据库--> <appender name="AdoNetAppender_MySql" type="log4net.Appender.AdoNetAppender"> <!--错误队列数据达到5个才持久化到数据库--> <bufferSize value="5" /> <param name="ConnectionType" value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"/> <param name="ConnectionString" value="server=localhost;database=specialdb;Uid=sa;Pwd=sa;old syntax=yes"/> <commandText value="INSERT INTO log_serviceslog(log_datetime,log_thread,log_level,log_logger,log_message) VALUES (@log_date, @thread, @log_level, @logger, @message)"/> <parameter> <parameterName value="@log_date"/> <dbType value="DateTime"/> <layout type="log4net.Layout.RawTimeStampLayout"/> <!--<layout type="log4net.Layout.PatternLayout" value="%date{yyyy‘-‘MM‘-‘dd HH‘:‘mm‘:‘ss}" />--> </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> <level value="ALL"/> <appender-ref ref="AdoNetAppender_MySql"/> </root> <logger name="*"> <level value="ALL" /> <appender-ref ref="AdoNetAppender_MySql" /> </logger> </log4net> </configuration>
3.在项目根目录下新建CustomServiceHostFactory并继承ServiceHostFactory重写CreateServiceHost方法
CustomServiceHost继承ServiceHost重写CustomServiceHost方法启动错误日志(需引用System.ServiceModel.Activation)
using System; using System.ServiceModel.Activation; using System.ServiceModel; namespace topicWCFServices { public class CustomServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost( Type serviceType, Uri[] baseAddresses) { CustomServiceHost customServiceHost = new CustomServiceHost(serviceType, baseAddresses); return customServiceHost; } } public class CustomServiceHost : ServiceHost { public CustomServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { log4net.Config.XmlConfigurator.Configure(); } protected override void ApplyConfiguration() { base.ApplyConfiguration(); } } }
4.在AssemblyInfo.cs文件中加上
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
5.创建错误日志记录文件
using System; using System.Collections.Generic; using System.Linq; using System.Web; public class TopicServiceLog { //获取实例 private static log4net.ILog myLogger = log4net.LogManager.GetLogger("AdoNetAppender_MySql"); //错误级别:Info public static void Info(string message) { myLogger.Info(message); } //错误级别:Debug public static void Debug(string message) { myLogger.Debug(message); } //错误级别:Warn public static void Warn(string message) { myLogger.Warn(message); } //错误级别:Fatal public static void Fatal(string message) { myLogger.Fatal(message); } //错误级别:Error public static void Error(string message) { myLogger.Error(message); } }
ok,结束,已测试可以使用
时间: 2024-10-17 18:36:56