zoukankan      html  css  js  c++  java
  • log4net笔记

    asp.net MVC使用log4net的步骤:

    1.通过Nuget安装log4net,安装首选在web项目上,主要用于在controller中捕捉用户的操作和异常结果等信息。

    2.配置好log4net的配置文件,有两种方法,一种直接在web.congfig中添加,另一种单独添加一个配置文件,比如log4net.config文件。

    3.将配置文件引入到程序初始化的地方,在mvc中,可以在Global.asax.cs中的Application_Start方法中添加如下代码:

    RouteConfig.RegisterRoutes(RouteTable.Routes);

    //注册 log4net 由此可以使用自定义的log4net.config配置文件
    log4net.Config.XmlConfigurator.Configure(
    new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "/log4net.config")
    );

    BundleConfig.RegisterBundles(BundleTable.Bundles);

    4.上面指明要读取的配置信息是从log4net.config中读取。

    5.对log4net.config配置文件进行详细配置信息的设置,appender、logger等等信息。

    6.定义LoggerHelper类,该类的作用是定义log对象,绑定配置文件中的logger,在该对象中有info方法,调用后会执行日志写入功能。

    7.定义例如MonitorLog类(具体的日志类型的类),该类的字段一般都是需要获取的信息片段,该类的方法GetMessage(自定义)可以是组合信息后的整段信息。

    8.定义Filter类,例如

    /// <summary>
    /// 统计跟踪器
    /// 错误日志跟踪
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class StatisticsTrackerAttribute : ActionFilterAttribute, IExceptionFilter

    该类继承ActionFilterAttribute类和其他接口(非必需),重写下列几个方法

    #region Action监控

    public override void OnActionExecuting(ActionExecutingContext filterContext)

    public override void OnActionExecuted(ActionExecutedContext filterContext)

    #endregion

    #region View监控

    public override void OnResultExecuting(ResultExecutingContext filterContext)

    public override void OnResultExecuted(ResultExecutedContext filterContext)

    #endregion

    #region 错误日志

    public void OnException(ExceptionContext filterContext)

    #endregion

    该类的几个方法中会创建6、7定义类的对象,并调用方法。

    9、最后将8的Filter类嵌入到全局中,具体可以在FilterConfig中

    FilterConfig类的RegisterGlobalFilters(GlobalFilterCollection filters)静态方法中添加代码

    filters.Add(new StatisticsTrackerAttribute()); //new的类即自定义的Filter类

  • 相关阅读:
    python执行命令行调试工具pdb
    py常用标准库
    Python的 垃圾回收机制
    私有化 : _x: 单前置下划线,私有化属性或方法;__xx:双前置下划线;__xx__:双前后下划线;属性property
    python 解析docx文档将文档名修改为docx里的合同编号
    LINUX7 HA(mysql)
    datax oracle到mysql数据抽取
    SQL优化案例(谓词越界)
    How to Find which Session is Holding a Particular Library Cache Lock (Doc ID 122793.1)
    窗口函数
  • 原文地址:https://www.cnblogs.com/dog12345/p/8136106.html
Copyright © 2011-2022 走看看