標籤:技術分享 sqlserver 資料 ide exce system base value art
日誌原理 就是觀察者模式(訂閱發布模式) , 具體和委託很像
使用步驟 在 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(); } }
測試
大功告成!!!!!
mvc中日誌的原理和使用步驟