日志原理 就是观察者模式(订阅发布模式) , 具体和委托很像
使用步骤 在 log4net框架下
1.在Common中定义LogHelper入口类
1.定义LogHelper类 namespace LTeasyOA.Common { public class LogHelper { public static Queue<string> ExceptionStringQueue = new Queue<string>();//日志消息队列 public static List<ILogWriter> LogWriterList = new List<ILogWriter>();//接受订阅者的集合 提供入口 public static void WriteLog(string exceptionText) { lock (ExceptionStringQueue) { ExceptionStringQueue.Enqueue(exceptionText); } } //静态函数的调用时机,是在类被实例化或者静态成员被调用的时候进行调用, //并且是由.net框架来调用静态构造函数来初始化静态成员变量。 static LogHelper() //静态构造函数 只要是调用该类的任何构造方法 或实例化 都会调用此构造方法来初始化和执行日志函数 { //LogWriterList.Add(new TextFileWrite()); //LogWriterList.Add(new SqlServerWriter()); LogWriterList.Add(new Log4NetWriter()); //添加订阅者 和委托赋值原理一样 ThreadPool.QueueUserWorkItem(o => { lock (ExceptionStringQueue) { string str = ExceptionStringQueue.Dequeue(); //将异常信息写到日志文件中去 但有可能是文本文件 也可能是数据库文件 foreach (var ListItem in LogWriterList) { ListItem.WriteLogInfo(str); } //控件在这里帮我们自动实现 有一个框架 Log4Net } }); } } }
当然可以用多态
实现接口
namespace LTeasyOA.Common { public interface ILogWriter { void WriteLogInfo(string str); } }
子类实现
public class Log4NetWriter:ILogWriter { public void WriteLogInfo(string str) { ILog logWriter = log4net.LogManager.GetLogger("Demo"); logWriter.Error(str); } }
static LogHelper中实例化
2.
定义一个继承HandleErrorAttribute的子类 实现OnException()方法
namespace LTeasyOA.UI.Portal2.Models { public class MyExceptionFilterAttribute:HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { //直接把错误写到 日志中去 base.OnException(filterContext); Common.LogHelper.WriteLog(filterContext.Exception.ToString()); } } }
filterConfig中添加过滤器
using LTeasyOA.UI.Portal2.Models; using System.Web; using System.Web.Mvc; namespace LTeasyOA.UI.Portal2 { public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); filters.Add(new MyExceptionFilterAttribute()); //第一步 // 过滤器 目前三种 ActionFilter ResultFilter ExceptionFilter } } }
web.config添加配置 两步
1.块配置
1 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 2 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 3 </configSections>
2.log4net节点配置
<log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ERROR"/> <appender-ref ref="SysAppender"/> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="WebLogger"> <level value="DEBUG"/> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="App_Data/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="日志开始" value=" ----------------------header-------------------------- " /> <param name="日志结束" value=" ----------------------footer-------------------------- " /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net>
global中初始化
public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //加载配置 初始化 log4net.Config.XmlConfigurator.Configure(); } }
测试
大功告成!!!!!
时间: 2024-10-13 05:37:30