asp.net log4net 设置

添加log4net.config文件




  
    
      



之后Global.asax里面添加


    public class Global : System.Web.HttpApplication
    {
        public static readonly log4net.ILog logger = log4net.LogManager.GetLogger("ApplicationErrorLogger");

        protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~") + @"\log4net.config"));
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            var ex = Server.GetLastError();
            logger.Error(ex);
            Server.ClearError();
            Response.Write("系统错误!" + ex.Message);
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }


你可能感兴趣的:(Asp.Net)